Category:maven
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:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
public class BaseTest {
    @Before
    public void init() {
        //The business code before running the test
    }
    @After
    public void after() {
        //The business code after the test is completed
    }
}

View Code

write specific test classes

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

public class HelloTest extends BaseTest {

    @Test
    public void getTicketInfo() {
        System.out.println("hello");
}

View Code

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <version>1.5.2.RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.7.RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

View Code

 

write the test base class

@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {
    @Before
    public void init() {
        
    }
    
    @After
    public void after() {
        
    }
}

View Code

 

Leave a Reply

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