Java 使用JPATest和MongoDB测试为Polyglot Springboot编写测试
我正在建立一个新的Springboot 2应用程序,它使用MYSQL数据库和MongoDB数据库进行数据存储 我无法理解如何为同时使用DataJPA和DataMongo的测试编写类 通过使用同时使用JPA存储库和Mongo存储库的服务,跨这两个存储库设置查询以供实际使用是一项相对简单的任务 在编写测试用例时,我能够使用H2和嵌入式Mongo轻松地为JPA实体(Java 使用JPATest和MongoDB测试为Polyglot Springboot编写测试,java,spring,mongodb,spring-boot,testing,Java,Spring,Mongodb,Spring Boot,Testing,我正在建立一个新的Springboot 2应用程序,它使用MYSQL数据库和MongoDB数据库进行数据存储 我无法理解如何为同时使用DataJPA和DataMongo的测试编写类 通过使用同时使用JPA存储库和Mongo存储库的服务,跨这两个存储库设置查询以供实际使用是一项相对简单的任务 在编写测试用例时,我能够使用H2和嵌入式Mongo轻松地为JPA实体(@DataJPATest)或Mongo实体(@DataMongoTest)编写测试 不可能同时使用JPA和Mongo注释定义测试类,因为S
@DataJPATest
)或Mongo实体(@DataMongoTest
)编写测试
不可能同时使用JPA和Mongo注释定义测试类,因为Spring只允许1个引导
这是来自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回购协议的类别:
@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;
}
使用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)
java.lang.IllegalStateException:配置错误:找到测试类[xyz.cybersapien.tech.reviews.ReviewRepositoryTests]的@BootstrapWith的多个声明:[@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)]
位于org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:166)
位于org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:127)
位于org.springframework.test.context.TestContextManager。(TestContextManager.java:124)
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner。(SpringJUnit4ClassRunner.java:142)
尝试@springbootest
而不是@DataJpaTest
和@DataMongoTest
@RunWith(SpringRunner.class)
@SpringBootTest
public class ReviewRepositoryTests {
@Autowired
TestEntityManager entityManager;
官方春季启动文件-
谢谢!似乎我错过了一件非常明显的事情:D
@RunWith(SpringRunner.class)
@SpringBootTest
public class ReviewRepositoryTests {
@Autowired
TestEntityManager entityManager;