Eclipse项目的类路径中的配置有什么用处:“org.eclipse.jdt.junit.JUNIT_CONTAINER / 4”

时间:2013-06-06 10:43:15

标签: java eclipse junit powermock

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

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代码和测试用例以及所有相关的lib复制到新项目中,重新运行测试用例,它工作正常! 然后我使用“Beyond Compare”比较了两个类路径文件的差异,发现了以下区别: enter image description here

我用红线标记了一些描述的区别。

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

这让我非常困惑,这个配置在类路径中有什么用?我应该每次手动复制吗?如果是这样,那不是一件好事: - (

1 个答案:

答案 0 :(得分:0)

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

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

相关问题