我创造了一个宁静+ JBehave +春季启动测试。
测试如下:
故事:
Meta:
Narrative:
As a new user, in order to efficiently use the service I first want to register to the service and then log in.
During the process I want to make sure that all validations are properly executed
Scenario: user registration
Given a user with email address 'test@test.com' and password 'aaa'
When attempt to register
Then system returns an information about password being too short
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestServer.class)
@WebAppConfiguration
@IntegrationTest({ "server.port=6666" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class UserRegistrationTest extends SerenityStory {
private String email;
private String password;
@Steps
private UserRegistrationSteps userRegistrationSteps;
@Given("a user with email address '$email' and password '$password'")
public void givenAUserWithEmailAndPassword(@Named("email") String email, @Named("password") String password) {
this.email = email;
this.password = password;
}
@When("attempt to register")
public void attemptToRegister() {
userRegistrationSteps.openRegistrationPage();
userRegistrationSteps.enterRegistrationCredentials(email, password);
}
@Then("system returns an information about password being too short")
public void thenSystemReturnsAnInformationAboutPasswordBeingTooShort() {
userRegistrationSteps.makeSureRegistrationErrorsContainErrorAboutShortPassword();
}
}
当我单独运行测试时,一切顺利。第一个Spring启动服务器启动,它会生成一些临时的内存数据库,然后才执行测试。但是,如果您将测试与另一个分开运行,则不会生成报告。为了生成报告,我需要执行
gradle test aggregate
不幸的是,当我这样做时,忘记了我有@RunWith(SpringJUnit4ClassRunner.class)注释。它尝试在尚未存在的服务器上运行测试,因此测试失败。
这里也是我的build.gradle
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("net.serenity-bdd:serenity-gradle-plugin:1.0.59")
}
}
group 'fi.achivi.server'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'net.serenity-bdd.aggregator'
jar {
baseName = 'achivi'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
gradle.startParameter.continueOnFailure = true
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-thymeleaf', // all spring MVC and spring boot in one dependency
'org.springframework.boot:spring-boot-starter-data-mongodb',
'commons-validator:commons-validator:1.4.1', // utils
'org.apache.commons:commons-lang3:3.4',
'org.springframework.security:spring-security-web:4.0.1.RELEASE', //spring security
'org.springframework.security:spring-security-config:4.0.1.RELEASE'
testCompile 'org.springframework.boot:spring-boot-starter-test',
'cz.jirutka.spring:embedmongo-spring:1.3.1', //mongodb dependencies
'de.flapdoodle.embed:de.flapdoodle.embed.mongo:1.48.0',
'net.serenity-bdd:core:1.0.47', // JBehave and serenity stuff
'net.serenity-bdd:serenity-junit:1.0.47',
'net.serenity-bdd:serenity-rest-assured:1.0.59',
'org.assertj:assertj-core:1.7.0',
'org.slf4j:slf4j-simple:1.7.7',
'net.serenity-bdd:serenity-jbehave:1.0.23'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
那么在生成报告时如何使@RunWith注释考虑到宁静,以便我的服务器实际生成?
答案 0 :(得分:2)
我认为您可能需要使用某种SerenityRunner而不是SpringJunit4TestRunner。然而,这有点困难。我自己一直在尝试这个,我所处的位置在下面。
对于某些历史记录,您可以从Serenity参考文档中的第16章开始。
虽然已过时,但我认为它主要适用,您可以使用更新的类,您可以在源代码中找到它们:
@Rule
private final SpringIntegrationMethodRule springIntegrationMethodRule = new SpringIntegrationMethodRule();
和
@ClassRule
private static final SpringIntegrationClassRule springIntegrationClassRule = new SpringIntegrationClassRule();
有关此功能的more up-to-date information中有些the commit可用于这些Serenity规则。
除此之外,如果你使用的是Spring 4.2.0或更高版本,那么Spring团队实际上已经发布了一些规则,用作SpringJUnit4ClassRunner的替代品。如果您使用最新版本的Spring,这可能是最佳选择。
以下是an excellent explanation如何使用Spring团队的新规则。
<强>更新强> 我实际上并没有设法使用上述信息 - 至少使用Serenity / Cucumber,所以我提出了Serenity项目的问题here。我还在stackoverflow上询问了这个follow on question。