How to use Serenity with Junit5?

半腔热情 提交于 2021-02-19 07:04:52

问题


I'm trying to run Serenity Tests in my maven project. I'm using junit-jupiter-engine 5.3.1. (It's in the parent pom.)

I'm getting multiple error.

Is it because the JUnit's version or is there something else? 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:

<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):

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!


回答1:


Unfortunately Serenity is not supported by basic JUnit 5 so I had to use JUnit 5 Vintage only. It can run JUnit 4 tests. It works now.



来源:https://stackoverflow.com/questions/53481559/how-to-use-serenity-with-junit5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!