Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - Test for controller fails with 404 code

I want to write a test for controller. Here is test snippet:

@RunWith(SpringRunner.class)
@WebMvcTest(WeatherStationController.class)
@ContextConfiguration(classes = MockConfig.class)
public class WeatherStationControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private IStationRepository stationRepository;

    @Test
    public void shouldReturnCorrectStation() throws Exception {

        mockMvc.perform(get("/stations")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());
    }
}

controller code snippet:

@RestController
@RequestMapping(value = "stations")
public class WeatherStationController {

    @Autowired
    private WeatherStationService weatherService;

    @RequestMapping(method = RequestMethod.GET)
    public List<WeatherStation> getAllWeatherStations() {
        return weatherService.getAllStations();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public WeatherStation getWeatherStation(@PathVariable String id) {
        return weatherService.getStation(id);
    }

MockConfig class:

@Configuration
@ComponentScan(basePackages = "edu.lelyak.repository")
public class MockConfig {

    //**************************** MOCK BEANS ******************************

    @Bean
    @Primary
    public WeatherStationService weatherServiceMock() {
        WeatherStationService mock = Mockito.mock(WeatherStationService.class);
        return mock;
    }

Here is error stack trace:

java.lang.AssertionError: Status 
Expected :200
Actual   :404

I can get what is wrong here.
How to fix test for controller?

like image 738
catch23 Avatar asked Jan 09 '17 13:01

catch23


2 Answers

I had the same issue. The controller was not getting picked up despite specifying it with @WebMvcTest(MyController.class). This meant all of its mappings were ignored, causing the 404. Adding @Import(MyController.class) resolved the issue, but I didn't expect the import to be necessary when I'm already specifying which controller to test.

like image 114
rougou Avatar answered Sep 23 '22 22:09

rougou


HTTP code 404, means no resource found (on the server) for your request, which I think that your controller is not visible(let me say is not scanned) by spring boot.

A simple solution is scanning a parent package in MockConfig class, so spring can pick up all beans,

@ComponentScan(basePackages = "edu.lelyak") // assuming that's the parent package in your project

if you don't like this approach, you can add the controller's package name in basePackages

@ComponentScan(basePackages = {"edu.lelyak.controller","edu.lelyak.repository") 

BTW, you don't have to manually set up WeatherStationService in MockConfig class, Spring boot can inject a mock for you and automatically reset it after each test method, you should just declare it in your test class:

@MockBean
private IStationRepository stationRepository;

On the other hand, you should mock weatherService.getAllStations() before calling get("/stations") in your test method (as you're not running integration test), so you can do:

List<WeatherStation> myList = ...;
//Add element(s) to your list
 Mockito.when(stationService.getAllStations()).thenReturn(myList);

You can find more in :

  • Testing improvements in Spring Boot 1.4

  • Spring Boot features: Testing

like image 29
O.Badr Avatar answered Sep 25 '22 22:09

O.Badr