Article From:https://www.cnblogs.com/coder-lzh/p/9060110.html

Spring”

Need to introduce dependency

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <!-- When the development is introduced, the package --> will not be loaded when it is released.< scope> test< /scope>< /dependency>< dependEncy>< groupId> org.springframework< /groupId>< artifactId> spriNg-test< /artifactId>< version> 4.0.2.RELEASE< /version>< scope≫ test< /scope>< /dependency>

View Code

You can also write the base class, then the concrete business class inherit, you can also directly. Here I will standardize, first write the base class.

BaseTest.javaAs follows,@Before and @After annotations are provided by JUnit, meaning is written in the notes of the code:

write specific test classes

After writing the test base class, you can start writing the real unit test. Here is a simple example:

We can see that a method of unit testing can be run directly in IDEA, without compiling a project and starting a server to achieve functional testing of the business code, and the convenience of it.

 

SpringBoot”

SpringBootJunit and SpringMVC are basically similar, and only need to change some configuration.

Add dependency

 

write the test base class

 

Leave a Reply

Your email address will not be published. Required fields are marked *