实用Spring教程(3)----单元测试

继续上一节的内容

在test文件夹下文件目录如下:

test

    |- java

        |- com

            |- byron

                |- spring 

                    |- demo

                        |- service

                            ServiceTest.java


扫描二维码关注公众号,回复: 1805370 查看本文章

ServiceTest.java

package com.byron.spring.demo.service;

import com.byron.spring.demo.Application;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ServiceTest {

    @Autowired
    private SomeServiceI someService;

    @Test
    public void testSomething(){
        String result = someService.doSomething("byron");
        Assert.assertNotNull(result);
        Assert.assertEquals(result, "byron has finished");
    }

}

在 testSomething 的方法上右键,点击 【Run testSomething()】



然后你就会见到一条 红色,或者绿色的进度条产生。



如果是红色,说明有单元测试运行失败,你也可以直接找到错误的原因



单元测试有助于我们快速测试一些方法,而不需要将整个项目搭建起来,对开发非常有帮助。

猜你喜欢

转载自blog.csdn.net/dwdyoung/article/details/80860843