如何在 Junit5 中使用 Serenity?
How to use Serenity with Junit5?
我正在尝试 运行 在我的 maven 项目中进行 Serenity 测试。我正在使用 junit-jupiter-engine 5.3.1。 (它在 parent pom 中。)
我收到多个错误。
是JUnit的版本问题还是其他原因?
我该如何解决?
我的测试class:
@RunWith(SerenityRunner.class)
public class MyTestClass {
private static final String CHARACTER_ENCODING_UTF_8 = "UTF-8";
@Steps
private ApiSteps apiSteps;
@Test
public void aTest() {
try {
String filePath = "initiatepayment/input.xml";
Client client = Client.create();
WebResource webResource = client
.resource("https://jsonplaceholder.typicode.com/posts/42");
// String input = new Scanner(MyTestClass.class.getClassLoader().getResourceAsStream(filePath), CHARACTER_ENCODING_UTF_8).useDelimiter("\A").next();
String input = "{\"address\": \"Budapest\"}";
ClientResponse response = webResource.type("application/json")
.get(ClientResponse.class);
System.out.println(response.getStatus());
System.out.println("Output from Server .... \n");
String output = response.getEntity(String.class);
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void bTest() {
apiSteps.sendGetRequest("https://jsonplaceholder.typicode.com/posts/42", WebResourceType.APPLICATION_XML);
apiSteps.checkStatus(200);
}
}
我的 child pom.xml 的依赖项:
<properties>
<serenity-junit.version>1.8.21</serenity-junit.version>
<serenity.version>1.35.0</serenity.version>
<jersey-client.version>1.19.4</jersey-client.version>
<jersey-common.version>2.22.2</jersey-common.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
<!--Jersey-->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey-client.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey-common.version}</version>
<scope>test</scope>
</dependency>
<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${version.junit-platform-launcher}</version>
<scope>test</scope>
</dependency>
<!--Serenity-->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>${serenity-junit.version}</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-cucumber</artifactId>
<version>${serenity.version}</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-jbehave</artifactId>
<version>${serenity.version}</version>
</dependency>
</dependencies>
我的 ApiSteps class(它扩展了空的 BaseSteps class,但扩展了 net.thucydides.core.steps.ScenarioSteps class):
public class ApiSteps extends BaseSteps {
private static final String CHARACTER_ENCODING_UTF_8 = "UTF-8";
private static String message;
private static ClientResponse clientResponse;
@Step
public void getMessageByPath(String filePath) {
message = new Scanner(ApiSteps.class.getClassLoader().getResourceAsStream(filePath), CHARACTER_ENCODING_UTF_8).useDelimiter("\A").next();
}
@Step
public void sendGetRequest(String url, WebResourceType type) {
Client client = Client.create();
WebResource webResource = client.resource(url);
clientResponse = webResource.type(type.getValue()).get(ClientResponse.class);
}
@Step
public void sendPostRequest(String url, WebResourceType type) {
Client client = Client.create();
WebResource webResource = client.resource(url);
clientResponse = webResource.type(type.getValue()).post(ClientResponse.class, message);
}
@Step
public void checkStatus(int expectedStatusCode) {
Assert.assertEquals(clientResponse.getStatus(), expectedStatusCode);
}
}
我的堆栈跟踪:
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at net.serenitybdd.junit.runners.SerenityRunner.<init>(SerenityRunner.java:160)
at net.serenitybdd.junit.runners.SerenityRunner.<init>(SerenityRunner.java:116)
at net.serenitybdd.junit.runners.SerenityRunner.<init>(SerenityRunner.java:100)
....
java.lang.NullPointerException
at com.khb.openapi.payment.web.bdd.MyTestClass.bTest(MyTestClass.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
....
谢谢!
不幸的是,基本的 JUnit 5 不支持 Serenity,所以我只能使用 JUnit 5 Vintage。它可以 运行 JUnit 4 测试。现在可以使用了。
我正在尝试 运行 在我的 maven 项目中进行 Serenity 测试。我正在使用 junit-jupiter-engine 5.3.1。 (它在 parent pom 中。)
我收到多个错误。
是JUnit的版本问题还是其他原因? 我该如何解决?
我的测试class:
@RunWith(SerenityRunner.class)
public class MyTestClass {
private static final String CHARACTER_ENCODING_UTF_8 = "UTF-8";
@Steps
private ApiSteps apiSteps;
@Test
public void aTest() {
try {
String filePath = "initiatepayment/input.xml";
Client client = Client.create();
WebResource webResource = client
.resource("https://jsonplaceholder.typicode.com/posts/42");
// String input = new Scanner(MyTestClass.class.getClassLoader().getResourceAsStream(filePath), CHARACTER_ENCODING_UTF_8).useDelimiter("\A").next();
String input = "{\"address\": \"Budapest\"}";
ClientResponse response = webResource.type("application/json")
.get(ClientResponse.class);
System.out.println(response.getStatus());
System.out.println("Output from Server .... \n");
String output = response.getEntity(String.class);
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void bTest() {
apiSteps.sendGetRequest("https://jsonplaceholder.typicode.com/posts/42", WebResourceType.APPLICATION_XML);
apiSteps.checkStatus(200);
}
}
我的 child pom.xml 的依赖项:
<properties>
<serenity-junit.version>1.8.21</serenity-junit.version>
<serenity.version>1.35.0</serenity.version>
<jersey-client.version>1.19.4</jersey-client.version>
<jersey-common.version>2.22.2</jersey-common.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
<!--Jersey-->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey-client.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey-common.version}</version>
<scope>test</scope>
</dependency>
<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${version.junit-platform-launcher}</version>
<scope>test</scope>
</dependency>
<!--Serenity-->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>${serenity-junit.version}</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-cucumber</artifactId>
<version>${serenity.version}</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-jbehave</artifactId>
<version>${serenity.version}</version>
</dependency>
</dependencies>
我的 ApiSteps class(它扩展了空的 BaseSteps class,但扩展了 net.thucydides.core.steps.ScenarioSteps class):
public class ApiSteps extends BaseSteps {
private static final String CHARACTER_ENCODING_UTF_8 = "UTF-8";
private static String message;
private static ClientResponse clientResponse;
@Step
public void getMessageByPath(String filePath) {
message = new Scanner(ApiSteps.class.getClassLoader().getResourceAsStream(filePath), CHARACTER_ENCODING_UTF_8).useDelimiter("\A").next();
}
@Step
public void sendGetRequest(String url, WebResourceType type) {
Client client = Client.create();
WebResource webResource = client.resource(url);
clientResponse = webResource.type(type.getValue()).get(ClientResponse.class);
}
@Step
public void sendPostRequest(String url, WebResourceType type) {
Client client = Client.create();
WebResource webResource = client.resource(url);
clientResponse = webResource.type(type.getValue()).post(ClientResponse.class, message);
}
@Step
public void checkStatus(int expectedStatusCode) {
Assert.assertEquals(clientResponse.getStatus(), expectedStatusCode);
}
}
我的堆栈跟踪:
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at net.serenitybdd.junit.runners.SerenityRunner.<init>(SerenityRunner.java:160)
at net.serenitybdd.junit.runners.SerenityRunner.<init>(SerenityRunner.java:116)
at net.serenitybdd.junit.runners.SerenityRunner.<init>(SerenityRunner.java:100)
....
java.lang.NullPointerException
at com.khb.openapi.payment.web.bdd.MyTestClass.bTest(MyTestClass.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
....
谢谢!
不幸的是,基本的 JUnit 5 不支持 Serenity,所以我只能使用 JUnit 5 Vintage。它可以 运行 JUnit 4 测试。现在可以使用了。