EvoSuite / evosuite

EvoSuite - automated generation of JUnit test suites for Java classes
http://www.evosuite.org
GNU Lesser General Public License v3.0
832 stars 341 forks source link

testcases- Evosuite #333

Closed ssk1216 closed 3 years ago

ssk1216 commented 3 years ago

Context

i used evosuite to generate test case for EvenODDmain.java. The idea was Evosuite should generate testcases to identify the odd or even numbers , import java.util.Scanner;

public class EvenOddMain {

public static void main(String[] args)  
  {  
     int num;
     Scanner input=new Scanner(System.in);
     num=Integer.parseInt(input.next());

      if(num % 2 == 0)

         System.out.println(num + " is even");
      else
        System.out.println(num + " is odd");
  }

}

Instead of generating testcases for even and odd numbers, I got the below results

@Test(timeout = 4000) public void test4() throws Throwable { SystemInUtil.addInputLine("9"); String[] stringArray0 = new String[5]; EvenOddMain.main(stringArray0); assertEquals(5, stringArray0.length); }

How to fix this ?

jose commented 3 years ago

Hi @ssk1216,

I failed to understand your question. For the following class:

import java.util.Scanner;

public class EvenOddMain {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = Integer.parseInt(input.next());
        input.close();

        if (num % 2 == 0) {
            System.out.println(num + " is even");
        } else {
            System.out.println(num + " is odd");
        }
    }
}

I get the following test suite with EvoSuite v1.1.0:

/*
 * This file was automatically generated by EvoSuite
 * Mon Dec 14 18:35:23 GMT 2020
 */

import org.junit.Test;
import static org.junit.Assert.*;
import static org.evosuite.runtime.EvoAssertions.*;
import java.util.NoSuchElementException;
import org.evosuite.runtime.EvoRunner;
import org.evosuite.runtime.EvoRunnerParameters;
import org.evosuite.runtime.util.SystemInUtil;
import org.junit.runner.RunWith;

@RunWith(EvoRunner.class) @EvoRunnerParameters(mockJVMNonDeterminism = true, useVFS = true, useVNET = true, resetStaticState = true, separateClassLoader = true) 
public class EvenOddMain_ESTest extends EvenOddMain_ESTest_scaffolding {

  @Test(timeout = 4000)
  public void test0()  throws Throwable  {
      String[] stringArray0 = new String[5];
      SystemInUtil.addInputLine("9");
      EvenOddMain.main(stringArray0);
      assertEquals(5, stringArray0.length);
  }

  @Test(timeout = 4000)
  public void test1()  throws Throwable  {
      String[] stringArray0 = new String[5];
      // Undeclared exception!
      try { 
        EvenOddMain.main(stringArray0);
        fail("Expecting exception: NoSuchElementException");

      } catch(NoSuchElementException e) {
         //
         // no message in exception (getMessage() returned null)
         //
         verifyException("java.util.Scanner", e);
      }
  }

  @Test(timeout = 4000)
  public void test2()  throws Throwable  {
      SystemInUtil.addInputLine("mKOEE*");
      String[] stringArray0 = new String[0];
      // Undeclared exception!
      try { 
        EvenOddMain.main(stringArray0);
        fail("Expecting exception: NumberFormatException");

      } catch(NumberFormatException e) {
         //
         // For input string: \"mKOEE*\"
         //
         verifyException("java.lang.NumberFormatException", e);
      }
  }

  @Test(timeout = 4000)
  public void test3()  throws Throwable  {
      String[] stringArray0 = new String[5];
      SystemInUtil.addInputLine("6");
      EvenOddMain.main(stringArray0);
      assertEquals(5, stringArray0.length);
  }

  @Test(timeout = 4000)
  public void test4()  throws Throwable  {
      SystemInUtil.addInputLine("-1");
      String[] stringArray0 = new String[19];
      EvenOddMain.main(stringArray0);
      assertEquals(19, stringArray0.length);
  }
}

which does include a test case to exercise the 'odd' (e.g., test0) and a test case to exercise the 'even' (e.g., test3).

-- Best, Jose

ssk1216 commented 3 years ago

Thanks Jose, I got the idea, however assertEquals(19, stringArray0.length); what's the need , won't it be always true?

jose commented 3 years ago

assertEquals(19, stringArray0.length); what's the need , won't it be always true?

Yes, I guess.