How to throw SDK Exception

Product/components used and version/fix level:

Cumulocity Production

Detailed explanation of the problem:

Hi, I am trying to create unit test cases in JAVA, I used JAVA SDK to create my backend application. I want to know how can i deliberately send an SDK exception when I am using an Sdk method. For example:

Some Method(MeasurementRepresentation){
try{
measurementApi.create(representation);
}catch(SDKException e){
sout(e);
}
}

Now I am trying to mock this method using Junit but i have no clue what to pass so that the measurementApi.create line will throw an exception. Could you please help me with this?

Thanks,
Samanyu

So all you want to do is to get in your catch block? Then you should be good with throwing an SDK Exception manually via throw new SDKException("some failure reason") within your try-block.
If you really want to run into SDK Exception from your measurementApi call, try not providing the required fields (source, type and time for Measurements).

// Though what do you actually want to test with this?

Basically, I am trying to mock my code and I want to get inside the catch block. Now this code is very very simple, so that’s why I am not able to get inside the catch block through my test cases. I just want to know what I need to do so that the measurementApi.create line will throw an exception and my test case will land inside the catch block. I already tried to pass a null and an empty object but it is still not throwing an exception.
Hope you understand my query.

There are a bunch of examples/tutorials on how to mock within junit tests available on the internet.
You can e.g. have a look at: Getting Started With Mockito and JUnit

SDKExceptions are thrown on any kind of error with the API. This could be that the server is not responding (5xx Status) or you send invalid content (4xx Status).

1 Like

Okay thanks :slight_smile:

Certainly, Samanyu, you can deliberately make the measurementApi.create(representation) line throw an exception in your unit test using JUnit. To do this, you can use a mocking framework like Mockito to mock the behavior of the measurementApi object. Here’s a brief explanation:

  1. Mockito Setup: First, ensure you have the Mockito library in your project dependencies. You can do this by adding it to your build.gradle or pom.xml file, depending on your build tool.
  2. Mock the measurementApi Object: In your JUnit test method, mock the measurementApi object using Mockito. You can do this like so:

javaCopy code

import static org.mockito.Mockito.*;

// ...

@Test
public void testSomeMethod() {
    MeasurementRepresentation representation = // Create your representation object here

    // Mock the measurementApi
    MeasurementApi measurementApi = mock(MeasurementApi.class);

    // Define the behavior to throw an exception when create() is called
    when(measurementApi.create(representation)).thenThrow(new SDKException("Some exception message"));

    // Call your method that uses measurementApi
    YourClass yourInstance = new YourClass(measurementApi);
    yourInstance.someMethod(representation);

    // Add your assertions here to verify the behavior
}

In this example, we’re using when(measurementApi.create(representation)).thenThrow(new SDKException("Some exception message")) to instruct Mockito to throw an SDKException with the specified message when the create() method is called on the mocked measurementApi object.

Now, when you run your test, the measurementApi.create(representation) line will throw the SDKException as intended, allowing you to test how your code handles this exception scenario.

Remember to replace YourClass with the actual class where someMethod resides and create the representation object as needed for your test case.

Just a casual suggestion

1 Like

To intentionally trigger an SDK exception when testing with JUnit, use Mockito to create a mock of measurementApi and configure it to throw an exception on the create method call.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.