I have written a JUnit test TestClass.test() and in a certain other Java program which sees the test class I want to execute a certain part of the code only if the JUnit test passes. How can I do that? Something like
if(JUnit.passes(TestClass.test)){
....
}
>Solution :
You can programmatically run the tests in a JUnit4 test suite and inspect the result with JUnitCore.runClasses:
if (JUnitCore.runClasses(MyTests.class).wasSuccessful()) {
System.err.println("Tests ran successfully.");
}
Or a single test with:
if (new JUnitCore().run(Request.method(MyTests.class, "myTest")).wasSuccessful()) {
System.err.println("Tests ran successfully.");
}