我正在尝试为具有多个模块的现有应用程序编写单元测试用例,每个模块中都有主类。有不止一个类有\@SpringBootApplication。我编写了一个简单的测试用例,但由于以下错误而失败。我怎样才能继续我的测试用例之一。
java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes [Generic bean: class [com.marketshare.ReportingMainClass]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\My Data\workspace\services2\microservices\Reporting-Microservice\build\classes\java\main\com\marketshare\ReportingMainClass.class], Generic bean: class [com.marketshare.SharedMain]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/C:/My%20Data/workspace/services2/microservices/Shared-Module/build/libs/Shared-Module-1.0-SNAPSHOT.jar!/com/marketshare/SharedMain.class]] at org.springframework.util.Assert.state(Assert.java:70) at org.springframework.boot.test.context.SpringBootConfigurationFinder.scanPackage(SpringBootConfigurationFinder.java:69) at org.springframework.boot.test.context.SpringBootConfigurationFinder.findFromPackage(SpringBootConfigurationFinder.java:59) at org.springframework.boot.test.context.SpringBootConfigurationFinder.findFromClass(SpringBootConfigurationFinder.java:52)
这是代码片段
@RunWith(SpringRunner.class)
@WebMvcTest(CustomReportController.class)
public class CustomReportControllerTest {
}
我只想对我的 Controller 进行单元测试。顺便说一句,我是 Spring 世界的新手。
最佳答案
@RunWith(SpringRunner.class) 将加载 Spring 上下文。要仅测试您可以使用的 Controller
@RunWith(MockitoJUnitRunner.class)
public class CustomReportControllerTest {
}
关于具有多个模块和多个主类的spring boot项目-单元测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52090025/