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
206
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
  • 5,512
  • 6
  • 50
  • 78
75
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,322
  • 12
  • 34
  • 41
67
votes
3 answers

How to replace @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
  • 2,929
  • 4
  • 24
  • 44
32
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
  • 331
  • 3
  • 3
21
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,473
  • 2
  • 21
  • 31
19
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
  • 658
  • 2
  • 8
  • 14
18
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
  • 776
  • 2
  • 6
  • 22
17
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,551
  • 1
  • 19
  • 28
13
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
  • 55,820
  • 69
  • 166
  • 236
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
  • 53,386
  • 69
  • 224
  • 390
12
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
  • 780
  • 9
  • 28
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
11
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
  • 3,502
  • 3
  • 30
  • 34
10
votes
3 answers

How to get the activity reference before its oncreate gets called during testing

How to get the reference of Activity before its onCreate will be called. while its under test. I use ActivityTestRule as JUnit Rule. The reason for this requirement is i want to inject Mocks into activity from tests. public class MyActivity extends…
Rajesh Batth
  • 1,604
  • 2
  • 19
  • 23
8
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
  • 113
  • 1
  • 6
1
2 3 4 5 6