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
8
votes
3 answers

Is it possible to mock a static method on a final class using a PowerMockRule instead of the PowerMockRunner?

According to the PowerMock docs, I should be able to run using a PowerMockRule instead of @RunWith(PowerMockRunner.class) and get the same results. I seem to have found a case where this isn't true. The below sample runs fine: package…
Tom Tresansky
  • 17,860
  • 16
  • 81
  • 122
7
votes
1 answer

CdiUnit test with Junit @Rule is impossible because of a public private field paradox

The following snippet is enough to reproduce my problem: Either I set the thrown attribute public and get the error org.jboss.weld.exceptions.DefinitionException: WELD-000075: Normal scoped managed bean implementation class has a public field Or I…
Aldian
  • 2,481
  • 2
  • 24
  • 38
7
votes
1 answer

Parallelize test execution in a @Rule

I want to reuse some integration tests for load testing purposes. I implemented a rule which is parameterized by an annotation: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Parallel { int invocations()…
Alexander Hansen
  • 813
  • 8
  • 14
6
votes
1 answer

How can I automatically skip certain JUnit tests based on a condition?

I would like a simple way to assign a priority value to my JUnit tests such that I can say 'run only priority 1 tests', 'run priority 1, 2 and 3 tests' etc. I am aware that I could just include a line like Assume.assumeTrue("Test skipped for…
Dave
  • 739
  • 9
  • 19
6
votes
2 answers

Aggregating JUnit Rules with dependencies between one another

In more complex unit tests, I often require a certain set of Rules to be present. Some of these Rules have dependencies to another. As the ordering is relevant, I use RuleChains for that. All good so far. This however is duplicated in most tests…
geld0r
  • 681
  • 8
  • 20
5
votes
3 answers

Best way of logging exceptions when tests fail (e.g. using a junit rule)

When I'm running a complete test suite, it would be helpful if exceptions that caused a test to fail would appear in my (SLF4J-)log. What is the best method to achieve this? What I would like is a junit4 rule that handles exception logging for me.…
DaveFar
  • 6,260
  • 3
  • 39
  • 80
5
votes
1 answer

How do I replace DropwizardAppRule in Junit5

In Junit 4 I could do something like @ClassRule public DropwizardAppRule app = new DropwizardAppRule<>(MyApp.class); ... app.getLocalPort() How do I replicate this behavior in Junit 5? From this github issue it seems like I need to…
David says Reinstate Monica
  • 16,634
  • 19
  • 69
  • 108
5
votes
3 answers

Using multiple TestRules in the same test

I have written a custom TestRule to use with my Android test suite. It populates a table in the database used by the app under test. Now I need to use this DataRule along with ActivityTestRule. Can I have two fields of my test class annotated with…
Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
5
votes
4 answers

How to combine @Rule and @ClassRule in JUnit 4.12

According to the 4.12 release notes, it is possible to annotate static members of a test class with both @Rule and @ClassRule: a static member annotated with both @Rule and @ClassRule is now considered valid. This means a single rule may be used to…
martiansnoop
  • 1,996
  • 2
  • 13
  • 15
5
votes
1 answer

Log4j appender for debug output if test fails

I would like to set up my test-logging so that log output is mainly suppressed - unless a test fails. then I would like to have the debug output. I know the manual solution of just modifying the log4j.properties in the test classpath or just having…
Flyhard
  • 498
  • 4
  • 22
5
votes
1 answer

Can I apply a time limit for all the tests in the suite

Is there a way in JUnit to define a global timeout/limit for all the tests included in the suite. Well, let me to explain this a little. I want to introduce a junit test suite into the build system of a legacy project that would be run on every…
Roland Tepp
  • 7,673
  • 10
  • 53
  • 68
3
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…
3
votes
3 answers

Initialize a List in a JUnit via @Rule

I am writing a JUnit test case. I want to initialize a list objects. For brevity, let's initialize a list of String objects: public class MyTest { @Rule public List tests = Arrays.asList(new MySQLContainer("5.5"), new…
Messol
  • 65
  • 5
3
votes
2 answers

Autowire JUnit rule in integration test

I have a piece of code which will be repeated across multiple integration tests. The code will run before and after tests. I have decided that using an JUnit @Rule would be the best way to achieve this. The problem is that the rule will need to…
Taks
  • 1,763
  • 3
  • 15
  • 23
3
votes
2 answers

java.lang.IllegalStateException: the temporary folder has not yet been created

I am creating a new @Rule for my use case which looks like public class ActiveDirectoryConfigurationRule extends ExternalResource { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); public File…
daydreamer
  • 73,989
  • 165
  • 410
  • 667