BAEL 1861 (#4913)
* BAEL-1861 Replaced real tests with demo test "placeholders" * BAEL-1861 Moved code from new module into existing ones * BAEL-1861 Renamed main() classes to not violate PMD rules
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.runfromjava;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class FirstUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenThis_thenThat() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSomething_thenSomething() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSomethingElse_thenSomethingElse() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.runfromjava;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({ FirstUnitTest.class, SecondUnitTest.class })
|
||||
public class MyTestSuite {
|
||||
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.runfromjava;
|
||||
|
||||
import junit.extensions.ActiveTestSuite;
|
||||
import junit.extensions.RepeatedTest;
|
||||
import junit.framework.JUnit4TestAdapter;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.junit.internal.TextListener;
|
||||
import org.junit.runner.JUnitCore;
|
||||
import org.junit.runner.Result;
|
||||
import org.junit.runner.notification.Failure;
|
||||
|
||||
public class RunJUnit4TestsFromJava {
|
||||
|
||||
public static void runOne() {
|
||||
JUnitCore junit = new JUnitCore();
|
||||
junit.addListener(new TextListener(System.out));
|
||||
junit.run(FirstUnitTest.class);
|
||||
}
|
||||
|
||||
public static void runAllClasses() {
|
||||
JUnitCore junit = new JUnitCore();
|
||||
junit.addListener(new TextListener(System.out));
|
||||
|
||||
Result result = junit.run(FirstUnitTest.class, SecondUnitTest.class);
|
||||
|
||||
for (Failure failure : result.getFailures()) {
|
||||
System.out.println(failure.toString());
|
||||
}
|
||||
|
||||
resultReport(result);
|
||||
}
|
||||
|
||||
public static void runSuiteOfClasses() {
|
||||
JUnitCore junit = new JUnitCore();
|
||||
junit.addListener(new TextListener(System.out));
|
||||
Result result = junit.run(MyTestSuite.class);
|
||||
|
||||
for (Failure failure : result.getFailures()) {
|
||||
System.out.println(failure.toString());
|
||||
}
|
||||
|
||||
resultReport(result);
|
||||
}
|
||||
|
||||
public static void runRepeated() {
|
||||
Test test = new JUnit4TestAdapter(SecondUnitTest.class);
|
||||
RepeatedTest repeatedTest = new RepeatedTest(test, 5);
|
||||
|
||||
JUnitCore junit = new JUnitCore();
|
||||
junit.addListener(new TextListener(System.out));
|
||||
|
||||
junit.run(repeatedTest);
|
||||
}
|
||||
|
||||
public static void runRepeatedSuite() {
|
||||
TestSuite mySuite = new ActiveTestSuite();
|
||||
|
||||
JUnitCore junit = new JUnitCore();
|
||||
junit.addListener(new TextListener(System.out));
|
||||
|
||||
mySuite.addTest(new RepeatedTest(new JUnit4TestAdapter(FirstUnitTest.class), 5));
|
||||
mySuite.addTest(new RepeatedTest(new JUnit4TestAdapter(SecondUnitTest.class), 3));
|
||||
|
||||
junit.run(mySuite);
|
||||
}
|
||||
|
||||
public static void resultReport(Result result) {
|
||||
System.out.println("Finished. Result: Failures: " +
|
||||
result.getFailureCount() + ". Ignored: " +
|
||||
result.getIgnoreCount() + ". Tests run: " +
|
||||
result.getRunCount() + ". Time: " +
|
||||
result.getRunTime() + "ms.");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("\nRunning one test class:");
|
||||
runOne();
|
||||
|
||||
System.out.println("\nRunning all test classes:");
|
||||
runAllClasses();
|
||||
|
||||
System.out.println("\nRunning a suite of test classes:");
|
||||
runSuiteOfClasses();
|
||||
|
||||
System.out.println("\nRunning repeated tests:");
|
||||
runRepeated();
|
||||
|
||||
System.out.println("\nRunning repeated suite tests:");
|
||||
runRepeatedSuite();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.runfromjava;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SecondUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenSomething_thenSomething() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whensomethingElse_thenSomethingElse() {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user