我使用eclipse并编写了以下代码:
package JUnitTestPackage;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
import JUnit_Test.BankAccount;
public class BankAccountTest {
@Test
public void testDeposit() {
BankAccount account = new BankAccount();
account.deposit(1000);
account.deposit(100);
assertTrue("Test Deposit", 1100 == account.getBalance());
}
@Test
@Ignore
public void testWithdraw() {
fail("Not yet implemented");
}
@Test
@Ignore
public void testGetBalance() {
fail("Not yet implemented");
}
}
所以,我第一次使用Eclipse和JUnit。所以,代码只是为了了解JUnit的工作原理。但是当我点击Run as - > JUnit测试用例,然后我收到消息initializationError
。我双击了那个错误,我收到了以下消息:
My Failure Trace如下所示:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
从之前的帖子(Eclipse JUnit - possible causes of seeing "initializationError" in Eclipse window),我遵循一些建议,但没有成功。例如,我尝试了以下步骤:
1. I have added harmcrest to my library
2. Go to Project -> Clean
我希望有人可以帮助我。
最好的问候,
答案 0 :(得分:0)
在项目属性上打开Java Build Path,&#34;添加库&#34;,然后添加JUnit 4库。你不应该直接添加任何JUnit罐子。