Running JUnit test classes from another JUnit test class

I have two classes that I am testing (let them name ClassA and ClassB). Each of them has its own JUnit test class (testClassA and testClassB, respectively).

ClassA relies on ClassB to function properly, so I want to make sure that ClassB passes its tests before running testClassA (otherwise the results from testClassA would be meaningless).

What is the best way to do this? In this case, this is for assignment, so if possible, I need to save it in the two specified test classes.

Can / should I throw an exception from testClassA if the tests of TestClassB are not all passed? This would require that testClassB run invisibly and simply report its success / failure testClassA, and not in the GUI (via JUnit).

I am using Eclipse and JUnit 4.8.1

Update: so far the best I have managed is a separate file with a set of tests, as shown below. This is still not quite what I need, since it still allows you to run ClassB tests if ClassA does not perform some tests.

import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({testClassA.class, testClassBCode.class}) public class testClassB { } 
+4
source share
2 answers

Testing your device should not be dependent on other tests.

One solution to your problem would be to mock ClassB in ClassA tests to test its behavior regardless of the results of ClassB tests.

I recommend you give mockito a try!

+9
source

You should try the testng framework, which allows you to do similar dependency checks.

0
source

Source: https://habr.com/ru/post/1304731/


All Articles