Java study notes-problems encountered by TestNG + Spring

In the unit test, the bean needs to be injected to test, but a null pointer java.lang.NullPointerException is reported.

This is because the class does not inherit the AbstractTestNGSpringContextTests class, resulting in no ability to inject instances.

The role of the AbstractTestNGSpringContextTests class: the test class has the ability to inject instances only if it inherits this class.

@SpringBootTest
@Slf4j
public class TransferTest extends AbstractTestNGSpringContextTests {
    @Autowired
    public StudentService studentService;
    
    @Test
    public void testStudent(){
        log.info("测试用例开始执行");
        studentService.play();
        Student student=new Student();
        student.setName("小周");
        student.setAge(12);
        System.out.println(student.getAge());
    }
}

 

Then it was executed, but the new problem came again

The problem is that the bean of StudentService has not been assembled successfully. I don’t say anything, and the annotations are added. What is the reason? Baidu.

Found the following sentence, carefully understand it (drawing key points, must be tested!)

According to the above, it is easy to solve, either put the service and dao under the scannable package, or add the scope of the scan package after the @SpringBootApplication annotation, such as the following (ignoring the naming of the package name)

@SpringBootApplication(scanBasePackages = {"com.test.dto"})

Execute again and it works perfectly.

 

@SpringBootTest

Under normal circumstances, after using @SpringBootTest, Spring will load all managed beans, which is basically equivalent to starting the entire service, and you can start functional testing at this time.

Include all the beans imported in the SpringBoot main class. The beans in the SpringBoot main class are the beans in the @SpringBootApplication class you marked

 

@MybatisTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

Use @MybatisTest, if you do not add the annotation @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE), then mybatis uses an in-memory database, not a real tddl database, and will report non-existent errors

In fact, as mentioned above, if you use @SpringBootTest, the entire service will be started, which is too time-consuming. What if we just want to test a certain class? Then use @MybatisTest

 

 

@ContextConfiguration

If this annotation has a class file in (), the bean with the class file will be scanned first, otherwise the @Configuration class will be scanned, and @SpringBootApplication will be scanned last.

@ContextConfigurationThis annotation is usually @RunWith(SpringJUnit4ClassRunner.class)used in conjunction with the test

When a class is annotated @Component, it will automatically become a bean, and there is no need for the configuration shown in the Spring configuration file. There are usually two ways to collect these beans, the Java way and the XML way. After these beans are collected, when we want to use @Autowiredannotations to introduce these collected beans in a certain test class , we only need to add @ContextConfigurationannotations to this test class to mark certain beans that we want to import this test class.

You can refer to this link: https://www.cnblogs.com/bihanghang/p/10023759.html

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/112287894
Recommended