Reputation: 894
I have 2 SpringBoot separate applications created Core Project and Rest Project.
Core Project :- Has the services for the CRUD operations called from from Rest Project.
Rest Project :- Which has the @SpringBootApplication which calls the Core Project services from the controller.
Note: Core project is added as a dependency to Rest Project.
While creating the JUNit tests I am getting this error in JUnit:
org.springframework.boot.test.context.SpringBootTestContextBootstrapper -> Neither @ContextConfiguration nor @ContextHierarchy found for test class core.repository.SampleRepositoryTests, using SpringBootContextLoader
DEBUG org.springframework.test.context.support.AbstractContextLoader -> Did not detect default resource location for test class core.repository.SampleRepositoryTests: class path resource core/repository/SampleRepositoryTests-context.xml does not exist
DEBUG org.springframework.test.context.support.AbstractContextLoader -> Did not detect default resource location for test class core.repository.SampleRepositoryTests: class path resource core/repository/SampleRepositoryTestsContext.groovy] does not exist
INFO org.springframework.test.context.support.AbstractContextLoader -> Could not detect default resource locations for test class core.repository.SampleRepositoryTests]: no resource found for suffixes {-context.xml, Context.groovy}.
INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -> Could not detect default configuration classes for test class core.repository.SampleRepositoryTests: ProjectRepositoryTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
DEBUG org.springframework.test.context.support.ActiveProfilesUtils -> Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class core.repository.SampleRepositoryTests
The stack trace is:
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:76)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:236)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:152)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:393)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:309)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:262)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:107)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:102)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:137)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:122)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:768)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
For reference :- I found a similar scenario https://www.baeldung.com/spring-boot-unable-to-find-springbootconfiguration-with-datajpatest#missing-springbootconfiguration , but same error is coming when configuring similarly.
Upvotes: 0
Views: 634
Reputation: 1
I'm guessing you annotated your junit test class with @RunWith(SpringRunner.class)
.
That initializes the Spring context but does not configure and add any beans to it. So if you will try to @Authowire
or use any other Spring functionality, it will fail.
You have two options here:
MyService
) by adding @ContextConfiguration(classes = MyService.class)
or @SpringBootTest(classes=MyService.class)
or by providing test configuration as described here, for example: https://www.baeldung.com/spring-boot-testing@RunWith(SpringRunner.class)
annotation and create instance of the class you will be testing manually, via new
. But this is not preferable options, it will not autocreate mocks etc...Upvotes: -1