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

JUnit Rule annotation is not supported #393

Closed
rlogiacco opened this issue Sep 14, 2012 · 13 comments
Closed

JUnit Rule annotation is not supported #393

rlogiacco opened this issue Sep 14, 2012 · 13 comments

Comments

@rlogiacco
Copy link

The JUnit Rule annotation support would be very useful to allow to take snapshots on failing tests, but the Cucumber JUnit runner does not support such annotation.
I think that it should be easy to add this support by modifying the runners and introducing the JUnit Statement class usage: I've forked the project and started working on it: please let me know if this is an unwanted feature.

@aslakhellesoy
Copy link
Contributor

You can take screenshots in an After hook, checking scenarioResult.isFailed() if you only want it to happen for failed scenarios. What else do you need Rule support for? I'm sceptical of making Junit a dependency in core.

See https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-webbit-websockets-selenium/src/test/java/cucumber/examples/java/websockets/SharedDriver.java#L53

@rlogiacco
Copy link
Author

I'm not suggesting of making JUnit a dependency in core, but in cucumber-junit which already depends on JUnit. The point is that rules are very flexible and represent a JUnit standard extension mechanism and supporting rules can redirect all those weird requests you receive into just "can't you write a rule for it?"

BTW, I tried that solution, but I wasn't able to have it working as I get an exception (I'm using Spring and the JavaHookDefinition class is trying to instantiate my singleton web driver a second time) and the imports look quite different as cucumber.api.java.After should be cucumber.annotation.After which makes me think that project is not up to date.

@aslakhellesoy
Copy link
Contributor

Feel free to send a pull request. I can't guarantee I'll accept it, but it's always easier to discuss around code.

@rlogiacco
Copy link
Author

I spent a few hours on this and I ended up with an implementation that was working withing the JUnit constraints but it was then unable to manipulate the report so I think it's not worth to be evaluated.

Thanks for your attention.

@markwinspear
Copy link

Hi. I came across this thread because I am trying to put together a simple framework using a Page Object Model patten using Webdriver and using Cucumber as the BDD framework. I'm not a developer first and foremost, however I have taken the advice of Dave Haeffner in his book 'Selenium Guidebook for Java' in setting up a Base test class which uses JUnit's rule to set up an external resource and set capabilities then fire up a browser either locally or in saucelabs.

I have conflict between this and my cucumber annotations and cannot get the two to play nicely together when my base test class is extended by my SearchTestDefinitions class. I suspect that if the cucumber api supported JUnit rule, I would not get this issue (but as I say, I am not a developer by nature).

Any advice on how to combine the two if possible would be welcomed

The code is at https://github.com/markwinspear/SeleniumCucumberJavaBasicTest2

Many thanks
Mark

@mikeholler
Copy link

Rules are important for Android. See #894.

@johndeverall
Copy link

I just came across this thread - see Serenity issue serenity-bdd/serenity-core#221

It seems this feature would be very helpful for Spring integration, and a lot of people use Spring in the JVM world. Is there any chance this could be looked at again?

@alb-i986
Copy link
Contributor

+1

I think the solution would be to have cucumber.api.junit.Cucumber extend BlockJUnit4ClassRunner instead of ParentRunner. In fact, BlockJUnit4ClassRunner encapsulates the Rules handling logic.
I'll see if I can find some time working on it.

@johndeverall
Copy link

I'm also wanting to put some time into this but haven't looked into it since I last commented as I've been away on holiday. @alb-i986 I'll take a look at what you're suggesting. It sounds like you're ahead of me in conceptualizing a solution. I'll be able to comment more once I take a fresh look at the code.

@alb-i986
Copy link
Contributor

@aslakhellesoy
Would you agree on having cucumber.api.junit.Cucumber extend BlockJUnit4ClassRunner instead of ParentRunner? Can you foresee any issues in doing that?
Thanks

@JasonSmiley208
Copy link

I can think of one potential issue, but maybe not an issue (not sure how Cucumber.class is being used).

BlockJUnit4ClassRunner does not work with dependency injection frameworks like Spring. There is Spring JUnit Runner for example and I am assuming other Dependency Injection Frameworks (DIs) use their own runners as well. I know that the Spring Runner current does not support all @rule annotations (in the documentation).

Therefore, if cucumber.api.junit.Cucumber is not the same runner which is used for DI Runner implementations, then I see no potential issue updating the runner (with my limited knowledge).

If this class is being used for by the DI cucumber runners, then you might just need a "CucumberJunitRule" runner for Pure Java implementations.

For the record, I would love to see a Cucumber Runner which would allow me to use rules out of the box :).

Currently I am just pulling in the Rules manually into my projects (copy and paste style) since I usually update them to work with my code base anyways (basically I would pull in my scenario objects for logging purposes)

@rlogiacco
Copy link
Author

There is also a JUnit 5 alternative... :)

Il lunedì 29 febbraio 2016, Jason Smiley [email protected] ha
scritto:

I can think of one potential issue, but maybe not an issue (not sure how
Cucumber.class is being used).

BlockJUnit4ClassRunner does not work with dependency injection frameworks
like Spring. There is Spring JUnit Runner for example and I am assuming
other Dependency Injection Frameworks (DIs) use their own runners as well.
I know that the Spring Runner current does not support all @rule
https://github.com/Rule annotations (in the documentation).

Therefore, if cucumber.api.junit.Cucumber is not the same runner which is
used for DI Runner implementations, then I see no potential issue updating
the runner (with my limited knowledge).

If this class is being used for by the DI cucumber runners, then you might
just need a "CucumberJunitRule" runner for Pure Java implementations.


Reply to this email directly or view it on GitHub
#393 (comment)
.

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants