设为首页 加入收藏

TOP

Spring、Spring Boot 和 TestNG 测试指南 ( 2 )(一)
2017-12-07 14:22:05 】 浏览:464
Tags:Spring Boot TestNG 测试 指南

引言

本项目所有的项目均采用Maven的标准目录结构:

  • src/main/java,程序java文件目录
  • src/main/resource,程序资源文件目录
  • src/test/java,测试代码目录
  • src/test/resources,测试资源文件目录

并且所有Maven项目都可以使用mvn clean test方式跑单元测试,特别需要注意,只有文件名是*Test.java才会被执行,一定要注意这一点哦。

认识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结果是否符合预期的过程。

使用Spring Testing工具

既然我们现在开发的是一个Spring项目,那么肯定会用到Spring Framework的各种特性,这些特性实在是太好用了,它能够大大提高我们的开发效率。那么自然而然,你会想在测试代码里也能够利用Spring Framework提供的特性,来提高测试代码的开发效率。这部分我们会讲如何使用Spring提供的测试工具来做测试。

例子1

源代码见FooServiceImplTest

@ContextConfiguration(classes = FooServiceImpl.class)
public class FooServiceImplTest extends AbstractTestNGSpringContextTests {

  @Autowired
  private FooService foo;

  @Test
  public void testPlusCount() throws Exception {
    assertEquals(foo.getCount(), 0);

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

}

在上面的源代码里我们要注意三点:

  1. 测试类继承了AbstractTestNGSpringContextTests,如果不这么做测试类是无法启动Spring容器的
  2. 使用了[@ContextConfiguration][javadoc-ContextConfiguration]来加载被测试的Bean:FooServiceImpl
  3. FooServiceImpl是@Component

以上三点缺一不可。

例子2

在这个例子里,我们将@Configuration作为nested static class放在测试类里,根据@ContextConfiguration的文档,它会在默认情况下查找测试类的nested static @Configuration class,用它来导入Bean。

源代码见FooServiceImplTest

@ContextConfiguration
public class FooServiceImplTest extends AbstractTestNGSpringContextTests {

  @Autowired
  private FooService foo;

  @Test
  public void testPlusCount() throws Exception {
    assertEquals(foo.getCount(), 0);

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

  @Configuration
  @Import(FooServiceImpl.class)
  static class Config {
  }

}

例子3

在这个例子里,我们将@Configuration放到外部,并让@ContextConfiguration去加载。

源代码见Config

@Configuration
@Import(FooServiceImpl.class)
public class Config {
}

FooServiceImplTest

@ContextConfiguration(classes = Config.class)
public class FooServiceImplTest extends AbstractTestNGSpringContextTests {

  @Autowired
  private FooService foo;

  @Test
  public void testPlusCount() throws Exception {
    assertEquals(foo.getCount(), 0);

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

}

需要注意的是,如果@Configuration是专供某个测试类使用的话,把它放到外部并不是一个好主意,因为它有可能会被@ComponentScan扫描到,从而产生一些奇怪的问题。

使用Spring Boot Testing工具

前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目。

在Spring Boot项目里既可以使用Spring Boot Testing工具,也可以使用Spring Testing工具。 在Spring项目里,一般使用Spring Testing工具,虽然理论上也可以使用Spring Boot Testing,不过因为Spring Boot Testing工具会引入Spring Boot的一些特性比如AutoConfiguration,这可能会给你的测试带来一些奇怪的问题,所以一般不推荐这样做。

例子1:直接加载Bean

使用Spring Boot Testing工具只需要将@ContextConfiguration改成@SpringBootTest即可,源代码见F

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇使用 Spock 框架进行单元测试 下一篇Spring、Spring Boot 和 TestNG ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目