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
0
votes
0 answers

Expected exception and expected message is not working in loops

Hi All I am doing Junit testing on Spring MVC project. Here the following code Method to be tested public UserDetails getUserInfo(String userID) { Session session = sessionFactory.getCurrentSession(); UserDetails userDetails = new…
0
votes
2 answers

Changing CLASSPATH to JUNIT_HOME/junit-4.12.jar, java command stops working

When I change my CLASSPATH variable to JUNIT_HOME/junit-4.12.jar in system variables, my Java command stops working. For example, when I want to execute a class file I get the error "Could not find or load main class", though javac is working…
Shubham Khare
  • 171
  • 1
  • 1
  • 12
0
votes
2 answers

the JAR of this class file belongs to container 'Junit 4' which does not allow modifications to source attachements on its entries

I am new to Junit framework. I am trying to invoke private method in another class from Junit test class using reflection API. I am getting below error while running JUnit test: java.lang.ExceptionInInitializerError at…
Karuna
  • 719
  • 1
  • 11
  • 22
0
votes
1 answer

Unable to use Burst with other test runner in JUnit

I found this interesting JUnit project: https://github.com/square/burst It's documentation only specify how to use it with class runner. However, Since JUnit only allows 1 Runner per JUnit, using Burst means user will have difficulty run it in…
janetsmith
  • 8,562
  • 11
  • 58
  • 76
0
votes
1 answer

Is it possible to find current JUnitParams parameters from a custom rule?

I started using JUnitParams to write parameterized tests and it works awesome. For example, the following test is invoked with false, then with true: @Test @Parameters ({ "false", "true" }) public void testBla (boolean foo) throws Exception ... One…
user319799
0
votes
1 answer

Using System Rules to intercept System.exit() fails with Swing windows

I'm tasked with setting up some JUnit tests for a utility app that is required to System.exit(1) under various circumstances, and System Rules is great for testing that. The app can also run in GUI mode and it pops a small JFrame. When it's…
dave thompson
  • 133
  • 3
  • 9
0
votes
1 answer

Junit TestRule annotates fields vs methods

Assuming you have a TestRule (for example, a TemporaryFolder) defined, you can apply the rule in a test class by either way below: 1.By annotating a method with @Rule public MyTestClass{ private TemporaryFolder folder= new TemporaryFolder(); …
sunrise
  • 195
  • 1
  • 11
0
votes
1 answer

Skip a test from a TestWatcher

The project I'm working on is currently using JUnit 4.10 and am trying to upgrade to 4.12 to get benefits from some features that's been introduced in the last 2 versions. However, TestWatcher has changed and now is catching every exception thrown…
Luis Mirabal
  • 2,606
  • 2
  • 16
  • 12
0
votes
1 answer

Extending from Suite Runner vs BlockJUnit4Runner while defining some TestRules

I have a requirement to write many tests. I have extended Suite Runner of JUnit in order to be able to add new annotations where I can mention several Prerequisite classes which will be executed before any of the tests or setups get executed. My…
Buddha
  • 4,339
  • 2
  • 27
  • 51
0
votes
1 answer

xwiki logrule does not capture log statements

I am trying to use the XWiki LogRule to capture logger output. No output is being captured. It is not clear what I am doing wrong. I have read the associated code and documents, and feel I have done a thorough search - all to no avail. I have chosen…
Bill Turner
  • 869
  • 1
  • 13
  • 27
0
votes
0 answers

Adding Tests with a JUnit Theory

Is there any way to use a rule to add additional tests? For example, I would like to convert the Theories runner to a rule, but I haven't been able to find a way to make Rules add additional tests.
Cobra1117
  • 1,150
  • 3
  • 11
  • 26
0
votes
1 answer

Mule: JUnit test case to call a service which is in middle of the Mule flow

I'm newbie for JUnit test case. Please help me on this issue. I have 2 mule flows- first flow having MQ as inbound and it has datamapper to transformer the xml. With the first flow input, i'm calling second flow where we are calling the existing…
star
  • 1,493
  • 1
  • 28
  • 61
0
votes
2 answers

How to set method level timeout for selenium test methods using @ tags. by overriding the framework level timeout

I need to override the existing framework level timeout set for all the test scripts as, @Rule public Timeout timeout; public String methodTimeout = null; The default timeout for all the tests is set as 10 mins (Each tests will run for max 10…
ParasuRam
  • 113
  • 1
  • 3
  • 13
0
votes
2 answers

@Rule declaration causes error on a debug attempt

A groovy JUnit test class has only one static declaration: @Rule public static ErrorCollector errorCollector; After an attempt to launch the test in debug mode an exception raises: java.lang.NullPointerException at…
Gangnus
  • 24,044
  • 16
  • 90
  • 149
0
votes
1 answer

How to print the value within the method in testcase?

I need to print the value within the method using the test case. My code is: public void testgetAllRatingElementsWeekNo() throws Exception { String METHOD_NAME = "testgetAllRatingElementsWeekNo"; log.entering(CLASS_NAME, METHOD_NAME); …