Junit Testing for a Boolean Method

Junit Testing for a Boolean Method

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);
    }

}

3 Answers

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;
            }

        }
}
James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.