Trying to run junit test on service level of Spring WebFlow Project. Assume$AssumptionViolatedException

大憨熊 提交于 2019-11-29 08:04:07

This is a shot in the dark - but I found this on spring's forum. It is consistent with your configuration - you are using Spring-test v2.5 and Junit 4.7, it looks like spring-test v2.5 might only support Junit v4.4.

Try to downgrade to JUnit 4.4 or upgrade to spring-test v2.5.6 and see if the problem goes away.

Seboudry

I have to keep Spring v2.5, downgrading to JUnit 4.4 resolves the problem.

Moreover, you'll see original cause hidden by Assume$AssumptionViolatedException ;)

FYI, Moving to latest spring-test 2.5.6 won't change anything.

Linked SO : Junit4 + Spring 2.5 : Asserts throw "NoClassDefFoundError"

I had to upgrade my version of spring-test and junit both to the most updated versions and bang everything is working great!

I found that spring-test 2.5.x expects the org.junit.Assume to contain AssumptionViolatedException, but in junit 4.10 it has been moved to the org.junit.internal package. The tests will stop running (not fail) with ClassNotFoundException.

My workaround was to add my own JUnit Assume copy based on 4.10 and add the AssumptionViolatedException (from internal). This local copy must be added to the classpath first is your IDE and build tool.

public class Assume {

    /*
     * Ported this from JUnit 4.4 to satisfy spring-test
     */
    public static class AssumptionViolatedException extends org.junit.internal.AssumptionViolatedException {
        private static final long serialVersionUID = 1L;

        public AssumptionViolatedException(Object value, Matcher<?> matcher) {
            super(value, matcher);
        }
    }
    // rest of Assume from JUint 4.10 ....
}

The disadvantage is that this class will obviously get stale quickly and future users of the code may not know it exists; it's a future failure waiting to happen.

Also it might not get loaded first. To mitigate this I added tests that check the Assume is loaded first and warn if not:

import java.net.URL;

public class JUnitSpringTestAssumeOverrideCompatibilityChecker {
    public static void checkAssumeOverridden() {
        URL url = Thread.currentThread().getContextClassLoader().getResource("org/junit/Assume.class");

        if (!"file".equals(url.getProtocol())) {
            throw new RuntimeException("Expect local override (1st on classpath) of JUnit 4.10 Assume for spring-test 2.5 compatibility");
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!