spring boot版本是2.0.0.RELEASE。
测试课程:
@RunWith(SpringRunner.class)
@SpringBootTest(classes={Application.class},webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc(secure = false)
public class ProcControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testGetProc() throws Exception {
String requestUrl = "/app/rest/query/proc";
// Request json body
JSONObject jsonBody = new JSONObject();
jsonBody.put("id", "101");
String contentBody = jsonBody.toJSONString();
MvcResult mvcResult = mockMvc.perform(post(requestUrl)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(contentBody)
).andReturn();
MockHttpServletResponse response = mvcResult.getResponse();
assertThat(response.getStatus()).isEqualTo(200);
}
}
我还尝试了另一个测试配置:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@AutoConfigureMockMvc(secure = false)
public class ProcessInstanceControllerMockMvcTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void before() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
Application.class如下:
@SpringBootApplication
@Import({ApplicationConfiguration.class})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
builder) {
return builder.sources(Application.class);
}
@Bean
public ServletRegistrationBean apiDispatcher(){
DispatcherServlet api = new DispatcherServlet();
api.setContextClass(AnnotationConfigWebApplicationContext.class);
api.setContextConfigLocation(ApiDispatcherServletConfiguration.class.getName());
ServletRegistrationBean registrationBean = new ServletRegistrationBean();
registrationBean.setServlet(api);
registrationBean.addUrlMappings("/api/*");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
registrationBean.setName("api");
return registrationBean;
}
@Bean
public ServletRegistrationBean appDispatcher(){
DispatcherServlet app = new DispatcherServlet();
app.setContextClass(AnnotationConfigWebApplicationContext.class);
app.setContextConfigLocation(AppDispatcherServletConfiguration.class.getName());
ServletRegistrationBean registrationBean = new ServletRegistrationBean();
registrationBean.setServlet(app);
registrationBean.addUrlMappings("/app/*");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
registrationBean.setName("app");
return registrationBean;
}
}
测试运行简单日志如下:
10:58:26.015 [main] [INFO]初始化Spring FrameworkServlet''o.a.c.c.C。[。[。[/]。log:180
10:58:26.015 [main] [INFO] FrameworkServlet'':初始化已开始o.s.t.w.s.TestDispatcherServlet.initServletBean:494
10:58:26.699 [main] [INFO]已映射......多行
10:58:30.408 [main] [INFO] FrameworkServlet'':初始化在4392毫秒内完成o.s.t.w.s.TestDispatcherServlet.initServletBean:513
10:58:31.159 [main] [INFO]用于FreeMarker的SpringTemplateLoader:使用资源加载器[org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@61f80d55:启动日期[2018年7月4日星期三10:57:51 CST];上下文层次结构的根]和模板加载器路径[classpath:/ templates /] o.s.u.f.SpringTemplateLoader.:62 10:58:31.163 [main] [INFO]用于Spring宏的ClassTemplateLoader已添加到FreeMarker配置中o.s.w.s.v.f.FreeMarkerConfigurer.postProcessTemplateLoaders:131 10:58:33.065 [main] [INFO]正在启动ProtocolHandler [“ http-nio-auto-1”] o.a.c.h.Http11NioProtocol.log:180
10:58:33.082 [main] [INFO]使用共享的选择器进行servlet读写o.a.t.u.n.NioSelectorPool.log:180
10:58:33.114 [main] [INFO]初始化Spring FrameworkServlet'app'o.a.c.c.C。[。[。[/]。log:180
10:58:33.115 [main] [INFO] FrameworkServlet'app':初始化开始于o.s.w.s.DispatcherServlet.initServletBean:494
10:58:33.121 [main] [INFO]刷新名称空间“ app-servlet”的WebApplicationContext:启动日期[Ced Jul 04 10:58:33 CST 2018];父级:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@61f80d55 o.s.w.c.s.AnnotationConfigWebApplicationContext.prepareRefresh:589 10:58:33.123 [main] [INFO]为[com.fg.app.servlet.AppDispatcherServletConfiguration]成功解析的类o.s.w.c.s.AnnotationConfigWebApplicationContext.loadBeanDefinitions:233
10:58:34.034 [main] [INFO]发现并支持JSR-330'javax.inject.Inject'注释以自动装配o.s.b.f.a.AutowiredAnnotationBeanPostProcessor。:154
10:58:26.699 [main] [INFO]已映射......多行
10:58:35.186 [main] [INFO] FrameworkServlet'app':初始化在2071毫秒内完成o.s.w.s.DispatcherServlet.initServletBean:513
10:58:35.199 [main] [INFO]初始化Spring FrameworkServlet'api'o.a.c.c.C。[。[。[/]。log:180
10:58:35.200 [main] [INFO] FrameworkServlet'api':初始化开始于o.s.w.s.DispatcherServlet.initServletBean:494
10:58:35.201 [main] [INFO]刷新名称空间“ api-servlet”的WebApplicationContext:启动日期[Ced Jul 04 10:58:35 CST 2018];父级:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@61f80d55 o.s.w.c.s.AnnotationConfigWebApplicationContext.prepareRefresh:589 10:58:35.202 [main] [INFO]已成功解析[com.fg.app.servlet.ApiDispatcherServletConfiguration]的类o.s.w.c.s.AnnotationConfigWebApplicationContext.loadBeanDefinitions:233
10:58:35.661 [main] [INFO]发现并支持JSR-330'javax.inject.Inject'注释以自动装配o.s.b.f.a.AutowiredAnnotationBeanPostProcessor。:154
10:58:26.699 [main] [INFO]已映射......多行
10:58:36.303 [main] [INFO] FrameworkServlet'api':初始化在1103毫秒内完成o.s.w.s.DispatcherServlet.initServletBean:513
10:58:36.305 [main] [INFO] Tomcat在端口:53711(http)上启动,上下文路径为” o.s.b.w.e.t.TomcatWebServer.start:205
10:58:36.309 [main] [INFO]在46.914秒内启动ProcControllerTest(JVM运行51.157)c.f.a.ProcControllerTest.logStarted:59
10:58:37.160 [Thread-9] [INFO]关闭org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@61f80d55:启动日期[2018年7月4日星期三10:57:51 CST];上下文层次结构的根o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext.doClose:989
10:58:37.180 [Thread-9] [INFO]为持久性单元“默认”关闭JPA EntityManagerFactory o.s.o.j.LocalContainerEntityManagerFactoryBean.destroy:572
10:58:37.196 [Thread-9] [INFO] {dataSource-1}已关闭c.a.d.p.DruidDataSource.close:1823
10:58:37.360 [localhost-startStop-2] [INFO]关闭名称空间“ app-servlet”的WebApplicationContext:启动日期[2018年7月4日星期三10:58:33 CST];父级:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@61f80d55 o.s.w.c.s.AnnotationConfigWebApplicationContext.doClose:989
10:58:37.364 [localhost-startStop-2] [INFO]关闭名称空间“ api-servlet”的WebApplicationContext:启动日期[2018年7月4日星期三10:58:35 CST];父级:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@61f80d55 o.s.w.c.s.AnnotationConfigWebApplicationContext.doClose:989
测试结果:org.junit.ComparisonFailure:预期:200实际:404
通过上面的日志,我猜想类TestDispatcherServlet
是原因。当我使用TestRestTemplate
时,它不会初始化TestDispatcherServlet
,并且很有趣。
答案 0 :(得分:0)
MockMvc
使用的DispatcherServlet
与应用程序注册的密码不同。您传递给它的URL应该与您在@Controller
批注中指定的URL完全相同。