春季启动1.5.2控制器层测试
我试图使用@RunWith
& @SpringBootTest
来测试我的控制器。春季启动1.5.2控制器层测试
控制器
@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)
经过大量的时间支出就可以了,我在@SpringBootTest
添加webEnvironment
,这是为我工作。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@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在我给的例子表明 – pvpkiran
罐子损坏,错了进口。很难说。 –