I know there are many posts related to JUnit5 env setting, but I am not sure why I still get the null injection. My unittest is for bellow to be referred to.
import com.example.demo.service.processing.Task;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; // the right one is used.
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import static org.junit.jupiter.api.Assertions.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class) //in JUnit5, these 2 are required.
@SpringBootTest
public class ProcessorUT {
// some works, settings,
@Autowired
private static TransactionServiceImpl service;
@BeforeAll
public static void initBeforeAll(){
//here the service is null
}
@BeforeEach
public void initBeforeEach() {
//service is null
}
@Test
public void test_initProcessor(){
//service is null
}
}
the maven dependency is from spring initializer,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
I also tried dependency bellow, but everything is the same. I think I am in the JUnit5 env based on spring-boot-starter-test with JUnit 5 . So what may be the issue? Any info is appreciated.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.4</version>
<scope>test</scope>
</dependency>