Why getting the AssumptionViolatedException? Junit 4.8.2

青春壹個敷衍的年華 提交于 2019-12-11 17:50:55

问题


I'm trying to make Junit tests. I want to start them by asking for a method in the CMS system. Because I'm testing plugins. The problem is that I get this exception and I don't know why. Naah I find that the problem could be that I'm using JUnit 4.8.2, but when I'm running the test in Eclipse everything worked fine. So I can't find the solution. Here is the error:

 org.apache.velocity.exception.MethodInvocationException: Invocation of method 
'getTest' in class nl.company.cms.three.viewtool.LoginViewTool threw exception 
java.lang.NoClassDefFoundError: org/junit/internal/AssumptionViolatedException at 
working/2a90929a-3fbf-43e9-9961-4a40279ec907_5c6e0bff-cfeb-44c6-86e2-
a0ba40e7b66c.field[line 1, column 15] 

Here is the code of my class and test class: Class that calls to start the Test:

    public String getTest(){
    Result r = org.junit.runner.JUnitCore.runClasses(MyTestClass.class);
    if(r.getRunCount() > 0){
    String s = "Failcount = " + r.getFailureCount() + " // " + 
    r.getRunCount() + " in " + r.getRunTime() + " ms";
    System.out.println(r.getFailures().get(0).getTrace());
    System.out.println("Runcount: "+r.getRunCount());
    System.out.println("Runtime: "+r.getRunTime());
    System.out.println("Ignore count: "+r.getIgnoreCount());
    System.out.println("Failure count: "+ r.getFailureCount());
    return s;
}
else{
    return "Something ging kei verkeerd jonge!";
}
}

Test class:

public class MyTestClass {

@Test
public void testMultiply() {
    CustomLoginViewTool tester = new CustomLoginViewTool();
     assertEquals("Result", 40, tester.multiply(10, 5));
}

@Test
public void testMultiply1() {
    CustomLoginViewTool tester = new CustomLoginViewTool();
     assertEquals("Result", 50, tester.multiply(10, 5));
}

@Test
public void testMultiply2() {
    CustomLoginViewTool tester = new CustomLoginViewTool();
     assertEquals("Result", "ASDF", tester.multiply(10, 5));
}

@Test
public void testMultiply3() {
    CustomLoginViewTool tester = new CustomLoginViewTool();
     assertEquals("Result", 50, tester.multiply(10, 5));
}

@Test
public void testMultiply4() {
    CustomLoginViewTool tester = new CustomLoginViewTool();
     assertEquals("Result", 47, tester.multiply(10, 5));
}

@Test
public void testMultiply5() {
    CustomLoginViewTool tester = new CustomLoginViewTool();
     assertEquals("Result", 50, tester.multiply(10, 5));
}
}

回答1:


Assumptions are exceptions that JUnit will catch but which won't fail the test. These are for "this test only makes sense if" kind of questions. There is no point to fail Windows-path tests on a Linux system, for example - they can't succeed and failing them will give you an error that you can't fix without disabling the tests.

What I find odd is Velocity and JUnit in a single error message. Why is Velocity running JUnit?

The error means that the classpath isn't set up correctly. So you need to look into the classloader which is used to load the class which contains the method getTest()




回答2:


The problem was that the CMS system uses JUnit 3.8.2 and the plugin needs JUnit 4.8.2. This caused the error because Java takes the newest version of JUnit and this has not the API of the AssumptionViolatedException.




回答3:


That class implements a hamcrest class, so make sure you have the hamcrest-core jar on the classpath.



来源:https://stackoverflow.com/questions/9874369/why-getting-the-assumptionviolatedexception-junit-4-8-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!