测试类上的Maven JUnit测试用例NoClassDefFoundException

时间:2013-07-27 19:50:19

标签: junit junit4 noclassdeffounderror parameterized surefire

我有一个参数化测试用例,就像我在测试套件中的一堆其他测试用例一样,我一直在尝试添加它们。

package com.example;

import com.google.gson.Gson;
import com.example.Event;
import com.example.LocalStorage;
import com.example.TimeMachine;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;
import java.util.Collection;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

@RunWith(Parameterized.class)
public class TimeMachineEventEndTimeTest {

    public TimeMachineEventEndTimeTest(Event event, String end_time) {
        _event = event;
        _end_time = end_time;
    }

    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
            { _UNSCHEDULED, "" },
            { _NOON_FOR_1_HOUR, "13:00" },
            { _NOON_FOR_HALF_HOUR, "12:30" },
            { _MIDNIGHT_FOR_1_HOUR, "1:00" },
            { _MIDNIGHT_FOR_HALF_HOUR, "0:30" },
            { _MIDNIGHT_FOR_12_HOUR, "12:00" }
        });
    }

    @Before
    public void initialize() throws Exception {
        _time_machine = new _TimeMachine();
    }

    @Test
    public void testEndTimeForEvent() throws Exception {
        assertThat(_time_machine.endTimeForEvent(_event)).isEqualTo(_end_time);
    }

    private TimeMachine _time_machine;
    private final Event _event;
    private final String _end_time;
    private static final Gson _gson = new _GsonProvider(mock(LocalStorage.class)).gson();
    private static final Event _UNSCHEDULED = _gson.fromJson("{}", Event.class);
    private static final Event _NOON_FOR_1_HOUR = _gson.fromJson("{\n" +
        "      \"starttime\" : \"12:00\",\n" +
        "      \"duration\" : \"60\",\n" +
        "    }", Event.class);
    private static final Event _NOON_FOR_HALF_HOUR = _gson.fromJson("{\n" +
        "      \"starttime\" : \"12:00\",\n" +
        "      \"duration\" : \"30\",\n" +
        "    }", Event.class);
    private static final Event _MIDNIGHT_FOR_1_HOUR = _gson.fromJson("{\n" +
        "      \"starttime\" : \"0:00\",\n" +
        "      \"duration\" : \"60\",\n" +
        "    }", Event.class);
    private static final Event _MIDNIGHT_FOR_HALF_HOUR = _gson.fromJson("{\n" +
        "      \"starttime\" : \"0:00\",\n" +
        "      \"duration\" : \"30\",\n" +
        "    }", Event.class);
    private static final Event _MIDNIGHT_FOR_12_HOUR = _gson.fromJson("{\n" +
        "      \"starttime\" : \"0:00\",\n" +
        "      \"duration\" : \"360\",\n" +
        "    }", Event.class);
}

我有一些测试用例几乎完全相同,可以在同一个目录和包中测试不同的方法。我的所有其他测试都运行并传递100%,除了这个测试用例抛出了这个堆栈跟踪:

  

java.lang.NoClassDefFoundError:无法初始化类com.example.TimeMachineEventEndTimeTest       at java.lang.Class.forName0(Native Method)       at java.lang.Class.forName(Class.java:186)       在org.apache.maven.surefire.report.SmartStackTraceParser.getClass(SmartStackTraceParser.java:63)       在org.apache.maven.surefire.report.SmartStackTraceParser。(SmartStackTraceParser.java:53)       在org.apache.maven.surefire.common.junit4.JUnit4StackTraceWriter.smartTrimmedStackTrace(JUnit4StackTraceWriter.java:72)       在org.apache.maven.surefire.booter.ForkingRunListener.encode(ForkingRunListener.java:328)       在org.apache.maven.surefire.booter.ForkingRunListener.encode(ForkingRunListener.java:312)       在org.apache.maven.surefire.booter.ForkingRunListener.toString(ForkingRunListener.java:258)       在org.apache.maven.surefire.booter.ForkingRunListener.testError(ForkingRunListener.java:131)       在org.apache.maven.surefire.common.junit4.JUnit4RunListener.testFailure(JUnit4RunListener.java:111)       在org.junit.runner.notification.RunNotifier $ 4.notifyListener(RunNotifier.java:100)       在org.junit.runner.notification.RunNotifier $ SafeNotifier.run(RunNotifier.java:41)       在org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:97)       在org.junit.internal.runners.ErrorReportingRunner.runCause(ErrorReportingRunner.java:57)       在org.junit.internal.runners.ErrorReportingRunner.run(ErrorReportingRunner.java:34)       在org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)       在org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)       在org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)

2 个答案:

答案 0 :(得分:1)

您可以从IDE运行测试吗?由于https://jira.codehaus.org/browse/SUREFIRE-962

,看起来堆栈跟踪具有误导性

如果你能提供一个可以重现问题的可运行的要点,那将会很有帮助。

答案 1 :(得分:0)

如果其他人遇到类似的问题,对我来说似乎是我最后几行创建了一些Event。他们在JSON中有一个小错误,在构造它们时必须导致Gson错误。

 private static final Event _NOON_FOR_1_HOUR = _gson.fromJson("{\n" +
    "      \"starttime\" : \"12:00\",\n" +
    "      \"duration\" : \"60\"\n" +
    "    }", Event.class);
private static final Event _NOON_FOR_HALF_HOUR = _gson.fromJson("{\n" +
    "      \"starttime\" : \"12:00\",\n" +
    "      \"duration\" : \"30\"\n" +
    "    }", Event.class);
private static final Event _MIDNIGHT_FOR_1_HOUR = _gson.fromJson("{\n" +
    "      \"starttime\" : \"0:00\",\n" +
    "      \"duration\" : \"60\"\n" +
    "    }", Event.class);
private static final Event _MIDNIGHT_FOR_HALF_HOUR = _gson.fromJson("{\n" +
    "      \"starttime\" : \"0:00\",\n" +
    "      \"duration\" : \"30\"\n" +
    "    }", Event.class);
private static final Event _MIDNIGHT_FOR_12_HOUR = _gson.fromJson("{\n" +
    "      \"starttime\" : \"0:00\",\n" +
    "      \"duration\" : \"360\"\n" +
    "    }", Event.class);

更正是在“持续时间”值之后删除“,”。看来堆栈跟踪非常神秘,而且确实具有误导性。