spring boot项目做junit

2018-09-10  本文已影响0人  Steven_sunlu

1、配置pom.xml文件

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

2、在test文件下建立test包


image.png

3、创建父test类

// SpringJUnit支持,由此引入Spring-Test框架支持!
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
//由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration
public class AppTests {

    @Before
    public void init(){
        System.out.println("开始测试-------------------------------");
    }

    @After
    public void after(){
        System.out.println("测试结束-------------------------------");
    }

}

4、建子测试类基层父测试类即可进行测试

public class WhiteIpTest extends AppTests {

    @Autowired
    private WhiteIpService whiteIpService;

    @Test
    public void insert(){
        WhiteIp whiteIp = new WhiteIp();
        whiteIp.setIp("192.168.0.1");
        whiteIp.setWname("本地地址");
        whiteIpService.addWhiteIp(whiteIp);
    }

  @Test
    public void list(){
        Page<WhiteIp> whiteIps = whiteIpService.findByPage(1,2);
        for (WhiteIp w: whiteIps) {
            System.out.println(w.getId());
        }
        System.out.println(whiteIps.getTotal()+"; "+whiteIps.getPages());
    }

上一篇 下一篇

猜你喜欢

热点阅读