java 布尔方法的 Junit 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30004599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Junit test for boolean method
提问by Abdulla
how can I create a test method using asseertEquals for method Boolean isEqual(Semester semester) with Boolean type in class Semester?
我如何使用 assertEquals 为方法 Boolean isEqual(Semester semi(学期学期)) 创建一个测试方法,在类 Semester 中具有布尔类型?
here is the code for class Semester and the junit test for class Semester
这是 Semester 类的代码和 Semester 类的 junit 测试
Semester
学期
public class Semester {
private String year;
private String name;
public Semester() {
year = null;
name = null;
}
public Semester(String year, String name) {
this.year = year;
this.name = name;
}
public boolean isEqual(Semester semestr) {
return ((semestr.name.equals(this.name)) && (semestr.year
.equals(this.year)));
}
public String getYear() {
return year;
}
public String getName() {
return name;
}
}
SemesterTest
学期测试
public class SemesterTestCase1 {
private Semester semster=new Semester("2014","Fall");
@Test
public void testIsEqual() {
}
@Test
public void testGetYear() {
assertEquals("2014",semster.getYear());
}
@Test
public void testGetName() {
assertEquals("Fall",semster.getName());
}
}
回答by Shondeslitch
First, I would change your isEqual()
:
首先,我会改变你的isEqual()
:
public boolean isEqual(Semester semestr) {
boolean flag = false;
if (this.name != null && this.year != null
&& this.semestr.name != null && this.semestr.year != null ){
flag = ((semestr.name.equals(this.name)) && (semestr.year
.equals(this.year)));
}
return flag;
}
Try with assertTrue instead of assertEquals:
尝试使用 assertTrue 而不是 assertEquals:
@Test
public void testIsEqual() {
Semester semsterTest = new Semester("2014","Fall");
boolean res = this.semster.isEqual(semsterTest);
assertTrue(res);
}
I recommend you using assertTrue
and assertFalse
when you want testing boolean methods.
我建议您在需要测试布尔方法时使用assertTrue
和assertFalse
。
回答by Makoto
There are three things to address with this code.
这段代码需要解决三件事。
First, there already exists a method in which two objects can compare equivalent with each other called equals
. You want to override that method instead of creating your own equality method.
首先,已经存在一种方法,其中两个对象可以相互比较等效,称为equals
。您想覆盖该方法而不是创建自己的相等方法。
@Override
public boolean equals(Object otherSemester) {
// your code here
}
Second, your method is not null tolerant. If one were to pass null
to that method, you'd get a wonderful NullPointerException
. This needs to be addressed by ensuring that the object you've passed in is not null.
其次,你的方法不是空容忍的。如果要传递null
到该方法,您会得到一个美妙的NullPointerException
. 这需要通过确保您传入的对象不为空来解决。
if(semester != null) {
// logic based on semester being non-null
} else {
return false;
}
Lastly, your test could do with a bit of a rewrite. You'd want to define several cases:
最后,您的测试可以进行一些重写。您想定义几种情况:
- Equal semesters
- Not equal semesters
- Null
- Non-null but non-inflated semesters (somehow passing
null
to the constructor)
- 等学期
- 不等学期
- 空值
- 非空但非膨胀的学期(以某种方式传递
null
给构造函数)
While there are too many cases for me to enumerate here, they can be tested with Hamcrest matchers.
虽然这里有太多案例无法一一列举,但可以使用Hamcrest 匹配器进行测试。
assertThat(semester.equals(otherSemester), is(false));
回答by JIM
For Boolean testing in Junit it would be better to use assertTrue / assertFalse
对于 Junit 中的布尔测试,最好使用 assertTrue / assertFalse
@Test
public void testIsEqual() {
assertTrue(isEqual(semester)); // to check value is true
}
回答by wsl
You can use assertj fluent assertions which will do the job:
您可以使用 assertj 流畅的断言来完成这项工作:
assertThat(boolean_expression).isTrue()
assertThat(boolean_expression).isFalse()
assertThat(boolean_expression).isEqualTo(another_boolean_expression)
回答by salmanbw
Instead of trying assertEquals, you can try assertTrue.
您可以尝试 assertTrue,而不是尝试 assertEquals。
@Test
public void testIsEqual() {
assertTrue(isEqual(semester));
}
assertTrue is specially used to check boolean values.
assertTrue 专门用于检查布尔值。