[FIXED] Spring Boot – Test for controller fails with 404 code


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

@ContextConfiguration(classes = MockConfig.class)
public class WeatherStationControllerTest {

    private MockMvc mockMvc;

    private IStationRepository stationRepository;

    public void shouldReturnCorrectStation() throws Exception {


controller code snippet:

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

    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:

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

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

    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?


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:

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

You can find more in :

Answered By – O.Badr

Answer Checked By – Pedro (FixeMe Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *