Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In case you are using the latest JUnit you might @Rules to be a lot cleaner solution to your problem. Here is a sample;</p> <p>Imagine this is your application;</p> <pre><code>package org.zero.samples.junit; /** * Hello world! * */ public class App { public static void main(String[] args) { System.out.println(new App().getMessage()); } String getMessage() { return "Hello, world!"; } } </code></pre> <p>This is your test class;</p> <pre><code>package org.zero.samples.junit; import static org.junit.Assert.*; import org.junit.Rule; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { @Rule public RepeatRule repeatRule = new RepeatRule(3); // Note Rule @Test public void testMessage() { assertEquals("Hello, world!", new App().getMessage()); } } </code></pre> <p>Create a rule class like;</p> <pre><code>package org.zero.samples.junit; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; public class RepeatRule implements TestRule { private int repeatFor; public RepeatRule(int repeatFor) { this.repeatFor = repeatFor; } public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { for (int i = 0; i &lt; repeatFor; i++) { base.evaluate(); } } }; } } </code></pre> <p>Execute your test case as usual, just that this time your test cases will be repeated for the given number of times. You might find interesting use cases where in @Rule might really prove to be handy. Try creating composite rules, play around you surely will be glued.. </p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload