5

I am currently running into some problems with spring boot and multi maven project structure. I am using Spring Boot 4.3.1.

My project structure looks as follows:

parent
-- pom.xml
-- application
   -- pom.xml
   -- src
      -- main
         -- java
            -- Application.java (annotated with @SpringBootApplication)
      -- test
         -- java 
            -- MyApplicationTest.java (annotated with @SpringBootTest)
-- library
   -- pom.xml
   -- src
      -- main
         -- java (...)
      -- test
         -- java
            -- MyLibraryTest.java (annotated with @SpringBootTest)

application module has a dependency on library.

MyApplicationTest works perfectly fine, but running MyLibraryTest instead, I fail with the following error:

    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:392)
    at  org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOr FindConfigurationClasses(SpringBootTestContextBootstrapper.java:173)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:133)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:305)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:78)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)

My first guess is that library needs a dependency on application, but this causes a cycle.

Is there any solution to that problem? How can I structure my application correctly?

thanks a lot for suggestions.

MyLibraryTest looks as follow:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Transactional
    public class MyLibraryTest {
       @Autowired
       private MyService service;

       @Test
       public void testMyService_Save() {...}

    }
user3278695
  • 113
  • 3
  • 10
  • do share the code of `MyLibraryTest` where you find the error. Also, make sure it has all required dependencies defined in its pom.(as already defined in the app -> pom.xml ) for `@SpringBootTest` – Naman Mar 05 '17 at 10:05
  • http://stackoverflow.com/questions/39084491/unable-to-find-a-springbootconfiguration-when-doing-a-jpatest shall help in that case. Could possibly be a duplicate of this itself. – Naman Mar 05 '17 at 11:07

1 Answers1

0

You need to make sure that the library module's pom.xml includes -

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>

which is required for the module to use the @SpringBootTest annotation. You might already be using the same in your app module but is not included in the library module.


Well post the Edit, found the question to be possibly a duplicate of Unable to find a @SpringBootConfiguration when doing a JpaTest

Also quoting from Thomas's answer from the same thread, here

The thing about @DataJpaTest and a few other annotations is that they look for a @SpringBootConfiguration annotation in the current package, and if they cannot find it there, they traverse the package hierarchy until they find it.

For example, if the fully qualified name for your test class was com.example.test.JpaTest and the one for your application was com.example.Application, then your test class would be able to find the @SpringBootApplication (and therein, the @SpringBootConfiguration).

If the application resided in a different branch of the package hierarchy, however, like com.example.application.Application, it would not find it.

which seems to be the case for you, where you are trying to test an application in a different module itself. Hence the error that you see.

Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353