Junit测试布尔方法 [英] Junit testing for a boolean method
问题描述
我在下面的方法中编写测试用例时遇到问题: EvenNum(double)
I have problem writing a testcase to this method below: EvenNum(double)
public class OddEven {
/**
* @param args
*/
public boolean evenNum(double num)
{
if(num%2 == 0)
{
System.out.print(true);
return true;
}
else
{
System.out.print(false);
return false;
}
}
这是我写的测试用例但是我认为在这个测试用例中我有一个继承问题或逻辑问题。应该是一个非常简单的但无法弄清楚。这是我写的代码:
This is the testcase I wrote but I think I have an inheritance problem or a logical problem in this test case. Should be a very simple one but can't figure out. Here is the code I wrote:
import static org.junit.Assert.*;
import org.junit.Test;
public class OddEvenTest {
@Test
public void testEvenNum() {
boolean ans = true;
boolean val;
double num= 6;
val = OddEven.EvenNum(num) // cant inherit the method dont know why???
assertEquals(ans,val);
}
}
推荐答案
两件事:
-
您正在静态调用非静态方法。该方法应声明为静态:
You are invoking a non-static method statically. The method should be declared static:
public static boolean evenNum(double num){
。
。
。 }
public static boolean evenNum(double num) {
.
.
. }
您没有正确输入方法的名称。仔细看。还可以考虑将其重命名为更可读的内容,例如 isEven(...)
You didn't type the name of the method correctly. Look closely. Also consider renaming it something more readable like, isEven(...)
这篇关于Junit测试布尔方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!