Home > Software design >  Can assertEquals only be applicable for same data types?
Can assertEquals only be applicable for same data types?

Time:06-27

Can AssertEquals be applied for different data typed input and outputs ?When I have applied AssertEquals to different data typed input and output,the AssertEquals gets striked off..and even the successful test case are not passing ?

What I meant by different data typed input and outputs means..Say example,Consider a program to check whether a number is even or odd.The input to this program is a number which is 'int' data type and output to this program is a 'String' datatype,which prints the number is "even" or "odd".

Here is the code

package BasicTesting;

import java.util.*;
class EvenOdd
{
    
  public static void main(String args[])
  {
    System.out.println("Enter any integer to check whether its odd or even...");
    Scanner ob = new Scanner(System.in);
    int i;
    
    i = ob.nextInt();
    OddEven(i);
    
  }
  public static void OddEven(int i) {
      if(i%2==0)
        {
            System.out.println("You entered an even number");
        }
        else
        {
            System.out.println("You entered an odd number");
        }
  }
}

This is the JUnit test case I have tried to written...but there are some errors in it because of the different data types. package BasicTesting;

import static org.junit.Assert.*;

import org.junit.Test;

public class EvenOddTest {

    @Test
    public static void test() {
        
        

        // TODO Auto-generated method stub
        EvenOdd a=new EvenOdd();
        //int res=a.OddEven(10);
        assertEquals("You entered an even number",a.OddEven(10));
        
    }

}

Please help me to write parameterized JUnit test case.Hope you will help.

CodePudding user response:

I'm afraid your test is nonsensical. You can't compare the result of a call to OddEven to anything. It is a void method. It doesn't return anything.

(Presumably the "different types" the compiler is talking about are String and void or Void. But void not really a type at all. Or at least, not in any useful sense.)

Unfortunately, a method whose sole observable action is to write to standard output is difficult to write a unit test for. You would need to use a mocking framework and mock System.out. (Or use System.setOut so replace standard output with some stream that allows you to capture the output.)

A better idea (and a better design) is to have your evenOdd method NOT write to standard output. Instead, have it return a boolean, and let the caller write the message ... or do something else with the result.

CodePudding user response:

If you're trying to compare different data types, like int with long something like this -

Assert.assertEquals(23.374f, 23.374d)

You can wrap the values in BigDecimal and compare like this -

float f = 23.374f;
double  d = 23.374d;
BigDecimal decimal_f = new BigDecimal(Float.toString(f));
BigDecimal decimal_d = new BigDecimal(Double.toString(d));
assertEquals(decimal_f,decimal_d);

Note - Usage of BigDecimal will also add some overhead.

  • Related