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
236
votes
4 answers

How does Junit @Rule work?

I want to write test cases for a bulk of code, I would like to know details of JUnit @Rule annotation feature, so that I can use it for writing test cases. Please provide some good answers or links, which give detailed description of its…
Dipak
  • 6,532
  • 8
  • 63
  • 87
96
votes
5 answers

How to replace WireMock @Rule annotation in JUnit 5?

I'm using WireMock in my tests and have such a line of code: @Rule public WireMockRule wireMockRule = new WireMockRule(8080); I want to switch to JUnit 5. So I added the next dependency (using…
IKo
  • 4,998
  • 8
  • 34
  • 54
79
votes
4 answers

Mockito verify after exception Junit 4.10

I am testing a method with an expected exception. I also need to verify that some cleanup code was called (on a mocked object) after the exception is thrown, but it looks like that verification is being ignored. Here is the code. I am using the…
Eqbal
  • 4,722
  • 12
  • 38
  • 47
33
votes
1 answer

Why @Rule annotated fields in JUnit has to be public?

In JUnit test case, a field annotated by @Rule must be public. It breaks a common Java coding convention (all class member variables should not be public). Why does JUnit require this? Documentation for @Rule:…
Shuhai Shen
  • 341
  • 3
  • 3
23
votes
4 answers

How to continue test after JUnit ExpectedException if thrown?

I have set up some JUnit (4.12) test with the ExpectedException feature, and I would like the test to continue after the expected exception. But I never see the log '3', as the execution seems to stop after the exception, event if catch? Is this…
Deathtiny
  • 728
  • 3
  • 8
  • 14
23
votes
5 answers

Run unit tests only on Windows

I have a class that makes native Windows API calls through JNA. How can I write JUnit tests that will execute on a Windows development machine but will be ignored on a Unix build server? I can easily get the host OS using…
darrenmc
  • 1,721
  • 1
  • 19
  • 29
22
votes
5 answers

Combining @ClassRule and @Rule in JUnit 4.11

In JUnit 4.10 and below, it is possible to annotate a rule as both @Rule and @ClassRule. This means the rule gets invoked before before/after the class, and before/after each test. One possible reason for doing so is to set up an expensive external…
Rowan
  • 2,585
  • 4
  • 24
  • 34
19
votes
3 answers

Apply '@Rule' after each '@Test' and before each '@After' in JUnit

I have a test suite where I am logging out of the system in @After and closing the browser in @AfterClass. I am trying to use @Rule to take failed test screenshot using Selenium for every test method. I checked manually that @Rule only runs before…
Reaz Patwary
  • 824
  • 2
  • 7
  • 23
15
votes
4 answers

How to test logging in junit5?

I am transitioning from junit4 to junit5 on a project and trying to figure out how to test logs. Previously, I used @Rule OutputCapture outputCapture = new OutputCapture(); and would then write an assertion using outputCapture.toString(), for…
ByteByByte
  • 303
  • 1
  • 2
  • 11
15
votes
2 answers

How to create a JUnit TemporaryFolder with subfolders

I would like to create a JUnit TemporyFolder that represents the baseFolder of such a tree: baseFolder/subFolderA/subSubFolder /subFolderB/file1.txt As far as I understand I can setUp a TemporaryFolder and than can create with…
KFleischer
  • 942
  • 2
  • 11
  • 33
14
votes
1 answer

Alternatives to the deprecated ExpectedException.none() in JUnit 4.13

I am trying to use the @Rule annotation with ExpectedException The ExceptedException.none() method to initialize a variable type of ExceptedException says it has been deprecated what are the alternatives to initialize an object of the…
14
votes
5 answers

JUnit @Rule to pass parameter to test

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…
IAdapter
  • 62,595
  • 73
  • 179
  • 242
13
votes
2 answers

JUnit Rule TemporaryFolder

I'm creating a TemporaryFolder using the @Rule annotation in JUnit 4.7. I've tried to create a new folder that is a child of the temp folder using tempFolder.newFolder("someFolder") in the @Before (setup) method of my test. It seems as though the…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
12
votes
2 answers

@Before method in TestRule is not called

I implemented a JUnit 4 TestRule (extending an ExternalResource), and injected it as a @ClassRule in my test class: I want to initialize a resource once for all in every test of this class, and tear it down eventually. My issue is that my @Before…
Campa
  • 4,267
  • 3
  • 37
  • 42
11
votes
3 answers

Test the error code of a custom exception with JUnit 4

I would like to test the return code of an exception. Here is my production code: class A { try { something... } catch (Exception e) { throw new MyExceptionClass(INTERNAL_ERROR_CODE, e); } } And the corresponding exception: class…
Guillaume
  • 135
  • 1
  • 8
1
2 3 4 5 6