问题
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();
}
}
and the following is parts of the exception:
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)
...
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.
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 :-(
回答1:
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.
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.
来源:https://stackoverflow.com/questions/16960054/whats-the-use-of-the-configuration-in-classpath-for-eclipse-project-org-eclip