1

The question aims to find a better solution than mine.

I have the following REQUIREMENTS for my integration tests:

  1. Each integration test is implemented in a single test class.
  2. The methods in a test class have to be executed in order.
  3. Each method is one step of the integration/functional test.
  4. If a test method fails all other test methods in the test class have to be skipped - because it does not make sense to find subsequent errors.

MY SOLUTION looks like this:

I just inherit every test class from my abstract test class:

package myPackage;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestWatcher;

public abstract class FailFastTest {

    private final static List<Class<?>> FAILED_CLASSES = new CopyOnWriteArrayList<Class<?>>() ;

    private final Class<? extends FailFastTest> thatClass = this.getClass();

    @Rule
    public final TestWatcher onFailedRule = new TestWatcher() {
        protected void failed(Throwable e, org.junit.runner.Description description) {
            FAILED_CLASSES.add(thatClass);
        };
    };

    @Before
    public final void beforeMethod() {
        final boolean failed = FAILED_CLASSES.contains(thatClass);

        if(failed) {
            System.out.println(thatClass + " FAILED. Aborting test.");
            org.junit.Assume.assumeTrue(!failed);
        }
     }
}

Does anybody have a better idea? My PROBLEM: I cannot run such a test class multiple times in parallel because of the classname signals the skipping...

NO! It is not a duplicate! I try to write functional tests which use different (web-)services in a specific order. The Question "How do I test a servlet?" is quite different!

Anyway. I switched to TestNG. TestNG supports all requirements I have and works like charm. I do not need my glue code any longer!

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
eventhorizon
  • 2,977
  • 8
  • 33
  • 57
  • 1
    Could you use TestNG? It allows ordering of tests, making it simpler for you to achieve what you are looking for. – manish Aug 27 '15 at 08:39
  • @manish: I had the same idea (switching to TestNG - but I did not find good examples for my requirements - yet. Maybe I am too dumb to use google :-) If you have good documentations for functional testing web services with TestNG it would be very kind of you to tell me about them ;-) – eventhorizon Aug 27 '15 at 10:08
  • I think what you need the most is a way to order tests and to skip tests if any previous ones fail. Ordering is simply about adding `priority` to `@Test`. `@Test(priority = 1)` will run before `@Test(priority = 2)` and so on. Skipping is about adding `dependsOnMethods` to `@Test`. `@Test(priority = 1) public void first() {}`, `@Test(priority = 2, dependsOnMethod="first") public void second() {}` will ensure that `second` will be run only if `first` succeeds. You may simply be able to skip using `priority` if you use just `dependsOnMethod`. – manish Aug 27 '15 at 10:22
  • possible duplicate of [How do I Unit Test a servlet?](http://stackoverflow.com/questions/4137484/how-do-i-unit-test-a-servlet) – Raedwald Aug 27 '15 at 11:13
  • @Raedwald: IMHO it is not a duplicate. The initial question is quite differet and the solution will olso differ. – eventhorizon Aug 27 '15 at 11:41
  • @manish: THX! I willtry that out. What about parallel testing different test classes? – eventhorizon Aug 27 '15 at 11:41
  • The [official TestNG documentation](http://testng.org/doc/documentation-main.html#parallel-tests) is quite comprehensive and covers all you need about running tests in parallel. – manish Aug 27 '15 at 12:07
  • THX! Using TestNG from now on :-) – eventhorizon Aug 27 '15 at 18:08

0 Answers0