Skip to content
Advertisement

mocking the response for the method called inside the same class

Iam trying to write JUnit test case for the below class scenario.

JavaScript

JUnit for the above class.

JavaScript

When the above JUnit test case is executed, it is throwing NullPointerException as the details value returned is null. What is the better approach to solve the above error.TIA.

–EDIT– In the above code sample, added the class3 dependency logic for better clarity.

Advertisement

Answer

In this case try this code

JavaScript

Dont forget to change your Class1 class to replace @Autowired injection with constructor injection.

(For the getTheMockResponse() it’s a private method in you test class? )

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement