JUnit测试方法没有执行

时间:2014-06-24 06:59:57

标签: java eclipse java-ee junit

我有一个DateAdapter类,其中将日期类型编组为String类型,并通过2种方法mrashall和unmarshall将字符串数据类型解组为Date类型。所以我的测试方法没有运行。

public class DateAdapter extends XmlAdapter<String, LocalDate> {

private static final String FORMAT = "yyyy-MM-dd";

/**
 * Java Type => XML
 * 
 * @param date unmappable Java object (date)
 * @return desired XML representation
 * @throws Exception
 */
public String marshal(LocalDate date) throws Exception {
    if (date == null) {
        return null;
    }

    return date.toString(FORMAT);
}

/**
 * String Type => Java Type
 * 
 * @param dateString String needs to parse into java type
 * @return desired Java Type representation
 * @throws Exception
 */

@Override
public LocalDate unmarshal(String dateString) throws Exception {

    if (StringUtils.isNotBlank(dateString)) {

        DateTimeFormatter formatter = DateTimeFormat.forPattern(FORMAT);  
        DateTime dateTime = formatter.parseDateTime(dateString);  

        return dateTime.toLocalDate();            
    }
        return null;
    }

}

我正在使用Junit,testNG,maven 2.2和eclipse Juno。右键单击以测试不能执行测试方法的案例。

public class DateAdapterTest {
        /* Date adapter is a class to marshall date type into String and unmarshall  
         viseversa.
        */

        DateAdapter dateAdapter;

    private static final String DATE_FORMAT = "2014-01-01";
    private static final LocalDate DATE = new LocalDate(2014, 01, 01);

    @BeforeClass
    public void setUp(){
        dateAdapter = new DateAdapter();
    }

    /*
     * @param Date Date type  
     * @return String String data type date
     * @throw Exception 
   */
    @Test
    public void testMarshal() throws Exception{

        String dateString = dateAdapter.marshal(DATE) ;
        assertEquals(DATE_FORMAT, dateString);      
    }

    /*
     * @param String String data type  
     * @return date LocalDate type date
     * @throw Exception 
    */
    @Test
    public void testUnmarshal() throws Exception{

        LocalDate date = dateAdapter.unmarshal(DATE_FORMAT);
        assertEquals(DATE , date);
}
}

JUnit测试结果:

java.lang.NoSuchMethodError: org.junit.runner.Request.classWithoutSuiteMethod(Ljava/lang/Class;)Lorg/junit/runner/Request;
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.createRequest(JUnit4TestMethodReference.java:31)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.<init>(JUnit4TestMethodReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:54)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:3)

首先,您必须决定使用哪个框架:JUnit或TestNG。一旦您决定检查您使用的@Test注释。我想您使用来自TestNG的@Test标记了测试并尝试使用JUnit运行测试。

答案 1 :(得分:1)

升级到Junit 4.4,这是一个老问题,现在应该修复。见http://www.java-tutorial.ch/software-testing/junit-error-classwithoutsuitemethod