When I start the spring boot application, I am using this code to set the application to ignore tenant:
CommonContext.setTenantIgnore(true);
this works fine. Then I write a JUnit test case, I also need to ignore the tenant by using CommonContext.setIgnoreTenant(true)
, I have tried like this:
@BeforeAll
public static void before() {
CommonContextHolder.setTenantIgnore(true);
}
@BeforeEach
void init() {
CommonContextHolder.setTenantIgnore(true);
}
@Before
public void setup() {
CommonContextHolder.setTenantIgnore(true);
}
also tried like this:
@SpringJUnitConfig
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class TenantIgnore {
@BeforeAll
public static void setUp() {
CommonContextHolder.setTenantIgnore(true);
}
}
also did not work. what should I do to set tenant ignore before start all unit test? I think it should be before the JUnit application start(or before the spring container start), before all test class. I also tried the extension:
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
public class YourExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private static boolean started = false;
@Override
public void beforeAll(ExtensionContext context) {
CommonContextHolder.setTenantIgnore(true);
}
@Override
public void close() {
}
}
The basic problem I see is, that you use CommonContextHolder and CommonContext in a static context and Spring is not designed to "deal" with static contexts... simply said.
You were writing about a Spring context which also does not exist in a unit test (here junit5).
If you want to take advantage of the Spring context in a test case you should invoke the test with @SpringBootTest. This will also load all your Spring annotated Beans into the test context. Only then you are able to access these classes in your test setup methods after injection with @Autowired. But they can't be static. Please avoid static classes in a Spring Boot application... for simplicity.
If you want to have the tenant flag editable during runtime, test or production, you need to create an instance of that class. Spring Boot will do this for you. Simply add @Component annotation on class level. Add @SpringbootTest annotation to your test, remove other annotations.
You can get and set tenant context in setup methods and runtime. Provide Getters and Setters of course.
Another way of having the context available and usable in a unit test environment is to mock it, for example with Mockito.
You have to know that unit test are made for testing only simple, small and dedicated parts of code. Spring context usually does not belong to a unit test context. So you have to mock it for that.