Questions tagged [junit-rule]

Rules are feature of JUnit, which allow to execute code befor, after or instead of a test.

Rules among other things can be used to

  • setup and tear down resources
  • change the way a test executes (like running it multiple times or in a special thread)
  • perform additional checks on the test like checking execution doesn't take longer than a certain threshold or that it doesn't write to System.out.

A Rule gets implemented by implementing the org.junit.rules.TestRule interface and adding the Rule as a public field with the @Rule annotation to the test class.

In former version of JUnit the interface for Rules was org.junit.rules.MethodRule

For examples how to implement a TestRule, you might check out the various rules that JUnit provides out of the box: https://github.com/KentBeck/junit/tree/master/src/main/java/org/junit/rules

77 questions
3
votes
3 answers

Use Spring Data random (embedded) Mongo port with NoSQL JUnit @Rule

I'm currently trying to write an Integration test class which uses Spring Data Mongo repositories. I use an embedded Mongo instance provided by the de.flapdoodle.embed.mongo dependency. Spring Data documentation specifies that we only have to put…
Lebowski
  • 588
  • 7
  • 21
3
votes
1 answer

How to apply a Hamcrest matcher to the property of a class under test?

Is there a way to build a combined Hamcrest matcher which tests an object and the property of this object? - pseudo code: both( instanceof(MultipleFailureException.class) ).and( // pseudo code starts adapt( new…
Claude
  • 1,724
  • 3
  • 17
  • 46
3
votes
3 answers

Skip @Before for a test || Is there a way to execute the @Before methods in a TestRule?

High level, I have JUnit test class which is pretty straightforward. I have several @Tests and a single @Before which does some set up. For one test case, the setup varies (I don't want it to be run). From some searching I found…
loeschg
  • 29,961
  • 26
  • 97
  • 150
3
votes
1 answer

Is it possible to create a JUnit Rule inside an interface

I'm trying to create test automation suite using JUnit. For all the tests, I want to create a Rule, for this I have created an Interface and placed the Rule inside it. Any tests that I want to run will have to implent that interface. It didn't throw…
Buddha
  • 4,339
  • 2
  • 27
  • 51
3
votes
2 answers

check errorcode with @rule in junit

I found @Rule annotation in jUnit for better handling of exception. Is there a way to check error code ? Currently my code looks like (without @Rule): @Test public void checkNullObject() { MyClass myClass= null; try { …
Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
2
votes
1 answer

How to use @Rule in Selenium Junit to fail a test

I've got the following test which I have put a try catch around so it fails if actual value does not equal expected value: try{ Assert.assertEquals(ExpectedCount, ActualCount); }catch (Throwable t){ System.out.println…
Pritum Patel
  • 57
  • 2
  • 6
2
votes
0 answers

Handling jUnit exceptions with DevAppServer

I am having an issue using jUnit tests and handling exceptions in Java. I created my TestClass with both the annotations @RunWith(DevAppServerTestRunner.class) and @DevAppServerTest(TestConfig.class) since I need to test a method that use some…
2
votes
1 answer

Apply a JUnit Custom @Rule a particular test method

Can I run a custom rule only for a particular test method in a test class? public class TestClassExample { @Rule public CustomRuleForOneEqualsOne customRuleForOneEqualsOne = new CustomRuleForOneEqualsOne(); @Test public void…
Gowrav
  • 289
  • 1
  • 4
  • 20
2
votes
1 answer

JUnit + Java + ErrorCollector issue

I'm having some trouble with ErrorCollectors in Java. I have some code, which is comparing two values. If the values match, a pass is the result. If the values do not match, a fail. Sounds simple. So I created a basic test case: public class…
Joseph
  • 541
  • 1
  • 4
  • 31
2
votes
1 answer

How to inject the value coming from a rule into a test spring context?

suppose you have the following situation while testing a spring context @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {ConfigClass.class}) public class IntegrationTest { @ClassRule static…
gotch4
  • 13,093
  • 29
  • 107
  • 170
2
votes
2 answers

Can't run PowerMock with JUnitParams

I'm trying use PowerMock with JUnitParams, however I'm getting this strange error: com.thoughtworks.xstream.converters.ConversionException: com.lutum.web.ui.controladores.ControladorExibicaoPDF$$EnhancerByMockitoWithCGLIB$$2852e4bb :…
brevleq
  • 2,081
  • 10
  • 53
  • 97
2
votes
2 answers

what is the best place to verify if an external system is available before executing tests?

We are using JUnit to execute integration tests and also the system integration tests which rely on external test systems (not necessarily maintained by our own company). I wonder where to put the code that checks if the system is available prior to…
wemu
  • 7,952
  • 4
  • 30
  • 59
1
vote
1 answer

JUnit 4: how to get test name inside a Rule?

JUnit 4: how to get test name inside a Rule? e.g., public class MyRule extends ExternalResource { @Before public void before() { // how to get the test method name to be run? } }
eastwater
  • 4,624
  • 9
  • 49
  • 118
1
vote
2 answers

JUnit getMethodName returns null

I'm running some Selenium tests and I'm not able to access my test methods' names. I would like to print something in the logs like "Starting test: foobarbaz" All my test classes inherit a public "AbstractTest" class, which contains: @Rule TestName…
Zoette
  • 1,241
  • 2
  • 18
  • 49
1
vote
2 answers

Spock test framework - how to parametrize a @Rule resource?

I am updating a Spock tests. There are few mocks and a @Rule resource: AuthTokenService mockAuthTokenService = Mock() ObjectMapper mockObjectMapper = Mock() GeneralConfiguration conf = Mock(); def CLA_BASE_URL =…
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277