简体   繁体   中英

InvalidTestClassError: Invalid test class. How to test spring boot with maven and @BeforeAll/Class initializator

I have the gradle project where I successfully use tests and try to implement the same solution with maven, but I totally can't execute tests there.

Main problem in that I need to use @BeforeAll/Class annotation and init NOT static method. In the Gradle this works so:

  • build.gradle
dependencies {
    testImplementation "org.springframework.boot:spring-boot-starter-test:${spring_boot_version}"
    implementation 'junit:junit:4.13.1'
    ....
}

tasks.named('test') {
    useJUnitPlatform()
}

test classes:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {DeardiaryApplication.class})
@WebAppConfiguration
public abstract class AbstractTest {
    protected MockMvc mvc;
    private ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    WebApplicationContext webApplicationContext;

    protected void setUp() {
        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }
...
}

import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DatabaseTest extends AbstractTest {

    @Autowired
    private ExerciseInterface exerciseService;

    @BeforeAll
    public void init() {
        setUp();
        exerciseDto = exerciseService.getByExerciseType(ExerciseTypeEnum.SNATCH).get(0);
    }

    @Test
....

But in the maven project I received InvalidTestClassError: Invalid test class . When I use junit.Test instead junit.jupiter.api.Test, the @BeforeAll/Class method nod called, or I received error BeforeAll must be static , etc...

  • pom.xml:
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M8</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.9.2</version>
                        <scope>test</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

This is all difference - classes implemented in the same way

If you don't want to use the static method for BeforeAll, you need to use the annotation TestInstance(Lifecycle.PER_CLASS)

By default the Junit uses PER_METHOD lifecycle, thus the BeforeAll requires it to be static to ensure it is not instantiated with every method.

The TestInstance(Lifecycle.PER_CLASS) annotation ensures that the lifecycle of the test is per class thus removing the requirement of static BeforeAll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM