問題分析
在創建單元測試Controller過程中,碰到了一個問題,具體如下:
MockHttpServletResponse:
Status = 404
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :404
從結果信息來看,無法找到對應的MVC服務。
環境配置
Spring Boot 1.5.13, JDK 1.8
Controller示例代碼如下:
import com.meituan.baobab.transport.supply.driver.manager.UserManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
public class MyController {
public MyController() {
log.info("init it");
}
@RequestMapping("/hello")
public String info() {
return "hello, data";
}
@Autowired
private UserManager userManager;
@RequestMapping(value="/users", method=RequestMethod.GET)
public String hello(@RequestParam("info") String info) {
log.info("Request Param:{}", info);
String resultInfo = this.userManager.getMessage(info);
log.info("resultInfo:{}", resultInfo);
return resultInfo;
}
}
測試代碼如下:
package com.meituan.baobab.transport.supply.driver.controller;
import cxxx.baseunit.TestWebConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@Slf4j
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestWebConfig.class)
@WebMvcTest(controllers={MyController.class}, secure=false)
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@MockBean
private UserManager userService;
@MockBean
private WhiteListDriverManager whiteListDriverManager;
@Test
public void testHello() throws Exception {
when(userService.getMessage("world")).thenReturn("hello,world");
MvcResult result = this.mockMvc.perform(get("/users?info=world")).andExpect(status().isOk()).andReturn();
String resultInfo = result.getResponse().getContentAsString();
log.info("resultInfo:{}", resultInfo);
Assert.assertTrue("hello,world".equalsIgnoreCase(resultInfo));
}
@Test
public void testInfo() throws Exception {
MvcResult result = this.mockMvc.perform(get("/hello")).andExpect(status().isOk()).andReturn();
}
}
解決方法
在class類之上,新增@ComponentScan註解,指定具體的掃描路徑:
@Slf4j
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestWebConfig.class)
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))
@WebMvcTest(controllers={MyController.class, WhiteListDriverController.class}, secure=false)
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;
//..........
}