0

I'm struggling with JUnit 5 when moving the @SpringBootApplication to a different package.

I have setup a new SpringBoot-project (2.2.1.RELEASE) with Maven and Eclipse (had to upgrade this from "Eclipse Photon" to support the SpringBoot-Release

My package layout looks like this:

/src/main/java
   com.package.sample.appl1
     StartSamples.java
   com.package.sample.appl1.start
   com.package.sample.appl1.dbaccess
   com.package.sample.appl1.run
   com.package.sample.appl1.utils
   com.package.sample.appl2.run
   com.package.sample.appl2.run

/src/test/java
   com.package.sample.appl1.dbaccess
     SimpleTest.java

The class holding the @SpringBootApplication is:

@ComponentScan({
    "com.package.sample"
    })
@SpringBootApplication
public class StartSamples {
   public static void main(String[] args) {
        System.out.println("Start");
        try {
            SpringApplication.run(StartSamples.class, args);
        } catch (Exception e) {
            LOGGER.error("", e);
            System.exit(-1);
        }
    }

And the test is this:

import static org.junit.Assert.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit.jupiter.SpringExtension;

/**
 * Test the Query-statements and the DAO methods
 * 
 * @author U005078
 *
 */
@SpringBootTest
@ExtendWith(SpringExtension.class)
@ComponentScan({
    "com.package.sample"})
@EnableAutoConfiguration
public class SimpleTest {

    @SuppressWarnings("unused")
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTest.class);

    @Test
    @DisplayName("SimpleTest")
    public void testTotalRows() {

With this configuration all is fine, "StartSamples" works as expected and aqlso the SimpleTest.

But when moving "StartSamples" to a different package (e.g. "com.package.sample.start" would make more sense to me - "StartSamples" is still ok but "SimpleTest" does not fail nor succeed - test seems not to become executed. I see a message: class path resource [com/package/sapmle/appl1/dbaccess/SimpleTest-context.xml] does not exist class path resource [com/package/sapmle/appl1/dbaccess/SimpleTestContext.groovy] does not exist .SimpleTest]: SimpleTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.

I also found: Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.package.sample.appl1.dbaccess.SimpleTest], using SpringBootContextLoader

So I defined the @ContextConfiguration to the "SimpleTest", then it worked. But I do not understand at all why the move of the @SpringBootApplication did change this behaviour.

With another try of setting up this project I ended up with "No tests found with test runner 'JUnit 5'" and could also not find any reason. I started over again with the current approach and get to here. And do do nat any clue what gives me the error - for either of the problems.

Any explanation witld be appreciated. I tried for lots of hours now to find something in the internet - but I only found recommendations like "try this", "try that" but no help in understanding. So any help is appreciated.

Ulrich
  • 715
  • 2
  • 7
  • 25
  • The section will look up the package structure but not in sub packages. As it is in a sub package it isn't detected. Also it is actually suggested to put the `@SpringBootApplication` class in a top-level and not in a sub package. If you do, you will eventually will end-up with manually configuring things again. – M. Deinum Nov 17 '19 at 11:10

1 Answers1

0

Define your SpringBoot Main class like below

@SpringBootTest(classes = {StartSamples.class})
public class SimpleTest {
   ...
}
Georgios
  • 4,764
  • 35
  • 48
sai
  • 111
  • 1
  • 3
  • This works, thanks a lot. But di you have an explanation for the behaviour? It came to my mind, that the package might be searched to late (sequence error) - but seems als to be sort of strange. – Ulrich Nov 16 '19 at 12:20