Man*_* Zi 5 testing spring spring-mvc spring-test spring-boot
我无法在spring boot 1.4中运行简单的测试.我从官方网站test-the-spring-mvc-slice跟随教程,但我没有让它工作.
每次我收到以下错误:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
Run Code Online (Sandbox Code Playgroud)
任何想法,提示?
提前致谢
编辑:
这是控制器
@Controller
public class UserManagementController {
@GetMapping(value = "/gs/users/getUsers")
public @ResponseBody String getAllUsers() {
return "test";
}
}
Run Code Online (Sandbox Code Playgroud)
这是考验
@RunWith(SpringRunner.class)
@WebMvcTest(UserManagementController.class)
public class UserManagementControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void showUserView() throws Exception {
this.mvc.perform(get("/gs/users/getUsers"))
.andExpect(status().isOk())
.andDo(print());
}
}
Run Code Online (Sandbox Code Playgroud)
从我的观点来看,它与网站上的这篇文章完全相同.
该@WebMvcTest会做:
@Controller,@RestController,@JsonComponent等)现在为什么我需要配置一个"超级"类
Ser*_*man 11
搜索算法从包含测试的包开始工作,直到找到@SpringBootApplication或@SpringBootConfiguration注释类.只要您以合理的方式构建代码,通常就会找到主要配置.
所以你用@*Test注释了你的测试.它运行,检查子类中的配置,没有找到任何,抛出异常.
您必须在测试类的包或子包中具有配置,或者直接将配置类传递给@ContextConfiguration或@SpringBootTest使用注释的类@SpringBootApplication.
根据@SpringBootApplication.我已经按照你提到的方式测试了控制器@WebMvcTest:如果应用程序有类注释为@SpringBootApplication,它可以工作,并且如果没有你提到的异常则失败.你提到的文章有评论:
在这个例子中,我们省略了类,这意味着测试将首先尝试从任何内部类加载@Configuration,如果失败,它将搜索您的主@SpringBootApplication类.
Github 讨论了同样的观点.
| 归档时间: |
|
| 查看次数: |
17886 次 |
| 最近记录: |