0

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

3 Answers 3

1

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); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

This does not work but the assertTrue works fine. Now the multiple input test cases is not working. It works only if i give no
Try different assert options: assert statement options
Problem: you are trying to get multiple inputs, but you are not using the proper data structure to hold these input. Suggestion: create a List<Employee> object to hole these inputs.
0
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.

10 Comments

From where do you call your test method testEmployee()?
Have you tried exactly this line: assert(emplooyeeType.equals("Junior Developer"));
Try adding the @Test tag above your method.
It executed fine now. but the multiple inputs is not working.
It only works for condition No but when i give yes its not checking the condition its taking input
|
0

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

2 Comments

still if i remove the assert(true), with only employeeType.equals("Junior Developer"), the test case passes even with wrong input
try assertTrue(employeeType.equals("Junior Developer"));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.