Reputation: 88
I have been trying for some time now to test my REST controller endpoints, using MockMvc
, Mockito
with Cucumber
.
My goal is to test my service layer, without the actual implementation being invoked. (So I don't want data to appear in the database)
I want to avoid using "in-memory" databases as I am working with a large scale project.
I recently had it to work, without the mocking, but since I've tried to mock my tests, I have been receiving NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
AddressController
snippet@Autowired
private AddressManager addressManager;
@GetMapping(value = "/{id}")
public ResponseEntity<Object> getAddress(@PathVariable("id") Long addressId) {
return new ResponseEntity<>(addressManager.getAddress(addressId), HttpStatus.OK);
// getAddress calls a data manager layer which then calls addressRepo.findOneById(addressId);
}
@PostMapping(value = "/add")
public ResponseEntity<Object> addAddress(@RequestBody Address address) {
return new ResponseEntity<>(addressManager.addAddress(address), HttpStatus.OK);
// addAddress calls a data manager layer which then calls addressRepo.save(address);
}
AddressStepDefs
snippet@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(webEnvironment= WebEnvironment.MOCK)
@Transactional
@AutoConfigureMockMvc
public class AddressStepDefs {
private MockMvc mockMvc;
private ResultActions result; // allows to track result
@InjectMocks
private AddressController addressController;
@Mock
private AddressDataManager addressService;
// given step
@Before
public void setup() throws IOException {
// must be called for the @Mock annotations to be processed and for the mock service to be injected
// into the controller under test.
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(new AddressController()).build();
}
@When("I add a new Address using POST at {string} with JSON:")
public void i_add_a_new_Address_using_POST_at_with_JSON(String request, String json) throws Exception {
/** Build a POST request using mockMvc **/
result = this.mockMvc.perform(post(request).contentType(MediaType.APPLICATION_JSON)
.content(json.getBytes()).characterEncoding("utf-8"));
}
@Then("the response code should be OK {int} and the resulting json should be:")
public void the_response_code_should_be_OK_and_the_resulting_json_should_be(Integer responseCode,
String json) throws Exception {
result.andExpect(status().is(responseCode));
result.andExpect(content().string(json));
}
@When("I request to view an Address with id {int} at {string}")
public void i_request_to_view_an_Address_with_id_at(Integer id, String request) throws Exception {
/** Build a GET request **/
result = this.mockMvc.perform(get(request + id).contentType(MediaType.APPLICATION_JSON));
}
Upvotes: 0
Views: 1444
Reputation:
Let's assume you are using a recent version of Spring Boot (and you don't need Cucumber yet for this), then you would only need AddressStepDefs
as:
@WebMvcTest(AddressController.class)
public class AddressStepDefs {
@MockBean
private AddressDataManager addressService;
@Autowired
private MockMvc mvc;
...
// Depending on how you configured your Spring beans, you might need this; try first without it ;)
@Configuration
@ComponentScan(basePackageClasses = AddressController.class)
static class TestConfig {
// ...will be used instead of the application's primary configuration
}
}
@WebMvcTest
annotation is convenient here for your use case since it's only used for Spring MVC tests that focuses only on Spring MVC components.
Then a given test could be written like:
@Test
void getAll_WhenRecordsExist() throws Exception { // HTTP 200 (OK)
final Collection<Address> expected = Arrays.asList(AddressFactory.random(), AddressFactory.random());
Mockito.when(addressService.searchAll()).thenReturn(expected);
mvc.perform(get("/addresses").accept(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().json(mapper.writeValueAsString(expected))); // ...you need Jackson's object mapper injected also as part of a class' member
Mockito.verify(service).searchAll();
}
If you are mocking
addressService
this is not an integration test, IMHO.
Upvotes: 1