eclipse JUnit测试用例中的初始化错误

yc0p9oo0  于 2022-11-23  发布在  Eclipse
关注(0)|答案(1)|浏览(143)

我使用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 Test Case时,我得到消息initializationError。我双击那个错误,我得到下面的消息:

我的失败跟踪如下所示:

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

我希望有人能帮助我。
顺祝商祺!

dbf7pr2w

dbf7pr2w1#

在项目属性中打开Java构建路径,“添加库”,然后添加JUnit 4库。您不需要直接添加任何JUnit jar。

相关问题