我有一个使用 SpringBoot2 和 mongodb 的应用程序,我正在尝试通过进行如下测试来测试某些 DTOS 的 json 序列化:
@JsonTest
@RunWith(SpringRunner.class)
public class SomeDTOTest {
@Autowired
JacksonTester < SomeDTO > json;
@Test
public void someTest() {}
}
然而,spring 正在尝试创建存储库 bean 并给我信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named 'mongoTemplate' that could not be found.
Action:
Consider defining a bean named 'mongoTemplate' in your configuration.
我有更多使用存储库的集成测试,并用 @SpringBootTests 进行注释,它们工作正常......
有没有办法限制 spring 只创建 JacksonTester bean?
最佳答案
您可以在没有 spring runner 的情况下创建一个测试。
这是一个示例 example test
加载 spring 上下文时,如果某处有 mongotemplate 的 Autowiring 注释,spring 将尝试提供它。您可能会考虑:
在测试中提供mongo模板
尝试使用@DataMongoTest,它将提供嵌入式数据库。
不需要设置自动连线
使用@Autowired(required= false)
模拟 mongotemplate
使用@MockBean注释来模拟mongoTemplate
关于java - Spring Boot尝试使用@JsonTest注释在测试中创建mongo存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49406878/