简体   繁体   English

如何在Junit5中使用Serenity?

[英]How to use Serenity with Junit5?

I'm trying to run Serenity Tests in my maven project. 我正在尝试在我的Maven项目中运行宁静测试。 I'm using junit-jupiter-engine 5.3.1. 我正在使用junit-jupiter-engine 5.3.1。 (It's in the parent pom.) (在父pom中。)

I'm getting multiple error. 我遇到多个错误。

Is it because the JUnit's version or is there something else? 是因为JUnit的版本还是其他? How can I solve it? 我该如何解决?

My test 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);
}
}

My child pom.xml's dependencies: 我的孩子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>

My ApiSteps class (It extends BaseSteps class which is empty, but that extends the net.thucydides.core.steps.ScenarioSteps class): 我的ApiSteps类(它扩展了BaseSteps类,该类为空,但是扩展了net.thucydides.core.steps.ScenarioSteps类):

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);
}

}

And my stack trace: 和我的堆栈跟踪:

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)

....

Thank you! 谢谢!

Unfortunately Serenity is not supported by basic JUnit 5 so I had to use JUnit 5 Vintage only. 不幸的是,基本JUnit 5不支持Serenity,因此我只能使用JUnit 5 Vintage。 It can run JUnit 4 tests. 它可以运行JUnit 4测试。 It works now. 现在可以使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM