I was wondering how to run some JUnit4 test inside a java program. Basically - depending on some conditions during runtime I need to decide which test runner to use. Using Junit3 I could override runTest method from TestCase class - but in JUnit4 tests do not extend TestCase class so I have nothing to override... Is there maybe some method that I need to implement... or sth else...
A:
Here's some sample code for you:
public void checkTests() throws org.junit.runners.model.InitializationError {
checkTestClass((Class<?>) MyTestClss.class);
}
private void checkTestClass(Class<?> theClass) throws InitializationError {
final RunNotifier notifier = new RunNotifier();
notifier.addListener(new RunListener() {
@Override
public void testFailure(Failure failure) throws Exception {
Assert.fail(failure.getMessage());
}
});
final Runner runner = new BlockJUnit4ClassRunner(theClass);
runner.run(notifier);
}
lexicore
2010-04-19 07:20:12
This looks promising. The problem I have is that I have to run a method (which cannot be static) before each test case... For example using JUnitCore runner. What is the best way to achieve this?
markovuksanovic
2010-04-19 10:06:12