问题描述
我是 Spring Boot 和测试的新手。我目前正在学习 baeldung 的教程 (https://spring.io/guides/gs/testing-web/),但遇到了这个错误。
事实上,当我使用 isNull() 方法时,它通过了。但我不明白这一点,因为我的控制器类有映射等等。为什么会为空?
这是我的控制器类
或者这可能不是这个测试应该如何完成?但是他们在 baeldung 中显示如下
感谢任何帮助。
最好,
解决方法
如果您想在 spring 上下文中运行测试用例,您需要在您的测试类中提供 @SpringBootTest
注释(即,而不是在您的测试类 @SpringBootConfiguration
中提供 Smoke
)。除了 @SpringBootTest
之外的其他选项如下。
对于 junit4:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class YourTestClass {}
对于junit 5:
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class YourTestClass
SpringTestConfiguration
类包含您需要自动装配的所有 bean。有关 SpringRunner 与 SpringBootTest 的更多详细信息,请阅读 article
你试过 assertNotNull(studentController) 吗?