【发布时间】: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 损坏,导入错误。很难说。
标签: java spring spring-boot junit spring-test