英文:
Error running Java Unit Test. Don't manage to import packages related to annotation "@Test" and method "equalTo()"
问题
请帮助我。我正在尝试在Java代码中运行单元测试(作为Java培训的一部分运行的简单代码),但我无法成功导入与注解“@Test”和方法“equalTo()”相关的包,在AccountTest类中(请参见下面的代码)。
以下是设置信息:
- 我尝试使用IDE的“Eclipse Java Oxygen”和“Eclipse IDE for Java Developers - 2020-03”来运行此代码;
- 我已将JDK更新到13版本(我已更新PATH系统变量,添加文件目录“C:\Program Files\Java\jdk-13.0.2\bin”);
- 我正在使用JRE 1.8.0_241;
- 操作系统:Windows 7专业版;
单元测试代码(这里是问题所在),AccountTest类:
public class AccountTest {
@Test // Eclipse错误报告:“无法将Test解析为一种类型”。它没有提供导入与注解“@Test”相关的包的选项
public void onWithdrawalBalanceShouldBeReduced() {
Account account = new Account(200d);
account.withdraw(50d);
assertThat(account.getBalance(), is(equalTo(150d))); // Eclipse错误报告:“对于类型AccountTest,方法equalTo(Double)未定义”。它没有提供导入方法“equalTo()”相关的包的选项
}
}
Account类的代码:
public class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public void withdraw(double value) {
this.balance = this.balance + value;
}
public void deposit(double valor) {
this.balance = this.balance + valor;
}
public void setBalance(double saldo) {
this.balance = saldo;
}
public double getBalance() {
return balance;
}
}
Bank类的代码:
public class Bank {
public void deposit(Account account, double value) {
account.deposit(value);
}
public void doTransfer(Account account1, double value, Account account2) {
account1.withdraw(value);
account2.deposit(value);
}
}
英文:
Please help me. I am trying to run a unit test in a Java code (simple code I am running as part of Java trainning), but I don't manage to import the packages related to the annotation "@Test" and method "equalTo()" in AccountTest class (see the code below).
Here is the setup information:
- I have tried to run this code using IDE's "Eclipse Java Oxygen" and "Eclipse IDE for Java Developers - 2020-03";
- I have updated the JDK to version 13 (I have updated PATH system variable adding the file directory "C:\Program Files\Java\jdk-13.0.2\bin");
- I am using JRE 1.8.0_241;
- OS: Windows 7 Professional;
Code of Unit Test (here is the problem), AccountTest class:
public class AccountTest {
@Test // Eclipse error reported: "Test cannot be resolved to a type". It does not offer to import the package related to the annotation "@Test"
public void onWithdrawalBalanceShouldBeReduced() {
Account account = new Account(200d);
account.withdraw(50d);
assertThat(account.getBalance(), is(equalTo(150d))); // Eclipse error reported: "The method equalTo(Double) is undefined for the type AccountTest". It does not offer to import the package related to method "equalTo()"
}
}
Code for Account Class:
public class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public void withdraw(double value) {
this.balance = this.balance + value;
}
public void deposit(double valor) {
this.balance = this.balance + valor;
}
public void setBalance(double saldo) {
this.balance = saldo;
}
public double getBalance() {
return balance;
}
}
Code for Bank Class:
public class Bank {
public void deposit(Account account, double value) {
account.deposit(value);
}
public void doTranference(Account account1, double value, Account account2) {
account1.withdraw(value);
account2.deposit(value);
}
}
答案1
得分: 0
你需要确保JUnit库位于你的构建路径上。
你的Eclipse版本内置了JUnit3、JUnit4和JUnit5库。
你可以尝试使用菜单命令创建一个新的JUnit测试案例:
File/New/JUnit Test Case
,然后选择以下其中一个选项:
- New Junit3 Test
- New Junit4 Test
- New Junit Jupiter Test
在测试类的名称(如果需要还有包名)中键入,然后点击 Finish
按钮,接下来会弹出一个对话框,让你添加适当的JUnit库到你的构建路径中。
点击 OK
按钮,然后你就可以添加/编辑你的测试类。
package my.task;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
class AccountTest {
@Test
public void onWithdrawalBalanceShouldBeReduced() {
Account account = new Account(200d);
account.withdraw(50d);
assertEquals("账户余额应减少到150", 150d, account.getBalance(), 0.001);
}
}
同时,请注意以下几点:
Account
中的withdraw
方法实际上没有减去取款金额,因此测试将失败:
java.lang.AssertionError: 账户余额应减少到150,预期值:150.0,实际值:250.0
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:555)
at my.task.AccountTest.onWithdrawalBalanceShouldBeReduced(AccountTest.java:14)
...
-
通常不建议使用浮点类型来表示余额,因为可能会导致意外的舍入误差
-
如果你真的需要使用
assertThat
和 Hamcrest 匹配器is()
和equalTo
,你需要修复导入部分:
package my.task;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Test;
public class AccountTest {
@Test
public void onWithdrawalBalanceShouldBeReduced() {
Account account = new Account(200d);
account.withdraw(50d);
assertThat(account.getBalance(), is(equalTo(150d)));
}
}
英文:
You need to make sure that the JUnit library is on your build path.
Your Eclipse version has built-in JUnit3, JUnit4, and JUnit5 libraries.
You should try to create a new JUnit test case using menu command:
File/New/JUnit Test Case
and select one of the following options:
- New Junit3 Test
- New Junit4 Test
- New Junit Jupiter Test
Type in the class name of your test (and package if needed), press Finish
button and you'll get to the dialog to add appropriate JUnit library to your build path.
Press OK
button and then you can add/edit your test class.
package my.task;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
class AccountTest {
@Test
public void onWithdrawalBalanceShouldBeReduced() {
Account account = new Account(200d);
account.withdraw(50d);
assertEquals("Account balance should be reduced to 150", 150d, account.getBalance(), 0.001);
}
}
Please also note the following points:
- method
withdraw
inAccount
does not actually subtract the withdrawal amount and the test will fail:
java.lang.AssertionError: Account balance should be reduced to 150 expected:<150.0> but was:<250.0>
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:555)
at my.task.AccountTest.onWithdrawalBalanceShouldBeReduced(AccountTest.java:14)
...
-
generally it is not recommended to use floating point type for balance because it may lead to unwanted rounding errors
-
if you really need to use
assertThat
, and hamcrest matchersis()
andequalTo
, you'll need to fix the imports:
package my.task;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Test;
public class AccountTest {
@Test
public void onWithdrawalBalanceShouldBeReduced() {
Account account = new Account(200d);
account.withdraw(50d);
assertThat(account.getBalance(), is(equalTo(150d)));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论