Spring Boot 1.5.2 控制器层测试
Spring Boot 1.5.2 Controller Layer Testing
我正在尝试使用 @RunWith
& @SpringBootTest
测试我的控制器。
控制器
@RestController
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String get(HttpServletResponse response) throws IOException {
return "Hello World";
}
}
测试Class
@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)
@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
上添加了 webEnvironment
,它对我有用。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
我正在尝试使用 @RunWith
& @SpringBootTest
测试我的控制器。
控制器
@RestController
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String get(HttpServletResponse response) throws IOException {
return "Hello World";
}
}
测试Class
@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)
@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
上添加了 webEnvironment
,它对我有用。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)