简体   繁体   中英

Spring Cloud Contract not executing @Before

I am writing tests using Spring Cloud Contract.
In my configuration I am using for test an in memory DB that is created and destroyed at each test run.
In order to test successfully I need to put some data in the DB but it seems like the classic @Before annotation used in jUnit does not work (the code in it is never executed).

import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.context.WebApplicationContext;


@ExtendWith(SpringExtension.class)
@SpringBootTest
public class BaseTestClass {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseTestClass.class);

    @Autowired
    EntityRepository entityRepository;

    @Autowired
    private WebApplicationContext context;

    @Before
    public void init() {
        // This code never runs
        LOGGER.info("Init started");
        MyEntity entity1 = new MyEntity();
        entityRepository.save(entity1);
    }

    @BeforeEach
    public void setup() {
        RestAssuredMockMvc.webAppContextSetup(context);
    }

}

What am I missing?

@Before belongs to JUnit4 and earlier while @BeforeEach belongs to JUnit5 as a replacement and clarification of @Before . If you are running with JUnit5, perhaps put all your initialization logic in @BeforeEach (or if it's a one-time init, in @BeforeAll )

I've found a solution that works.

I was adding the @TestInstance decorator incorrectly, here's my working solution:

import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.context.WebApplicationContext;


@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class BaseTestClass {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseTestClass.class);

    @Autowired
    EntityRepository entityRepository;

    @Autowired
    private WebApplicationContext context;

    @BeforeAll
    public void init() {
        LOGGER.info("Init started");
        MyEntity entity1 = new MyEntity();
        entityRepository.save(entity1);
    }

    @BeforeEach
    public void setup() {
        RestAssuredMockMvc.webAppContextSetup(context);
    }

}

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