【问题标题】:What's the use of the configuration in classpath for Eclipse project: "org.eclipse.jdt.junit.JUNIT_CONTAINER/4"Eclipse项目的classpath中的配置有什么用:“org.eclipse.jdt.junit.JUNIT_CONTAINER/4”
【发布时间】:2013-06-02 08:11:23
【问题描述】:

我已经完成了一个单元测试类,但是当我运行它时,它返回一个异常。以下是我的测试用例:

import java.util.Date;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.rule.PowerMockRule;

import com.tibco.plugin.hl7.utils.EnvUtils;

import static org.easymock.EasyMock.expect;
import static org.junit.Assert.*;
import static org.powermock.api.easymock.PowerMock.expectNew;

import powermock.exception.A;
import powermock.exception.AException;

@RunWith(PowerMockRunner.class)
@PrepareForTest({A.class})
public class PowerMockRuleTest {

  @Rule
  public ExpectedException exception = ExpectedException.none();
// 

    @Test
    public void testException() throws Exception{
        exception.expect(AException.class);
        A a = new A();
        a.sayA();
    }

     @Test
    public void testMockB() throws Exception{

        A b = PowerMock.createMock(A.class);
        expect(b.sayB()).andReturn("c");
        PowerMock.replay(b);
        assertEquals(b.sayB(), "c");
        simulateCurrentTime();

    }
}

以下是异常的一部分:

java.lang.ClassCastException: org.junit.rules.ExpectedException cannot be cast to org.junit.rules.MethodRule

    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:79)
...

然后我新建一个java项目并将相关的java代码和测试用例以及所有相关的库复制到新项目中,重新运行测试用例,它工作正常! 然后我使用“Beyond Compare”比较了两个类路径文件的差异,发现了以下差异:

我已经用红线标记了差异并带有一些描述。

然后我将内容“<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>”复制到第一个项目中,单元测试就可以运行成功了!

这让我很困惑,这个配置在classpath中有什么用?我应该每次都手动复制吗?如果是这样,这不是一份快乐的工作:-(

【问题讨论】:

    标签: java eclipse junit powermock


    【解决方案1】:

    您的项目需要使用 JUnit 库来运行 JUnit 测试。 CLASSPATH 中的条目指定了这些库的位置;在这种情况下,它是来自 Eclipse 的 JUnit 4。您可以使用不属于 Eclipse 的其他版本(例如 3)或库。

    要添加这个库,您可以使用 Eclipse UI。打开项目属性,转到 Java Build Path,然后打开 Libraries 选项卡。然后单击添加库,您将获得一个库列表,您可以在其中选择 JUnit。每次使用 JUnit 测试创建新项目时都必须这样做。

    【讨论】:

    • 感谢您的评论,但实际上,类路径已包含配置“”(对不起屏幕-问题中的镜头不够清晰),因此它可以运行正常的测试用例,但使用 PowerMock 失败。不同的是第一个项目没有“”,第二个包含这个有它。
    猜你喜欢
    • 2011-03-21
    • 2011-11-03
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 2010-12-08
    相关资源
    最近更新 更多