JUnit @Rule, @ClassRule methods not getting called by Cucumber

2,311 views
Skip to first unread message

80Vikram

unread,
Jun 2, 2017, 2:37:54 AM6/2/17
to Cukes
Hi All,

I've below code in main class

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features={"src/test/java/mobile/test/examples/ContactManager.feature"})
public class ContactManagerCucumber{

}

But this doesn't call methods with JUnit annotations @Rule, @ClassRule methods

Please let me know if you know the correct way to get this executed

Thanks in advance.

Regards,
Vikram

Paolo Ambrosio

unread,
Jun 2, 2017, 8:21:50 AM6/2/17
to cu...@googlegroups.com
On Fri, Jun 2, 2017 at 7:37 AM, 80Vikram <vikra...@gmail.com> wrote:
> Hi All,
>
> I've below code in main class
>
> import org.junit.runner.RunWith;
>
> import cucumber.api.junit.Cucumber;
> import cucumber.api.CucumberOptions;
>
> @RunWith(Cucumber.class)
> @CucumberOptions(features={"src/test/java/mobile/test/examples/ContactManager.feature"})
> public class ContactManagerCucumber{
>
> }
>
> But this doesn't call methods with JUnit annotations @Rule, @ClassRule
> methods

I don't think the Cucumber Junit runner considers Rules at all. What
are you trying to run with those rules?

I suspect that what you want to do might be achievable via hooks (in a
consistent way, that does not depend on any runner used).

MP Korstanje

unread,
Jun 3, 2017, 4:52:49 AM6/3/17
to Cukes
There can be no test rules because the cucumber runner generates a suite of junit tests from feature files. There is no way to add junit annotations to feature files.

The runner class however is inspected for class rules.
You have to put the class rule on the runner. They'll execute once for the whole suite.

@RunWith(Cucumber.class)
@CucumberOptions(...)
public class Some2IT {

  @ClassRule
  public static TestRule classRule = new TestRule(){

    @Override
    public Statement apply(Statement base, Description description) {
      System.out.println("Class Rule" + description);
      return base;
    }
  };
}

80Vikram

unread,
Jun 3, 2017, 6:53:00 AM6/3/17
to Cukes
Hi Paolo,

Please find below update,  we've enhancing a framework based on JUnit with Cucumber integration.

Frame has got below code

@ClassRule
    public static TestRule suiteWatcher = new TestWatcher() {
        @Override
        protected void starting(Description desc) {
            ..
        }

        @Override
        protected void finished(Description desc) {
            ..
        }
    };

    @Rule
    public TestRule testWatcher = new TestWatcher() {
        @Override
        protected void starting(Description description) {
           ..

            if (info != null) {
                ..
            } else {
              ..
            }
        }

        @Override
        protected void finished(Description description) {
            ..
        }

        @Override
        protected void failed(Throwable e, Description description) {
           ..
            }
        }

    };

Can you please suggest any better way to handle this in Cucumber scenario ? This part of code is used for detailed logging and reporting purpose only.


Thanks in advance.

Regards,
Vikram



MP Korstanje

unread,
Jun 3, 2017, 3:31:53 PM6/3/17
to Cukes
@Rule will never work.

You'd be best off writing a custom reporter and pass that as a plugin to cucumber through CucumberOptions.

http://automationrhapsody.com/create-cucumber-jvm-custom-formatter/

80Vikram

unread,
Jun 6, 2017, 6:05:07 AM6/6/17
to Cukes

Thanks Korstanje for clarifications and link. I'll try to make changes accordingly.

Regards,
Vikram

80Vikram

unread,
Jun 6, 2017, 6:06:05 AM6/6/17
to Cukes
Hi Paolo,

Can you please clarify what exactly is hooks ?

Any link or sample code in github can be useful for learning.


Regards,
Vikram

On Friday, June 2, 2017 at 2:21:50 PM UTC+2, Paolo Ambrosio wrote:

80Vikram

