春季启动 - 控制器测试失败,404代码

问题描述:

我想写一个测试控制器。下面是测试片段:春季启动 - 控制器测试失败,404代码

@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()); 
    } 
} 

控制器代码片段:

@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类:

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

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

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

这是错误的堆栈跟踪:

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

我能得到什么在这里是错误的。
如何解决控制器的测试问题?

这是一个不同的方法来控制我的测试。

假设:类WeatherStationService@SpringBootApplication

接着,下面的测试类应该为你工作:

@RunWith(SpringRunner.class) 
@SpringApplicationConfiguration(WeatherStationService.class) 
@WebIntegrationTest 
public class WeatherStationControllerTest { 

    @Autowired 
    private WebApplicationContext context; 

    MockMvc mockMvc; 

    @Before 
    public void setup() { 
     mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build(); 
    } 

    @Test 
    public void shouldReturnCorrectStation() throws Exception { 
     mockMvc.perform(get("/stations") 
       .accept(MediaType.APPLICATION_JSON)) 
     .andExpect(status().isOk(); 
    } 
} 

有了这个测试设置,您应该不再需要MockConfig类。

+0

我试过了没有帮助。 –

+0

我希望这个建议能够有所帮助,而且还为时不晚。 – Nkokhelox

我不知道为什么你的测试不起作用。但是我得到了另一个适用于我的解决方案。

@SpringBootTest 
public class ControllerTest { 

    @Autowired 
    private MockMvc mockMvc; 

    @Before 
    public void setup() { 
     this.mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).build(); 
    } 

    @Test 
    public void shouldReturnCorrectStation() throws Exception { 
     mockMvc.perform(get("/stations") 
       .accept(MediaType.APPLICATION_JSON)) 
       .andExpect(status().isOk()); 
    } 
} 
+0

这里是链接到完整的[控制器测试](https://github.com/nazar-art/geo-api-data/blob/master/src/test/java/edu/lelyak/controllers/WeatherStationControllerTest.java) –

+0

你如何看待全面的控制器测试? –

+0

@nazar_art我克隆了你的github项目,但它充满了错误。不确定您的MockMvc是否正确。我会像我提供的那样轻松地在您的测试课程中注册一个控制器。 – Patrick

HTTP code 404,意味着没有发现资源(在服务器上)为您的要求,我认为你的控制器是不可见的(让我说是不是扫描)的弹簧启动。

一个简单的解决方案是在MockConfig类扫描父包,所以春季可以拿起所有的豆类,

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

,如果你不喜欢这种方式,你可以在basePackages

添加控制器的包名
@ComponentScan(basePackages = {"edu.lelyak.controller","edu.lelyak.repository") 

BTW,您不必手动设置WeatherStationServiceMockConfig类呢,春天开机即可注入模拟为你自动每个测试方法后重置它,你应该去克莱尔在您的测试类:

@MockBean 
private IStationRepository stationRepository; 

在另一方面,你应该在你的测试方法调用get("/stations")(因为你没有运行integration test)之前嘲笑weatherService.getAllStations(),所以你可以做:

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

你可以找到更多在: