我正在設(shè)置一個新的 Springboot 2 應(yīng)用程序,它同時使用 MYSQL 數(shù)據(jù)庫和 MongoDB 數(shù)據(jù)庫進行數(shù)據(jù)存儲。我無法理解如何為同時使用 DataJPA 和 DataMongo 的測試編寫類。通過使用同時使用 JPA 存儲庫和 Mongo 存儲庫的服務(wù),在兩者之間設(shè)置查詢以供實際使用是一項相對簡單的任務(wù)。在編寫測試用例時,我能夠使用 H2 和嵌入式 Mongo 輕松地為 JPA 實體 ( @DataJPATest) 或僅為 Mongo 實體 ( ) 編寫測試。@DataMongoTest不可能同時使用 JPA 和 Mongo 注釋定義測試類,因為 Spring 只允許 1 個引導(dǎo)程序。這是來自 JPA MYSQL 的類:@Entity@Datapublic 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@Datapublic 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@Datapublic class Comment { @Id private String id; private String title; private String commentText;}樣本預(yù)期測試類:@RunWith(SpringRunner.class)@DataJpaTest@DataMongoTestpublic class ReviewRepositoryTests { @Autowired TestEntityManager entityManager;使用 DataJPA 和 DataMongo 編寫測試類會導(dǎo)致此堆棧錯誤: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)]
1 回答

慕勒3428872
TA貢獻1848條經(jīng)驗 獲得超6個贊
嘗試@SpringBootTest代替@DataJpaTest和@DataMongoTest
@RunWith(SpringRunner.class)
@SpringBootTest
public class ReviewRepositoryTests {
? ? @Autowired
? ? TestEntityManager entityManager;
添加回答
舉報
0/150
提交
取消