[英]Problems with Testing Spring Boot Application with Multi Maven Project Setup
我目前在 Spring Boot 和多 Maven 項目結構方面遇到了一些問題。 我正在使用 Spring Boot 4.3.1。
我的項目結構如下:
parent
-- pom.xml
-- application
-- pom.xml
-- src
-- main
-- java
-- Application.java (annotated with @SpringBootApplication)
-- test
-- java
-- MyApplicationTest.java (annotated with @SpringBootTest)
-- library
-- pom.xml
-- src
-- main
-- java (...)
-- test
-- java
-- MyLibraryTest.java (annotated with @SpringBootTest)
application
模塊依賴於library
。
MyApplicationTest
工作得很好,但運行MyLibraryTest
,我失敗並出現以下錯誤:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test at org.springframework.util.Assert.state(Assert.java:392)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOr FindConfigurationClasses(SpringBootTestContextBootstrapper.java:173)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:133)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:305)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:78)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
我的第一個猜測是library
需要依賴於application
,但這會導致循環。
有沒有辦法解決這個問題? 如何正確構建我的應用程序?
非常感謝您的建議。
MyLibraryTest
如下所示:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class MyLibraryTest {
@Autowired
private MyService service;
@Test
public void testMyService_Save() {...}
}
您需要確保library
模塊的pom.xml
包含 -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
這是模塊使用@SpringBootTest
注釋所@SpringBootTest
。 您可能已經在app
模塊中使用了相同的內容,但未包含在library
模塊中。
好吧發布編輯,發現問題可能是在執行 JpaTest 時無法找到 @SpringBootConfiguration 的重復
還引用了同一線程中托馬斯的回答,這里
關於
@DataJpaTest
和其他一些注解的事情是他們在當前包中尋找@SpringBootConfiguration
注解,如果他們在那里找不到它,他們就會遍歷包層次結構直到找到它。例如,如果您的測試類的完全限定名稱是
com.example.test.JpaTest
而您的應用程序的名稱是com.example.Application
,那么您的測試類將能夠找到@SpringBootApplication
(並且在其中,@SpringBootConfiguration
)。但是,如果應用程序駐留在包層次結構的不同分支中,例如
com.example.application.Application
,它將找不到它。
對您來說似乎就是這種情況,您試圖在不同的模塊本身中測試應用程序。 因此,您看到的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.