Eclipse项目的类路径中的配置有什么用途:"org.eclipse.jdt.junit.JUNIT_CONTAINER/4"; [英] What's the use of the configuration in classpath for Eclipse project: "org.eclipse.jdt.junit.JUNIT_CONTAINER/4"
问题描述
我已经完成了一个单元测试类,但是当我运行它时,它会返回一个异常.以下是我的测试用例:
I have finished one unit test class, but when I run it, it returns with an Exception. the following is my test cases:
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代码和测试用例以及所有相关的库复制到新项目中,重新运行测试用例,一切正常! 然后我使用超越比较"比较了两个类路径文件的区别,发现了以下区别:
Then I create a new java project and copy the related java code and test cases and all the related libs to the new project, rerun the test case, it works fine! then I compared the the difference about the two classpath files using "Beyond Compare" and found the following difference:
我用一些描述用红线标记了差异.
I have marked the difference with red line with some description.
然后将内容"<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
"复制到第一个项目,单元测试可以成功运行!
then I copy the content "<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
" to the first project, the unit test can run successfully!
这让我非常困惑,在类路径中此配置的用途是什么?我应该每次手动复制吗?如果是这样,那将不是一件快乐的工作:-(
It makes me very confused, what's the use of this configuration in the classpath? should I manually copy it every time? If so, it is not a happy job :-(
推荐答案
您的项目需要使用JUnit库来运行JUnit测试. CLASSPATH中的条目指定这些库的位置.在这种情况下,它是Eclipse的JUnit 4.您可以使用其他版本(例如3)或不属于Eclipse的库.
Your project needs to use JUnit libraries to run JUnit tests. The entry in CLASSPATH specifies the location of these libraries; in this case it's JUnit 4 from Eclipse. You can use some other version (3, for example) or libraries which are not part of Eclipse.
要添加此库,可以使用Eclipse UI.打开项目属性,转到Java Build Path,然后打开Libraries选项卡.然后单击添加库",您将获得一个库列表,您可以在其中选择JUnit.每次使用JUnit测试创建新项目时,都必须这样做.
To add this library you can use Eclipse UI. Open project properties, go to Java Build Path, then open Libraries tab. Then click Add Library, you'll get a list of libraries where you can choose JUnit. You have to do it every time you create a new project with JUnit tests.
这篇关于Eclipse项目的类路径中的配置有什么用途:"org.eclipse.jdt.junit.JUNIT_CONTAINER/4";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!