Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if JUnit test passed

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading