Consider I have a class Tournament
with methods register()
and isAlreadyRegistered()
. Below is the sample code.
public class Tournament {
private boolean register(String teamName) {
if(!isAlreadyRegistered(teamName)) {
// register team
return True;
}
return False;
}
private boolean isAlreadyRegistered(String teamName) {
// Check if team is already registered, involves DB calls
}
public static void main(String[] args) throws Exception {
Tournament tournament = new Tournament();
tournament.register("e-LEMON-ators");
}
}
I have a Java test-case which calls main method of class Tournament
, which leads to call to
register()
method and register()
method calls isAlreadyRegistered()
. Consider below code:
@Test
public void testTournament() {
try {
Tournament.main(args);
} catch (Exception e) {
fail();
}
}
I want to mock isAlreadyRegistered()
, maybe using Mockito, so it always returns True
Note: The example is only for demonstration purpose and I cannot modify the Tournament class. Modifications can only be made in Test case. Testing register()
separately is not an option (call has to be made through main method)
EDIT: I cannot create object for class Tournament
i.e. I can interact with the class only through main()
method