springboot project with multiple module and multiple main class -unit test failing

springboot project with multiple module and multiple main class -unit test failing



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
Dataworkspaceservices2microservicesReporting-MicroservicebuildclassesjavamaincommarketshareReportingMainClass.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.





Possible duplicate of Two Spring Boot projects both with @SpringBootApplication
– Alien
Aug 30 at 6:18





Going through different doc I came to know that @WebMvcTest is good enough to test a controller in isolation. The link that you have specified is using @SpringBootTest annotation and loads the full context as per my understanding. For unit testing of my controller and make it fast I just want to test this controller.
– Braj Kishore
Aug 30 at 6:33




3 Answers
3



The @RunWith(SpringRunner.class) will load the spring context. To test just the controller you can use


@RunWith(MockitoJUnitRunner.class)
public class CustomReportControllerTest






Thanks @Vitor. I tried that and it worked. It is fast also. Then what is the difference between @RunWith(SpringRunner.class) and the other two I have mentioned above.
– Braj Kishore
Aug 30 at 16:48



There is a simple approach, you can creatae a new Spring Boot Application, let's say TestApplication under your test source folder, just like


TestApplication


test



srctestjavacomexampleTestApplication.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.


@ComponentScan



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



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)