I can't seem to test this function out. If the value doesn't match the regex it would go to the else statement. But the else statement has a scanner so the JUnit class would ask to enter a valid value until it matches with the regex. Is there a way to stop the function before it goes to else?
public String addName(String name){
String setName = "";
while(true) {
if(name.matches("[a-zA-Z ]{1,30}")) {
setName = name;
break;
} else {
System.out.println("Please enter a valid name");
name = scan.nextLine();
}
}
return setName;
}
@Test
public void test(){
String name = "1001";
String setname = person.addName(name);
assertEquals(name,setname);
}