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
1
vote
1 answer

JUnit Rule and cdi

I'm not really familiar with cdi or or JUnit Rule (in fact I've just discovered them) but I try to test my ldap api with the embedded-ldap-junit library (based on UnboundID LDAP SDK) following this post. I precise that my api is not based on…
OphyTe
  • 68
  • 9
1
vote
1 answer

JUnit does not seem to be invoking my overridden TestWatcher failed/succeeded methods

Note: The code for MyTest has been updated to reflect the answer I found, which is caused by multiple implementations of assertEquals() being available in JUnit's ecosystem. I've seen a similar question in C, but no answer/responses. I've either…
SoCal
  • 801
  • 1
  • 10
  • 23
1
vote
1 answer

Testing a PreferenceFragment does not work with Espresso and JUnit-Rules or ActivityInstrumentationTestCase2

I am working on an app with a settings screen, which i want to test with espresso. In this settings screen i add a PreferenceFragment like…
richard
  • 724
  • 2
  • 14
  • 36
1
vote
0 answers

Suppress android test based on device type?

I run an Instrumentation test suite using Gradle 'connectedCheck' which executes tests on all connected devices. Some tests fail only on certain devices and I want a mechanism to suppress/ignore those tests only for that device. Can anyone tell me a…
Karthik Kota
  • 423
  • 1
  • 5
  • 10
1
vote
1 answer

How to share an ExternalResource between two test classes?

I am struggling a bit to understand the benefit of using ExternalResource. The documentation and other posts (How Junit @Rule works?) have all alluded to being able to share code between tests within a class and/or share code between test…
user1766760
  • 631
  • 2
  • 8
  • 26
1
vote
1 answer

JUnit Rule not being executed when test fails

Following is my test file : package test; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestWatcher; import org.junit.runner.Description; public class TestWatcherTest { @Rule public…
aradhak
  • 836
  • 1
  • 9
  • 22
1
vote
1 answer

Exception assertion along with other assertions jUnit

I have a method that throws exception. And i have a test like this. @Rule public ExpectedException expectedEx = ExpectedException.none(); @Test public void shouldThrowExceptionIfValidationFails() throws Exception { doThrow(new…
Prasanna
  • 10,956
  • 2
  • 28
  • 40
1
vote
1 answer

What is the logic of @Rule(JUnit) declaration and assignment in a Groovy class

Trying some variants of rules creation in a groovy file, I have come to the thought, that @Rule doesn't describe DECLARATION, but ASSIGNMENT. So, the runner, when loading the test, tries every rule for the correct assignment. //Correct…
Gangnus
  • 24,044
  • 16
  • 90
  • 149
1
vote
1 answer

Does Mule FunctionalTestCase needs org.junit.rules.TestRule class to run

I'm trying to run a simple Functional test in Mule 3.3. Below is my sample code: import java.util.Map; import org.junit.Test; import org.mule.api.MuleMessage; import org.mule.api.client.MuleClient; import…
Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
0
votes
1 answer

Junit Rule with PowerMockito : ClassNotPreparedException

I working on testing some static classes using PowerMockito, and sometimes the test fail, in order to overcame this issue a create a customize JUnit Rule to re-run the failure tests. The rule works fine but whenever the test is re-executed , it's…
Ak.tech
  • 71
  • 1
  • 5
0
votes
0 answers

Execute code in Spock on test Succeed and Failure

I want to execute a block of code that changes based on a pass or a fail. Basically looking to set the results from a test in our test case tracking software. I created my own test rules, shown below, but no matter if the test passes or fails it…
Mouse
  • 1
0
votes
2 answers

Kotlin - Replace class delegation - Multiple classes with same functionality different signature

I am working with JunitRules RuleChain and Kotlin. I am new to both and have two classes that do the exact same thing, the only difference being the rule chain. The first class looks like this : class BaseActivityTestRule( …
0
votes
0 answers

Trying to mock method in same class using JMock but it is not mocking the same

I am trying to mock the method which is used in same class but it is giving me exception. The expectation is that newId() mock return String id which is added to List idList and mocked to insert(idList) method. Test class is : public final…
Rahul
  • 95
  • 1
  • 3
  • 14
0
votes
1 answer

RunTimeException from Thread not logged into SystemErrRule

I am trying test if RunTimeException thrown from thread is added into the System.err. But even after waiting for 1 min it is not found into System.err Following is code piece public class TestTest { @Rule public SystemErrRule errRule = new…
0
votes
0 answers

Unitils EasyMock and JUnit @Rule from JUnit 4.10 to 4.11

I have the Problem that when I use the @RuleAnnotation from JUnit for my TemporaryFolder and want to use Mocks from unitils.easymock at the same time I get an IlleagalStateException in JUnit 4.11, whereas in JUnit 4.10 it still works. So the…
DEX
  • 95
  • 1
  • 7