问题描述
我有一个数据项目和一个UI项目.这两个项目都是Spring Boot应用程序.这两个项目都具有相同的根包(com.myorg),其主类带有@SpringBootApplication
注释.
I have a data project and UI project. Both projects are Spring Boot applications. Both projects have the same root package (com.myorg) with a main class annotated with @SpringBootApplication
.
数据项目的主要类别是:
Data project's main class is:
package com.myorg;
@SpringBootApplication
public class DataApplication {
public static void main(String[] args) {
SpringApplication.run(DataApplication.class, args);
}
}
UI项目的主要类是:
The UI project's main class is:
package com.myorg;
@SpringBootApplication
public class UiApplication {
public static void main(String[] args) {
SpringApplication.run(UiApplication .class, args);
}
}
UI项目通过以下Gradle依赖关系依赖于数据项目:
The UI project depends on the data project via the following Gradle dependency:
dependencies {
compile('com.myorg:data:1.0')
}
如果我运行UI应用程序,它将运行没有问题.但是,如果我在UI应用程序中运行集成测试,如下所示:
If I run the UI application, it runs without issue. However, if I run an integration test within the UI application such as follows:
package com.myorg
@RunWith(SpringRunner.class)
@SpringBootTest
public class UiIntTest {
@Test
public void contextLoads() {
}
}
发生以下初始化错误:
java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes
在数据项目的主类中,如果我将@SpringBootApplication
替换为
In the data project's main class, if I replace @SpringBootApplication
with
@Configuration
@EnableAutoConfiguration
@ComponentScan({ "com.myorg" })
尝试运行其集成测试时收到以下初始化错误:
I get the following initialization error when trying to run its integration tests:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
例如,如果我尝试运行:
For example, if I try to run:
package com.myorg
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataIntTest {
@Test
public void contextLoads() {
}
}
如何正确配置数据和UI项目?
How can I properly configure the data and UI projects?
推荐答案
您需要指定与@SpringBootTest
一起使用的Spring Boot Main类:
You need to specify which Spring Boot Main class to use along with @SpringBootTest
:
@SpringBootTest(classes = YourUiSpringBootApp.class)
这篇关于两个均带有@SpringBootApplication的Spring Boot项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!