Web集成测试允许对Spring Boot应用程序进行集成测试,而无需进行任何模拟。 通过使用@WebIntegrationTest@SpringApplicationConfiguration我们可以创建加载应用程序并在普通端口上侦听的测试。 Spring Boot的这一小增加使使用Selenium WebDriver创建集成测试变得更加容易。

测试依赖

我们将要测试的应用程序是一个简单的Spring Boot / Thymeleaf应用程序,具有spring-boot-starter-webspring-boot-starter-thymeleafspring-boot-starter-actuator依赖性。 请参阅参考资料以获取GitHub项目的链接。

测试依赖项为:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
<dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>1.5.0</version><scope>test</scope>
</dependency>
<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>2.45.0</version><scope>test</scope>
</dependency>

网络集成测试

在经典的Spring Test中,使用MockMvc可以创建如下的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class HomeControllerClassicTest {@Autowiredprivate WebApplicationContext wac;private MockMvc mockMvc;@Beforepublic void setUp() throws Exception {mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();}@Testpublic void verifiesHomePageLoads() throws Exception {mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isOk());}
}

@SpringApplicationConfiguration扩展了@ContextConfiguration功能,并加载应用程序上下文以进行集成测试。 要创建没有@WebIntegrationTest环境的测试,我们应该使用@WebIntegrationTest批注定义测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest(value = "server.port=9000")
public class HomeControllerTest {}

这将在JUnit测试中启动完整的应用程序,监听端口9000 。 有了这样的测试,我们可以轻松地使用浏览器添加Selenium并执行实际的功能测试(除非使用HtmlUnit驱动程序,否则在无头环境中将无法工作–但这不在本文的讨论范围之内)。

添加硒

将Selenium添加到测试中非常简单,但是我想实现的目标还不止于此,因此我创建了一个自定义批注,将我的测试标记为Selenium测试。 我还以允许将WebDriver注入测试实例的方式配置了它:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest(value = "server.port=9000")
@SeleniumTest(driver = ChromeDriver.class, baseUrl = "http://localhost:9000")
public class HomeControllerTest {@Autowiredprivate WebDriver driver;}

@SeleniumTest是一个自定义注释:

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@TestExecutionListeners(listeners = SeleniumTestExecutionListener.class,mergeMode = MERGE_WITH_DEFAULTS)
public @interface SeleniumTest {Class<? extends WebDriver> driver() default FirefoxDriver.class;String baseUrl() default "http://localhost:8080";
}

注释使用添加了测试执行侦听器,该侦听器将创建可在集成测试中使用的WebDriver实例。 TestExecutionListener定义了一个侦听器API,用于对测试执行事件做出反应。 它可以用于测试。 例如,Spring Test中的示例实现用于支持测试管理的事务或将依赖项注入到测试实例中。

TestExecutionListener

注意:为了更好的可读性, SeleniumTestExecutionListener的代码的某些部分被跳过。

SeleniumTestExecutionListener提供了将配置的WebDriver注入测试实例的方法。 该驱动程序实例仅创建一次,并且可以使用@SeleniumTest批注简单地更改所使用的驱动程序。 最重要的是在Bean Factory中注册驱动程序。

