nike
nike

Reputation: 13

JUnit Testcase not executing properly

I am trying to execute a simple testcase using JUnit, but the testcase always passes even in false condition. I am not able to figure out the mistake in my program below. It looks like somewhere there is a mistake with the Scanner class multiple inputs.

I would like to test with multiple inputs to the testcase such that the test case accepts different employee types.

import java.util.Scanner;

import static org.junit.Assert.assertEquals;

public class EmployeeIdentificationTest extends TestCase {
    String firstName, middleName,lastName;
       int age=0;

       String choice=null;

       // assigning the values
       protected void setUp(){
          do{
            Scanner scanner = new Scanner(System.in);

           System.out.println("Enter  firstName  ");
           firstName= scanner.nextLine();
           System.out.println("Enter middleName");
           middleName= scanner.nextLine();
           System.out.println("Enter lastName");
           lastName= scanner.nextLine();
           System.out.println("Enter the age");
           int flag=0; 
           while(flag==0){
            try{
               age= scanner.nextInt();
               flag=1;
               }
               catch(Exception e){
               scanner.nextLine();
                System.out.println("Wrong entry, please enter digits only:");
            }
          }

        System.out.println("Do you like to fetch more records press Yes or No");
       scanner.nextLine();   
       choice=scanner.nextLine();
       }while(choice.contains("Y")||choice.contains("y"));      
       }
       // test method 
       public void testEmployee(){
       String employeeType=EmployeeIdentification.getEmployeeType(firstName, middleName, powerTrain, age);
       if(employeeType.equals("Junior Developer")||employeeType.equals("Senior Developer")||employeeType.equals("System Analyst")||employeeType.equals("Manager")||employeeType.equals("Delivery Head"))
       assert(true);
       }


}

Upvotes: 0

Views: 77

Answers (3)

Max
Max

Reputation: 129

Valid points from Ldvg, I think what you mean to write is:

public void testEmployee(){
    String employeeType = EmployeeIdentification.getEmployeeType(firstName, middleName, powerTrain, age);
    if(employeeType.equals("Junior Developer")||
        employeeType.equals("Senior Developer")||
        employeeType.equals("System Analyst")||
        employeeType.equals("Manager")||
        employeeType.equals("Delivery Head")) {
        assert(true);
    } else {
        assert(false);
    }
}

Upvotes: 1

shark
shark

Reputation: 117

JUnit checks for various asserts to pass/fail a test, so if you have

assert(true);

it will always pass.

assert(false);

it will allways fail.

use the conditions you need to test within assert. i.e.

assert(employeeType.equals("Junior Developer"));

Upvotes: 0

Ldvg
Ldvg

Reputation: 64

assert(true);

This line will always return true. The assert keyword will evaluate the expression given after it. You have to assert that the employeeType that the user selected is one of which you accept(i guess you keep them in a list).

A usecase for assert could look like this:

assert(a+b == c);

if you wish to verify that the result of a+b is equal to c.

Upvotes: 0

Related Questions