mercredi 26 août 2015

Webservice Integrationtest with Junit

The question aims to find a better solution than mine.

I have the following REQUIREMENTS for my integration tests:

  1. Each integration test i simplemented 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 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 com.lidl.eu.lisa.test.junit.test;

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...

Aucun commentaire:

Enregistrer un commentaire