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


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.


Add test dependency:


And add notes on the test class:


solve the problem

Read More: