使用不良数据类型的Junit5测试
为了学习Junit测试案例 我想写一个简单的junit5测试用例,我得到 错误
我的1类
简单课程
public class Med {
private String name;
private double price;
private int stock;
public Med(String p_name,double p_price,int p_stock) {
this.name = p_name;
this.price = p_price;
this.stock = p_stock;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return ("med name: " + this.getName() + ", med price: " + this.price + ", stock: " + this.getStock());
}
}
我的方法进入我的主
1简单方法
public static void f05DisplayStocks(Med[] p_arrMeds) {
System.out.println("f05DisplayStock");
System.out.println("stocks state");
for (Med med:p_arrMeds) {
System.out.println(med.getName() + ", " + med.getStock());
}
}
我的测试
一个简单的junit5测试
class TestPharmacie {
public Pharmacie ph01 = new Pharmacie();
@Test
public void testF05DisplayStocks() {
// ACT: Vars
Med med01 = new Med("medName01", 1.0, 1);
Med med02 = new Med("medName02", 2.0, 2);
Med [] meds = new Med[2];
meds[0] = med01;
meds[1] = med02;
// ARRANGE
// ASSERT
assertEquals(1, Pharmacie.f05DisplayStocks(meds[0].getStock()));
}
}
。 在这里,
java.lang.Error: Unresolved compilation problem:
The method f05DisplayStocks(Med[]) in the type Pharmacie is not applicable for the arguments (int)
我不知道如何修复我的测试
建议。
谢谢
RR
[编辑] 嗨,我阅读了您的评论和回答,并更改了Pharmacie类和Junit Test案例: 以下更改:
这两种语法,
public class Pharmacie {
private static Med[] med = new Med[2];
public static void main(String[] args) {
med[0] = new Med("Aspiron", 20.40, 4);
med[1] = new Med("Rhinoplexil",19.15, 5);
f05DisplayStocks(med);
}
public static int f05DisplayStocks(Med[] p_arrMeds) {
// System.out.println("f05DisplayStock");
// System.out.println("stocks state");
for (Med med:p_arrMeds) {
// System.out.println(med.getName() + ", " + med.getStock());
}
// System.out.println(med[0].getStock());
return med[0].getStock();
}
}
根据
class TestMed {
public Pharmacie ph01 = new Pharmacie();
public Med med01 = new Med("medName01", 1.0, 1);
public Med med02 = new Med("medName02", 2.0, 2);
public Med [] meds = new Med[] {med01, med02};
@Test
public void testF05DisplayStocks() {
// ACT: Vars
// ARRANGE
// ASSERT
System.out.println(meds[0].getStock());
// will send a NullPointerException cause:
// java.lang.NullPointerException: Cannot invoke "org.pharmacie02.Med.getStock()" because "org.pharmacie02.Pharmacie.med[0]" is null
// --> assertEquals(1, Pharmacie.f05DisplayStocks(meds));
// will send the fist error:
// The method f05DisplayStocks(Med[]) in the type Pharmacie is not applicable for the arguments (int)
// --> assertEquals(1, Pharmacie.f05DisplayStocks(meds[0].getStock()));
}
}
我遇到的错误或另一个语法,我都无法理解。
我认为您知道我是新手,所以感谢您的耐心和知识。 RR
In order to learn JUnit test cases
I want to write a simple JUnit5 test case and i get
an error
My class
1 easy class
public class Med {
private String name;
private double price;
private int stock;
public Med(String p_name,double p_price,int p_stock) {
this.name = p_name;
this.price = p_price;
this.stock = p_stock;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return ("med name: " + this.getName() + ", med price: " + this.price + ", stock: " + this.getStock());
}
}
my method into my main
1 easy method
public static void f05DisplayStocks(Med[] p_arrMeds) {
System.out.println("f05DisplayStock");
System.out.println("stocks state");
for (Med med:p_arrMeds) {
System.out.println(med.getName() + ", " + med.getStock());
}
}
My test
an easy JUnit5 test
class TestPharmacie {
public Pharmacie ph01 = new Pharmacie();
@Test
public void testF05DisplayStocks() {
// ACT: Vars
Med med01 = new Med("medName01", 1.0, 1);
Med med02 = new Med("medName02", 2.0, 2);
Med [] meds = new Med[2];
meds[0] = med01;
meds[1] = med02;
// ARRANGE
// ASSERT
assertEquals(1, Pharmacie.f05DisplayStocks(meds[0].getStock()));
}
}
the problem is into JUnit test, last line (// ASSERT section)
here the error msg
java.lang.Error: Unresolved compilation problem:
The method f05DisplayStocks(Med[]) in the type Pharmacie is not applicable for the arguments (int)
I don't know how to fix my test
Advices..?
Thank you
RR
[EDIT]
Hi, i read your comments and answer and made changes into Pharmacie class and JUnit test case:
Below changes:
Pharmacie class
public class Pharmacie {
private static Med[] med = new Med[2];
public static void main(String[] args) {
med[0] = new Med("Aspiron", 20.40, 4);
med[1] = new Med("Rhinoplexil",19.15, 5);
f05DisplayStocks(med);
}
public static int f05DisplayStocks(Med[] p_arrMeds) {
// System.out.println("f05DisplayStock");
// System.out.println("stocks state");
for (Med med:p_arrMeds) {
// System.out.println(med.getName() + ", " + med.getStock());
}
// System.out.println(med[0].getStock());
return med[0].getStock();
}
}
ant the JUnit test
class TestMed {
public Pharmacie ph01 = new Pharmacie();
public Med med01 = new Med("medName01", 1.0, 1);
public Med med02 = new Med("medName02", 2.0, 2);
public Med [] meds = new Med[] {med01, med02};
@Test
public void testF05DisplayStocks() {
// ACT: Vars
// ARRANGE
// ASSERT
System.out.println(meds[0].getStock());
// will send a NullPointerException cause:
// java.lang.NullPointerException: Cannot invoke "org.pharmacie02.Med.getStock()" because "org.pharmacie02.Pharmacie.med[0]" is null
// --> assertEquals(1, Pharmacie.f05DisplayStocks(meds));
// will send the fist error:
// The method f05DisplayStocks(Med[]) in the type Pharmacie is not applicable for the arguments (int)
// --> assertEquals(1, Pharmacie.f05DisplayStocks(meds[0].getStock()));
}
}
According to these both syntaxes i get the same error or another, different i'm not able to understand either.
i think you understood i'm newbie, so thank you for your patience and your knowledge.
RR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,assertequals方法检查第一个参数 1 是否等于第二个参数, pharmacie.f05displaystocks(meds [0] .getStock().getStock())
但是,函数f05displaystocks的返回类型为 void ,因此无法将其与assertequals第一个参数中的整数1进行比较。
其次,我还看到您已将f05displaystocks定义为参数为MED的阵列,但是在您传递的代码的“断言”部分中,仅MEDS [0] .getStock().getStock()),int值,而不是整个大批
Firstly, assertEquals method checks whether the first parameter, 1 , is equal to the second parameter, Pharmacie.f05DisplayStocks(meds[0].getStock())
However, the return type of the function f05DisplayStocks is void, so it cannot be compared with the integer 1 in the first parameter of the assertEquals.
Secondly, I also see that you have defined f05DisplayStocks to take in as parameter an array of Med, but in the ASSERT section of the code you are passing it only meds[0].getStock()), an int value and not the whole array