I have started newly working on testing using mockito. I have 2 questions ...
1. Question
I have method like below with optional and must have parameter so when I call this service method without the must params it should throw Exception.
@RequestMapping( method=RequestMethod.GET, produces={"application/xml", "application/json"})
public ResponseEntity<PResponse> get(@RequestParam(value="params1",required=false) String params1,
@RequestParam(value ="params2",required=false) String params2,
@RequestParam(value= "params3",required=true) String params3,
@RequestParam(value="refresh",required=false) boolean refresh,
@RequestParam(value="params4",required=true) List<String> params4)
{method logic ...}
Here params1,2,refresh
are optional and params3,4
are must so when i get request with out params3,4
it should give an error. I am trying to write a test for this using mockito
@Test(expected = RuntimeException.class)
public void throwExceptionIfMissingParams34() throws RuntimeException {
when(myService.get(argThat(new MessagesArgumentMatcher()))).thenThrow(new RuntimeException()) ;
}
I am getting error saying get() in myService can't be applied to expected Parameters:, Actual Arguments:
2. Question :
In the above get method I am calling other method which calls other service method to get data from DB
List<Product> lstProduct = productComponent.readProduct(params3,params4);
which calls
Product product = productReader.readProduct(params3, params4, Product.class);
where in ProductReader.java Service class it gets data from DB by running query. I am trying to test the
List lstProduct = productComponent.readProduct(params3,params4);
in get()
method so I tried mocking the Service Object but getting NullPointer Exception when I run the test.