Spring Boot 1.5.2 控制器层测试
Posted
技术标签:
【中文标题】Spring Boot 1.5.2 控制器层测试【英文标题】:Spring Boot 1.5.2 Controller Layer Testing 【发布时间】:2017-10-03 19:26:15 【问题描述】:我正在尝试使用@RunWith
和@SpringBootTest
测试我的控制器。
控制器
@RestController
public class HomeController
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String get(HttpServletResponse response) throws IOException
return "Hello World";
测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class SampleTestNGApplicationTests
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testHome() throws Exception
ResponseEntity<String> entity = this.restTemplate.getForEntity("/home", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World");
用于测试的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
现在找不到@RunWith
和@SpringBootTest
注释,我是否缺少任何库?我知道 Spring Boot 1.5.2 与 Spring Boot 1.4.2 相比有很多变化。
更新
上述问题现已解决,实际上我创建了新的测试模块,控制器位于不同的模块中。我在测试模块的main->src->java下写测试代码,我把spring-boot-starter-test
的依赖范围标记为test
,所以去掉<scope>test</scope>
,现在可以得到@RunWith
&@SpringBootTest
注释。
现在我收到错误 @ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)
错误日志
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches:
-----------------
DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)
DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:
- @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)
【问题讨论】:
jar 损坏,导入错误。很难说。 【参考方案1】:在花了很多时间之后,我在@SpringBootTest
上添加了webEnvironment
,它对我有用。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
【讨论】:
【参考方案2】:@SpringBootTest(classes = Application.class)
将加载整个应用程序上下文。我想如果你只想测试你的控制器层,那是没有必要的。你可以这样做
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HomeController.class)
public class HomeControllerTest
@Autowired
private MockMvc mvc;
@MockBean
private SomeService someservice // Mock any other dependencies which you have in your controller.
@Test
public void someTest() throws Exception
doAnswer(invocation ->
// return some predefined data which would be returned from your service layer
).when(someservice).someMethod();
mvc.perform(MockMvcRequestBuilders.get("/home").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("expectedString"));
【讨论】:
我删除了 SpringBootTest 并添加了 WebMvcTest,但出现异常“无法加载 ApplicationContext” 删除 @WebAppConfiguration 并且不要使用 RestTemplate(这需要设置整个应用程序上下文)使用 Mockmvc,如我给出的示例中所示以上是关于Spring Boot 1.5.2 控制器层测试的主要内容,如果未能解决你的问题,请参考以下文章
如何在Spring Boot中的所有测试用例之前只设置一次独立控制器?