Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EmptyStackException is thrown after sending Response using SerenityRest.rest() in BeforeClass step #984

Closed
MariuszSkomra opened this issue Oct 24, 2017 · 5 comments

Comments

@MariuszSkomra
Copy link

Hello

In my project I want to in @BeforeClass method send post request and get database id from response. Then I want to use this id in all tests in Test class.

To do that I'm sending following request:

        Response response = SerenityRest.rest()
                .given()
                .contentType(CONTENT_TYPE)
                .body(jsonAsMap)
                .post(requestPath);
        response.then().statusCode(200);

When I run Test class prepared in that way I receive following exception:

java.util.EmptyStackException
	at java.util.Stack.peek(Stack.java:102)
	at net.thucydides.core.steps.BaseStepListener.getCurrentStep(BaseStepListener.java:569)
	at net.thucydides.core.steps.BaseStepListener.addRestQuery(BaseStepListener.java:1006)
	at net.thucydides.core.steps.BaseStepListener.recordRestQuery(BaseStepListener.java:1001)
	at net.serenitybdd.rest.utils.RestReportingHelper.registerCall(RestReportingHelper.java:79)
	at net.serenitybdd.rest.decorators.request.RequestSpecificationDecorated.execute(RequestSpecificationDecorated.java:263)
	at net.serenitybdd.rest.decorators.request.RequestSpecificationDecorated.post(RequestSpecificationDecorated.java:74)
	at net.serenitybdd.rest.decorators.request.RequestSpecificationDecorated.post(RequestSpecificationDecorated.java:27)
	at utils.RestUtils.sendPostRequest(RestUtils.java:31)
	at features.EmptyStackExceptionTest.create_user(EmptyStackExceptionTest.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at net.serenitybdd.junit.runners.SerenityRunner.run(SerenityRunner.java:252)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

before response status code is checked, but request is executed successfully - prepared data are present in database.

I found source of the problem - is using SerenityRest.rest() method to create Response. Without it (by using only RestAssured):

        Response response = RestAssured.given()
                .contentType(CONTENT_TYPE)
                .body(jsonAsMap)
                .post(requestPath);
        response.then().statusCode(200);

whole code is working properly.

To reproduce this situation I prepare project (which is attached to this issue report) based on pom.xml from serenity-demos/junit-webtests/ project.
EmptyStackExceptionTest class contains code where exception is thrown (using SerenityRest.rest()).
NoEmptyStackExceptionTest class contain code without exception (not using SerenityRest.rest()).

Is there possibility to execute the same request in @BeforeClass method as in @Before and @Test methods or I have to create separated request for them (as in example project)?

I would be grateful for your feedback.

Project to reproduce described problem:
restassured-exception.zip

@wakaleo
Copy link
Member

wakaleo commented Oct 24, 2017

Serenity RestAssured is a wrapper around RestAssured that helps report on REST activity within a test. When you call it outside of a test (i.e. in a @BeforeClass method), there is no test context to interact with. This is why it fails. I could write a patch so that it doesn't fail, but do you expect this REST query to appear in your Serenity reports?

@MariuszSkomra
Copy link
Author

Thank you for explanations.

On the one hand - no, I'm not expecting it to appear in Serenity report. I can add logging and find all necessary information in console output.
But on the other hand it would be great feature if REST query from @BeforeClass would somehow appear in report (maybe for each test from Test class same like REST query from @Before method). It would be very helpful when (for example) query fail - to easily find detailed information about it and a response from server.

To sum up my issue - for now I have to create separated query (only with RestAssured class) and log as much information as I can.

@wakaleo
Copy link
Member

wakaleo commented Oct 24, 2017

Unfortunately, any steps invoked outside of the test lifecycle cannot appear in the reports (what test would they be associated with?)

@MariuszSkomra
Copy link
Author

OK :) I understand. Then your proposition:

I could write a patch so that it doesn't fail

will be enough solution for me.

wakaleo added a commit that referenced this issue Nov 1, 2017
@wakaleo
Copy link
Member

wakaleo commented Nov 1, 2017

Should be fixed in 1.6.8.

@wakaleo wakaleo closed this as completed Nov 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants