views:

64

answers:

1

I'd like to create @Rule to be able to do something like this

@Test public void testValidationDefault(int i) throws Throwable {..}

Where i is parameter passed to the test by @Rule.

However I do get

java.lang.Exception: Method testValidationDefault should have no parameters

is there any way to bypass it and set the i parameter in the @Rule?

+2  A: 

I use @Parameters and @RunWith(value = Parameterized.class) for passing values to tests. An example can be found here.

I did not know about the @Rule annotation, but after reading this post, I think it serves another purpose than passing parameters to the tests:

If in your test class, you create a field pointing to an object implementing the MethodRule interface, and you mark this to be processed as a rule, by adding the @Rule implementation, then JUnit will call back on your instance for every test it will run, allowing you to add additional behavior around your test execution.

An additonal example of how to use @Rule is here.

I hope this helps.

MarcoS