1.需要在pom.xml中引入spring-boot-starter-test
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
2.单元测试代码
两个关键的注解
@RunWith(SpringRunner.class)
@SpringBootTest
package com.niugang.test; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.niugang.entity.User; import com.niugang.service.UserService; @RunWith(SpringRunner.class) @SpringBootTest public class TestUser { @Resource private UserService userService; @Test public void test1(){ List<User> list = userService.queryList(null); System.out.println("集合大小:"+list.size()); } }
注意不能不建包,直接写测试类
微信公众号