Question

I have a parameterized test case, just like a bunch of other test cases I have in the testing suite, that I have been trying to add the them.

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);
}

I have a few test cases with nearly identical structure that test different methods in the same directory and package. All my other tests run and pass 100% except this test case throws the this stack trace:

java.lang.NoClassDefFoundError: Could not initialize class com.example.TimeMachineEventEndTimeTest at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:186) at org.apache.maven.surefire.report.SmartStackTraceParser.getClass(SmartStackTraceParser.java:63) at org.apache.maven.surefire.report.SmartStackTraceParser.(SmartStackTraceParser.java:53) at org.apache.maven.surefire.common.junit4.JUnit4StackTraceWriter.smartTrimmedStackTrace(JUnit4StackTraceWriter.java:72) at org.apache.maven.surefire.booter.ForkingRunListener.encode(ForkingRunListener.java:328) at org.apache.maven.surefire.booter.ForkingRunListener.encode(ForkingRunListener.java:312) at org.apache.maven.surefire.booter.ForkingRunListener.toString(ForkingRunListener.java:258) at 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) at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:41) at org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:97) at org.junit.internal.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) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)

Was it helpful?

Solution 2

If anyone else runs into a similar problem, it seems that for me it was my last few lines where I create some Events. They had a small error in the JSON, which must cause Gson to error when constructing them.

 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);

The correction was to remove the "," after the "duration" values. It seems that the stacktrace was extremely cryptic and pretty misleading, indeed.

OTHER TIPS

Could you run the test from your IDE? It looks like the stacktrace is misleading because of https://jira.codehaus.org/browse/SUREFIRE-962

It would be helpful, if you could provide a runnable gist, which reproduces the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top