Reputation: 351
I am trying to write unit test cases for an existing application with multiple module having main classes in each of them. There are more than one class having \@SpringBootApplication. I have written a simple test case which is failing with following error. How can I continue with my test case for one of them.
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)
Here is the code snippet
@RunWith(SpringRunner.class)
@WebMvcTest(CustomReportController.class)
public class CustomReportControllerTest {
}
I just want to unit test my controller. BTW I am new to spring world.
Upvotes: 1
Views: 2891
Reputation: 906
Add @SpringBootTest @RunWith(SpringRunner.class) @AutoConfigureMockMvc Use these annotations on the test class and Autowire MockMvc and then you can use mockMvc.perform to test Controller methods
Upvotes: 0
Reputation: 4476
There is a simple approach, you can creatae a new Spring Boot Application, let's say TestApplication
under your test
source folder, just like
src\test\java\com\example\TestApplication.java
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("......").
@EnableAsync
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
and add your package into the @ComponentScan
, you can get all the spring boot capacities and only applicable to the test propose.
Upvotes: 0
Reputation: 365
The @RunWith(SpringRunner.class) will load the spring context. To test just the controller you can use
@RunWith(MockitoJUnitRunner.class)
public class CustomReportControllerTest {
}
Upvotes: 1