- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在设置一个新的 Springboot 2 应用程序,它同时使用 MYSQL 数据库和 MongoDB 数据库进行数据存储。
我无法理解如何为同时使用 DataJPA 和 DataMongo 的测试编写类。
通过使用同时使用 JPA 存储库和 Mongo 存储库的服务,在两者之间设置查询以供实际使用是一项相对简单的任务。
在编写测试用例时,我能够只为 JPA 实体(@DataJPATest
)或仅为 Mongo 实体(@DataMongoTest
)编写测试轻松使用 H2 和嵌入式 Mongo。
不可能同时使用 JPA 和 Mongo 注释定义测试类,因为 Spring 只允许 1 个 Bootstrap 。
这是来自 JPA MYSQL 的类:
@Entity
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Size(max = 255)
private String name;
@Size(max = 1000)
private String description;
}
Mongo Repos 类:
@Document
@Data
public class Review {
@Id
private String id;
@Indexed
private String title;
private String reviewText;
private boolean recommended;
@Indexed
private Integer productId;
@DBRef
private List<Comment> comments;
}
@Document
@Data
public class Comment {
@Id
private String id;
private String title;
private String commentText;
}
示例预期测试类:
@RunWith(SpringRunner.class)
@DataJpaTest
@DataMongoTest
public class ReviewRepositoryTests {
@Autowired
TestEntityManager entityManager;
使用 DataJPA 和 DataMongo 编写测试类会导致此堆栈错误:
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [xyz.cybersapien.tech.reviews.ReviewRepositoryTests]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTestContextBootstrapper)]
at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:166)
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:127)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142)
最佳答案
尝试使用 @SpringBootTest
而不是 @DataJpaTest
和 @DataMongoTest
@RunWith(SpringRunner.class)
@SpringBootTest
public class ReviewRepositoryTests {
@Autowired
TestEntityManager entityManager;
官方 Spring Boot 文档 - https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
关于java - 使用 JPATest 和 MongoDB 测试为 Polyglot Springboot 编写测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346516/
我正在尝试运行一个简单的 Junit 测试,看看我的 JpaRepository 是否确实在工作。 我不断收到的错误是: Unable to find a @SpringBootConfigurati
我正在设置一个新的 Springboot 2 应用程序,它同时使用 MYSQL 数据库和 MongoDB 数据库进行数据存储。 我无法理解如何为同时使用 DataJPA 和 DataMongo 的测试
我是一名优秀的程序员,十分优秀!