3 Star 6 Fork 5

jarchan / spring-test-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
chapter_1_s1_testng.md 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
jarchan 提交于 2017-08-24 16:53 . refactor

Chapter 1: 基本用法 - 认识TestNG

先认识一下TestNG,这里有一个FooServiceImpl,里面有两个方法,一个是给计数器+1,一个是获取当前计数器的值:

@Component
public class FooServiceImpl implements FooService {

  private int count = 0;

  @Override
  public void plusCount() {
    this.count++;
  }

  @Override
  public int getCount() {
    return count;
  }

}

然后我们针对它有一个FooServiceImplTest作为UT:

public class FooServiceImplTest {

  @Test
  public void testPlusCount() {
    FooService foo = new FooServiceImpl();
    assertEquals(foo.getCount(), 0);

    foo.plusCount();
    assertEquals(foo.getCount(), 1);
  }

}

注意看代码里的assertEquals(...),我们利用它来判断Foo.getCount方法是否按照预期执行。所以,所谓的测试其实就是给定输入、执行一些方法,assert结果是否符合预期的过程。

参考文档

Java
1
https://gitee.com/chanjarster/spring-test-examples.git
git@gitee.com:chanjarster/spring-test-examples.git
chanjarster
spring-test-examples
spring-test-examples
master

搜索帮助