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

Not able to use some Junit classes properly

I want to use ErrorCollector class in jUnit but not able to import its required class. I want to import org.junit.rule.* but instead of that i get option for importing import sun.org.mozilla.javascript.internal.ast.ErrorCollector. I do not…
Jak
  • 1
  • 4
-2
votes
1 answer

how to write Junit test case for testing usage of a Google api(Directory api)

Currently the code starts like this Directory directory = DirectoryServiceFactory.getDirectoryService(); directory.groups().get(someEmail).execute(); I am not sure how to write test case for it or should i even write a test case. What exactly it…
saurabh jain
  • 167
  • 1
  • 3
  • 12
1 2 3 4 5
6