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
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,672
  • 2
  • 19
  • 23
8
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
  • 977
  • 2
  • 12
  • 22
8
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
  • 800
  • 10
  • 22
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
  • 19,364
  • 17
  • 93
  • 129
7
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
  • 19,209
  • 22
  • 79
  • 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,592
  • 2
  • 27
  • 39
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
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
  • 7,078
  • 4
  • 50
  • 90
6
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
  • 81,660
  • 23
  • 145
  • 268
6
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
  • 2,316
  • 2
  • 16
  • 16
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
  • 544
  • 5
  • 26
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
  • 8,301
  • 11
  • 55
  • 73
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
  • 75
  • 6
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
  • 2,033
  • 4
  • 18
  • 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
  • 87,243
  • 191
  • 450
  • 722