I try to run unit tests for 2 methods. When I run JUnit test for testRetrieveAllHotels_ServiceLayer() method it works sucessfully, but when I try to run JUnit test for testRetrieveAllHotels_WebLayer() method, I get this error: java.lang.NullPointerException
This is the code:
package com.xxx.restfultesting.controller;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Arrays;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import com.xxx.restfultesting.business.HotelBusinessService;
import com.xxx.restfultesting.model.Hotel;
@RunWith(SpringRunner.class)
@WebMvcTest(HotelController.class)
public class HotelControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private HotelBusinessService businessService;
@Test
public void testRetrieveAllHotels_ServiceLayer(){
businessService.retrieveAllHotels();
verify(businessService, times(1)).retrieveAllHotels();
}
@Test
public void testRetrieveAllHotels_WebLayer(){
HotelController hotelController = new HotelController();
when(businessService.retrieveAllHotels()).thenReturn(
Arrays.asList(new Hotel(1, "Sofitel", 120, 20),
new Hotel(2, "Ibis", 50, 40),
new Item(3, "Marriot", 200, 15)));
hotelController.retrieveAllHotels();
verify(businessService, times(1)).retrieveAllHotels();
}
HotelController:
package com.xxx.restfultesting.controller;
import java.net.URI;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import com.xxx.restfultesting.business.HotelBusinessService;
import com.xxx.restfultesting.model.Hotel;
@RestController
public class HotelController {
@Autowired
private HotelBusinessService businessService;
@GetMapping("/items")
public List<Hotel> retrieveAllHotels() {
System.out.println("Debugging 1");
List<Hotel> hotels = businessService.retrieveAllHotels();
return items;
}
HotelBusinessService:
package com.xxx.restfultesting.business;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xxx.restfultesting.data.HotelRepository;
import com.xxx.restfultesting.model.Hotel;
@Component
public class HotelBusinessService {
@Autowired
private HotelRepository repository;
public List<Hotel> retrieveAllHotels() {
System.out.println("Debugging 2");
List<Hotel> hotels = repository.findAll();
return hotels;
}
}
I put System.out.println("Debugging 1") inside retrieveAllHotels() method from Controller and System.out.println("Debugging 2") inside retrieveAllHotels() method from Service. These are only for debugging. When I run JUnit test for testRetrieveAllHotels_ServiceLayer() method, it works successfully, but I don't get "Debugging 2" in the console.
When I run JUnit test for testRetrieveAllHotels_WebLayer() method I get the java.lang.NullPointerException error and I get "Debugging 1" in the console. I try to understand why there is not "Debugging 2" in the first case and how to run successfully the second method. Any feedback will be apreciated!
Aucun commentaire:
Enregistrer un commentaire