@Override
public void prepareTestInstance(TestContext testContext) throws Exception {ApplicationContext context = testContext.getApplicationContext();if (context instanceof ConfigurableApplicationContext) {SeleniumTest annotation = findAnnotation(testContext.getTestClass(), SeleniumTest.class);webDriver = BeanUtils.instantiate(annotation.driver());// register the bean with bean factory}
}

在使用WebDriver打开应用程序的每个测试方法的基本URL之前,请执行以下操作:

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {SeleniumTest annotation = findAnnotation(testContext.getTestClass(), SeleniumTest.class);webDriver.get(annotation.baseUrl());}

另外,在每次失败时都会生成一个屏幕截图:

@Override
public void afterTestMethod(TestContext testContext) throws Exception {if (testContext.getTestException() == null) {return;}File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);// do stuff with the screenshot}

每次测试后,驱动程序将关闭:

@Override
public void afterTestClass(TestContext testContext) throws Exception {if (webDriver != null) {webDriver.quit();}
}

这只是一个例子。 实现非常简单。 我们可以扩展注释和侦听器的功能。

考试

运行以下测试将启动Chrome浏览器并使用Selenium执行一些简单的检查:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest(value = "server.port=9000")
@SeleniumTest(driver = ChromeDriver.class, baseUrl = "http://localhost:9000")
public class HomeControllerTest {@Autowiredprivate WebDriver driver;private HomePage homePage;@Beforepublic void setUp() throws Exception {homePage = PageFactory.initElements(driver, HomePage.class);}@Testpublic void containsActuatorLinks() {homePage.assertThat().hasActuatorLink("autoconfig", "beans", "configprops", "dump", "env", "health", "info", "metrics", "mappings", "trace").hasNoActuatorLink("shutdown");}@Testpublic void failingTest() {homePage.assertThat().hasNoActuatorLink("autoconfig");}
}

该测试使用带有自定义AssertJ断言的简单页面对象。 您可以在GitHub中找到完整的源代码。 请参阅参考资料。

如果发生故障,驱动程序拍摄的屏幕快照将存储在适当的目录中。

摘要

@WebIntegrationTest@SpringApplicationConfiguration批注,可以在常规JUnit测试中对完全加载的Spring Boot应用程序进行集成测试。 使应用程序在测试中运行将为您提供使用Selenium并使用浏览器运行功能测试的可能性。 如果将其与Profile和Spring Test的其他功能(例如@Sql@SqlConfig@Sql@SqlConfig可能会为集成测试提供功能强大而简单的解决方案。

参考文献

  • 源代码: https : //github.com/kolorobot/spring-boot-thymeleaf
  • Spring Boot测试: http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-testing
  • Spring测试: http : //docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html

翻译自: https://www.javacodegeeks.com/2015/03/spring-boot-integration-testing-with-selenium.html

使用Selenium进行Spring Boot集成测试相关推荐

  1. selenium持续集成_使用Selenium进行Spring Boot集成测试

    selenium持续集成 Web集成测试允许对Spring Boot应用程序进行集成测试,而无需进行任何模拟. 通过使用@WebIntegrationTest和@SpringApplicationCo ...

  2. Spring Boot集成测试中@ContextConfiguration和@SpringApplicationConfiguration之间的区别

    即使同时使用@ContextConfiguration和@SpringApplicationConfiguration批注以及SpringJUnit4ClassRunner来指定如何加载Spring应 ...

  3. 一个twitch 直播地址搜索爬虫基于java selenium 和 spring boot

    1.运行环境 selenium 首先要下载chromedriver,chromedriver与google浏览器对应的版本如下表所示: 驱动版本 浏览器对应的版本 ChromeDriver v2.43 ...

  4. 不要在 Spring Boot 集成测试中使用 @Transactional

    在测试运行时,测试类中 @Transactional 注解,会导致测试中 Entity 数据的操作都是在内存中完成,最终并不会进行 commit 操作,也就是不会将 Entity 数据进行持久化操作, ...

  5. Spring Boot 集成测试

    一.测试一般程序 1.1 测试步骤 在pom.xml 中加入测试环境的依赖. 在测试类上加入@RunWith(SpringRunner.class) 与@SpringBootTest 注解. 1.2 ...

  6. boot jersey_Jersey和Spring Boot入门

    boot jersey 除了许多新功能,Spring Boot 1.2还带来了Jersey支持. 这是吸引喜欢标准方法的开发人员的重要一步,因为他们现在可以使用JAX-RS规范构建RESTful AP ...

  7. 使用Testcontainers和PostgreSQL,MySQL或MariaDB的Spring Boot测试

    Testcontainers是一个Java库,可轻松将Docker容器集成到JUnit测试中. 在Containerized World中 ,将测试配置与嵌入式数据库和服务复杂化几乎没有意义. 而是使 ...

  8. Jersey和Spring Boot入门

    除了许多新功能,Spring Boot 1.2还带来了Jersey支持. 这是吸引喜欢标准方法的开发人员的重要一步,因为他们现在可以使用JAX-RS规范构建RESTful API,并将其轻松部署到To ...

  9. Spring Boot 的单元测试和集成测试

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 万想 来源 | 公众号「锅外的大佬」 学习如何使用本教程中提供的工具,并在 Spr ...

最新文章

  1. Science:英国Castrillo组揭示微生物群与根内皮的协调支持植物营养平衡!
  2. java性能分析 linux,linux 系统性能分析
  3. 使用TFHpple解析html
  4. centos8.2安装mysql_centos8安装mysql
  5. 自然语言处理中的预训练模型 —— 邱锡鹏老师的演讲记录
  6. oracle怎么删除存储,删除Oracle分区存储是一个怎样的过程?
  7. html 自动生产,【SQL】用Sql Server自动生产html格式的数据字典
  8. mysql 5.6 修改默认字符集_mysql5.6修改默认字符集
  9. 第3章 java的基本程序设计结构
  10. excel数据透视表应用大全_从Excel进阶到Python:更强大的数据透视表
  11. 软考计算机硬件工程师考试大纲,2016年软考信息安全工程师考试大纲
  12. DH 算法思想 SSH解决内容篡改问题
  13. 小技巧分享:电脑屏幕亮度怎么调?
  14. 泛微OA-测试机更改sysadmin密码为1
  15. 文件描述符 fd 究竟是什么?
  16. 计算机提示资源管理器停止,电脑开机黑屏并弹出Windows 资源管理器已停止工作该怎么办?...
  17. 不会吧?!新版本longhorn部署需要k8s.gcr.io镜像?
  18. ORACLE DG断档处理
  19. 淘宝直播小窗如何开启?怎么免费引流?
  20. 压缩图片大小怎么弄?这样压缩不改变清晰度

热门文章

  1. 3-1 Apache Shiro权限管理框架介绍
  2. python3.0什么时候发布的_Django 3.0 发布说明
  3. 阳泉2021高考成绩查询时间段,2021年阳泉高考成绩排名及成绩公布时间什么时候出来...
  4. python基础教程zip密码_python基础教程Python实现加密的RAR文件解压的方法(密码已知)...
  5. 使用maven聚合安装多个maven工程到本地仓库报错的解决方法:child module pom.xml does not exist
  6. MySQL、MongoDB、列数据库的区别及应用场景
  7. mybatis多表新增如何获取主键ID
  8. poi中文api文档
  9. 读入的字节都写入字节数组中_使用Java将文件读入字节数组的7个示例
  10. fluent design_Fluent Design单选按钮,复选框,选择框,Java菜单