Find logical bugs in below program | Selenium Forum
M
Posted on 17/04/2016
The following code contains three serious logic bugs.
Unfortunately, the supplied test, which is intended to test the production code, passes.
What are the three bugs?
Why doesn't the test find them?
Describe, in words or code, the step-by-step logic for up to three tests - manual or automated
- that would have caught the bugs.

You can assume that any called functions whose source isn't provided will perform as their
names imply they would.

public ArrayList<Message> addRecipientToAllMessages(final ArrayList<Message> messages, final User currentUser, final User recipient)
{
ArrayList<Message> successfulmessages = new ArrayList<Message> ();
ArrayList<Message> failedMessages = new ArrayList<Message> ();
for (Message message : messages)
{
if (canRecievemessage(message, currentUser, recipient))
{
successfulmessages.add (message);
}
else
{
failedMessages.add (message);
}
}
if (successfulmessages.isEmpty())
{
recipientManager.addRecipient (currentUser, successfulmessages);

}
return failedMessages;
}
private boolean canRecievemessage (Message message, User currentUser, User recipient)
{
if (currentUser.equals(recipient) || currentUser.getHasPermissionToModifyRecipients())
{
return message.getAddRecipientAllowed (recipient);
}
return true;
}


public void TestAddRecipients ()
{
User user = new User();
user.name = "Admin";
user.setHasPermissionToModifyRecipients(true);
Message goodMessage = new Message ("Test 1");
Message badMessage = new Message ("Test 2");
goodMessage.setAddRecipientAllowed (user, true);
badMessage.setAddRecipientAllowed (user, false);
ArrayList<Message> messages = new ArrayList<Message> ();
messages.add (goodMessage);
messages.add (badMessage);
// Assert that we pass two Messages into the function
Assert.assertEquals(messages.size(), 2);
ArrayList<Message> result = rec.addRecipientToAllMessages(messages, user, user);
// Assert that only one Message failed to be updated
Assert.assertEquals(result.size(), 1);
}

M
Replied on 18/04/2016

send your code in java file.