1

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

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:无法在 org.apache.maven.surefire 的 java.lang.Class.forName(Class.java:186) 处的 java.lang.Class.forName0(Native Method) 处初始化类 com.example.TimeMachineEventEndTimeTest .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) at org.apache.maven.surefire.common.junit4.JUnit4RunListener.testFailure(JUnit4RunListener.java:111) at org.junit.runner.notification.RunNotifier$4.notifyListener(RunNotifier.java :100) 在 org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:97) 在 org.junit.internal 的 org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:41)。 runners.ErrorReportingRunner.runCause(ErrorReportingRunner.java:57) at org.junit.internal.runners.ErrorReportingRunner.run(ErrorReportingRunner.java:34) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java: 264) 在 org.apache.maven.surefire.junit4 的 org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)。JUnit4Provider.invoke(JUnit4Provider.java:124)

4

2 回答 2

1

你能从你的IDE运行测试吗?由于https://jira.codehaus.org/browse/SUREFIRE-962,看起来堆栈跟踪具有误导性

如果您可以提供可重现问题的可运行要点,那将很有帮助。

于 2013-07-27T22:44:10.440 回答
0

如果其他人遇到类似的问题,对我来说似乎是我在最后几行创建了一些Events。他们在 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);

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

于 2013-08-09T22:43:34.103 回答