My test part is the following:
@Test
//testing user report method of UserAdmin - number of users less than 10
public void testuserReport_SizeLessThan10() throws Exception
{
new Expectations() {{
dBConnection.getUsers();
times=1;
result= Arrays.asList("Abc","123");
}};
System.out.println("in less than 10");
userAdmin.runUserReport();
}
The method under test belonging to a class named userAdmin is the following:
public void runUserReport() {
try {
List<User> users = dbConn.getUsers();
System.out.println(users.size());
if (users.isEmpty()) { // empty database
System.out.println("No users in database...");
} else if (users.size() <= 10) { // detailed reporting
System.out.println("Listing all usernames:");
for (User user : users) {
System.out.println(user.getUsername());
}
} else { // summary reporting
System.out.println("Total number of users: " + users.size());
System.out.println(users.get(0).getUsername());
System.out.println(users.get(1).getUsername());
System.out.println(users.get(2).getUsername());
System.out.println(users.get(3).getUsername());
System.out.println(users.get(4).getUsername());
System.out.println((users.size() - 5) + " more...");
}
} catch (SQLException sqle) {
System.out.println("DBConnection problem at runUserReport().");
}
}
My tests runs by giving the size of users as 2 but it does not print the usernames starting with "Listing all usernames:" as defined in the method. Am i defining the result wrongly in the expectations part of the test? Please help
I am not even sure how come System.out.println(users.size());
prints the size as 2 and not the test fails.
List<User> users = dbConn.getUsers();
says that users
is a List of User type while result= Arrays.asList("Abc","123");
makes result
as List of String, List<String>
. You are assigning List<String>
to List<User>
and somehow it doesn't fail at run time.
You need to prepare a List of User type and assign to result
instead of what you are doing currently.