开发者

Junit testing for a boolean method

I have problem writing a testcase to this method开发者_Go百科 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 have a number of issues:

  • you are attempting to call a non-static method statically
  • method names in java are case sensitive and you've mixed up the case.

I corrected some things for you and just verified the code below:

OddEven.java:

public class OddEven {

        public boolean evenNum(double num)
        {
            if(num%2 == 0)
            {
                System.out.print(true);
                return true;
            }
            else
            {
                System.out.print(false);
                return false;
            }

        }
}

OddEvenTest.java

import static org.junit.Assert.*;
import org.junit.Test;

public class OddEvenTest {

    @Test
    public void testEvenNum() {
        boolean ans = true;
        boolean val;
        double num = 6;
        OddEven oddEven = new OddEven();

        val = oddEven.evenNum(num);
        assertEquals(ans,val);
    }

}

Assuming the calls to System.out.println() in OddEven are strictly for debugging, the whole thing could be collapsed down to:

OddEven.java

public class OddEven {
    public boolean evenNum(double num) {
        return num%2 == 0;
    }
}

OddEvenTest.java

import static org.junit.Assert.*;
import org.junit.Test;

public class OddEvenTest {

    @Test
    public void testEvenNum() {
        OddEven oddEven = new OddEven();
        assertTrue(oddEven.evenNum(6));
        assertFalse(oddEven.evenNum(5));
    }
}

The code is now shorter and the unit test even covers an odd case for good measure.


Two things :

  • You are invoking a non-static method statically. The method should be declared static:

    public static boolean evenNum(double num) {

    }

  • You didn't type the name of the method correctly. Look closely. Also consider renaming it something more readable like, isEven(...)


This seems like testing gone mad to me, and programming gone mad too. All the method does is evaluate num % 2 == 0. You may as well just code that everywhere required and throw away both the method and its tests. If you must keep the method, it relies on a mathematical identity, you don't need to test those. You may as well test 1+1==2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