【问题标题】:Junit Error when trying to run unit test尝试运行单元测试时出现 Junit 错误
【发布时间】:2013-01-01 04:16:46
【问题描述】:

我收到此错误:

   Class not found com.apache.camel.example.tests.ReportIncidentRoutesTest
java.lang.ClassNotFoundException: com.apache.camel.example.tests.ReportIncidentRoutesTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
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)

这是我收到的新错误的更新堆栈跟踪。在运行配置中,我确实有它指向这个类,所以我不确定为什么我仍然收到这个错误。

编辑:--------------------------------------------- -------------------------------------- 我的测试课的副本:

package com.apache.camel.example.tests;

import org.apache.camel.CamelContext;
import org.apache.camel.example.reportincident.InputReportIncident;
import org.apache.camel.example.reportincident.OutputReportIncident;
import org.apache.camel.example.reportincident.ReportIncidentEndpoint;
import org.apache.camel.example.reportincident.ReportIncidentRoutes;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.jvnet.mock_javamail.Mailbox;

import junit.framework.Test;
import junit.framework.TestCase;

/** * 对我们的路线进行单元测试 */

   public class ReportIncidentRoutesTest extends TestCase {

private CamelContext camel;

// should be the same address as we have in our route
private static String ADDRESS = "http://localhost:8080/part-five/webservices/incident";

protected void startCamel() throws Exception {
    camel = new DefaultCamelContext();
    camel.addRoutes(new ReportIncidentRoutes());
    camel.start();
}

protected static ReportIncidentEndpoint createCXFClient() {
    // we use CXF to create a client for us as its easier than JAXWS and works
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(ReportIncidentEndpoint.class);
    factory.setAddress(ADDRESS);
    return (ReportIncidentEndpoint) factory.create();
}

public void testRendportIncident() throws Exception {
    // start camel
    startCamel();

    // assert mailbox is empty before starting
    Mailbox inbox = Mailbox.get("blah@blah.com");
    assertEquals("Should not have mails", 0, inbox.size());

    // create input parameter
    InputReportIncident input = new InputReportIncident();
    input.setIncidentId("123");
    input.setIncidentDate("2008-08-18");
    input.setGivenName("Patrick");
    input.setFamilyName("joe");
    input.setSummary("Blah");
    input.setDetails("Blah blah");
    input.setEmail("blah@blah.com");
    input.setPhone("845 2962 7576");

    // create the webservice client and send the request
    ReportIncidentEndpoint client = createCXFClient();
    OutputReportIncident out = client.reportIncident(input);

    // assert we got a OK back
    assertEquals("0", out.getCode());

    // let some time pass to allow Camel to pickup the file and send it as an email
    Thread.sleep(3000);

    // assert mail box
    assertEquals("Should have got 1 mail", 1, inbox.size());

    // stop camel
    camel.stop();
}

}

【问题讨论】:

  • 如何运行单元测试?你如何构建你的发布 jar?
  • 我只是在做一个 eclipse Run as --> JUnit Test
  • 而我的发布 jar 将打包在部署到 tomcat 服务器的 WAR 文件中
  • 谢谢。尝试在我们的运行配置 Eclipse JUnit 运行器中为您的类检查默认运行器。还要确保有效的 JUnit jar 在您的类路径中以用于特定的运行配置。
  • 啊,是的,它似乎不在运行配置的类路径中,我现在得到一个不同的错误,说“找不到类”进行测试,即使我有明确的在运行配置中声明

标签: java junit classnotfoundexception


【解决方案1】:

编译时似乎 junit 3 在类路径中(假设您没有因任何使用 junit api 而出现编译错误),但在运行时却没有,因此涉及到一些类路径不匹配。

您似乎通过 eclipse 测试运行器运行测试,是否配置为与编写测试相同的 junit 版本?

【讨论】:

  • 是的,我之前遇到了一个不同的错误,我查看了运行配置,发现它设置为 JUNIT 4,所以我将其更改为 3,但随后开始出现此错误。跨度>
  • 我猜 eclipse 配置的 junit 3 .jar 文件路径不正确,但我不知道如何查找和修复该配置,抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2013-05-29
相关资源
最近更新 更多