Spring、Spring Boot和TestNG测试指南 ( 2 )

发表于:2017-12-08 10:30

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:chanjarster    来源:ImportNew

  引言
  本项目所有的项目均采用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即可,源代码见 FooServiceImpltest :
@SpringBootTest(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);
}
}
  例子2:使用内嵌@Configuration加载Bean
  源代码见 FooServiceImpltest :
@SpringBootTest
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加载Bean
Config :
@Configuration
@Import(FooServiceImpl.class)
public class Config {
}
FooServiceImpltest :
@SpringBootTest(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);
}
}
  这个例子和例子2差不多,只不过将@Configuration放到了外部。
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号