Tag Archives: JUnit tests mybatis-plus error

JUnit tests mybatis-plus error: null pointer [How to Solve]

Question:

Because the project uses microservices, a service is created in the microservices to write mybatis plus in order to save trouble. However, a null pointer error occurs when JUnit tests after writing according to the official website documents.

Test class of official website document:

An error occurred:

The value of usermapper was found to be empty

By checking the data, it is found that this is because ordinary classes can’t use springbeans, so ordinary test classes can’t get beans, so they report null pointers. Therefore, we need to make the test class get the bean.

Solution:

Add test dependency:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

And add notes on the test class:

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)

solve the problem