unread,
Jun 6, 2017, 10:01:06 AM6/6/17
to Cukes
Hey Paolo,

I hope you're talking about https://github.com/cucumber/cucumber/wiki/Hooks

In case you've any sample github repo please share.

Thanks & Regards,
Vikram

Paolo Ambrosio

unread,
Jun 6, 2017, 2:54:57 PM6/6/17
to cu...@googlegroups.com
On Tue, Jun 6, 2017 at 3:01 PM, 80Vikram <vikra...@gmail.com> wrote:
> Hey Paolo,
>
> I hope you're talking about https://github.com/cucumber/cucumber/wiki/Hooks

Yes, those hooks, but in Java. I was surprised not to find anything in
the official documentation.

Googling a bit I found this that seems to be a decent introduction to the topic:
https://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

Something that the blog post doesn't cover (because it was introduced
later if I recall correctly) is the optional Scenario argument, but
you can read about it here (in the first and only answer):

https://stackoverflow.com/questions/35550386/cucumber-jvm-hooks-when-scenario-is-passed-or-failed

I would also consider buying "The Cucumber for Java Book" that is an
excellent learning resource, well worth the money:
https://pragprog.com/book/srjcuc/the-cucumber-for-java-book

> In case you've any sample github repo please share.

You can find some examples in the examples directory of Cucumber-JVM's
repository on GitHub. This is one:

https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-webbit-websockets-selenium/src/test/java/cucumber/examples/java/websockets/ServerHooks.java

Cheers,
Paolo
> --
> Posting rules: http://cukes.info/posting-rules.html
> ---
> You received this message because you are subscribed to the Google Groups
> "Cukes" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cukes+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

80Vikram

unread,
Jun 7, 2017, 5:40:27 AM6/7/17
to Cukes
Hey Paolo,

Thanks a ton for detailed info & pointers.

Regards,
Vikram

80Vikram

unread,
Jun 8, 2017, 6:15:54 AM6/8/17
to Cukes
Hi Paolo, MO Korstanje,

Please find below update of the final implementation

1. JUnit methods ( called by @ClassRule, @Rule ) are pushed into independent methods

2.  These are still called by JUnit

3. In case of Cucumber I wrote below 3 methods

@Before
public void setUpBeforeScenarioAndBeforeFeature(){
//used static variable to call method related to before class execution
//before each scenario method gets called here

}

@After
public void tearDownAfterSceanrio(){
//after reach scenario method gets called here
}

@After("@last") //had added this tag to last scenario in each of the feature file
public void tearDownAfterFeature(){
//after class method will be called from here
}


The engineer with whom I'm collaborating on framework; is not yet following BDD but for now relies on JUnit to execute test cases.
I'm forcing him hard to start practicing BDD though :)

Thanks & Regards,
Vikram

Paolo Ambrosio

unread,
Jun 9, 2017, 1:51:32 AM6/9/17
to cu...@googlegroups.com
On Thu, Jun 8, 2017 at 11:15 AM, 80Vikram <vikra...@gmail.com> wrote:
> Hi Paolo, MO Korstanje,
>
> Please find below update of the final implementation
>
> 1. JUnit methods ( called by @ClassRule, @Rule ) are pushed into independent
> methods
>
> 2. These are still called by JUnit
>
> 3. In case of Cucumber I wrote below 3 methods
>
> @Before
> public void setUpBeforeScenarioAndBeforeFeature(){
> //used static variable to call method related to before class execution
> //before each scenario method gets called here
>
> }
>
> @After
> public void tearDownAfterSceanrio(){
> //after reach scenario method gets called here
> }
>
> @After("@last") //had added this tag to last scenario in each of the feature
> file
> public void tearDownAfterFeature(){
> //after class method will be called from here
> }

Hi Vikram,

Tagging the last scenario in each feature file is dangerous. There are
several reasons, but think of what would happen when running a single
scenario that is not the last one, or randomising the scenario
execution.

What do you need to do at the end of each feature?

Paolo
Reply all
Reply to author
Forward
0 new messages