please read all steps and also explain the answers
I also posted the code photos and text version
thank you
Part2
11. Go back to Eclipse. Now take a look at Message.java and Mailbox.java. The
Mailbox class manages a list of messages sent to a user. List and briefly
explain what each Mailbox method does (yes, you can look at the Javadoc
comments)
12. Create JUnit test class for Message. Make sure the setup() option is
checked. The methods you will be testing are getRecipient(), getSender,
getText, and toString() – select those in the dialog box.
13. Run MessageTester. It should fail.
14. Now, add code to the setup() method. This code will look like this
msgStr = “Test Message”;
recip = “Frank”;
sender =”Mary”;
msg = new Message(sender, recip, msgStr);
Also, add instance variables to MessageTester corresponding to the variables in the
code above.
15. Now, fill in the code that tests getRecipient(). The method is testGetRecipient.
The code should invoke the method getRecipient on the Message object
created in setup() – the object name is msg. It should test the value returned
by get Recipient to see if it is equal to the recipient stored in the instance
variable recip. Describe how this test case works.
16. Fill in the code that tests getSender and getText.
17. Fill in the code that tests toString(). This is trickier because toString() returns a
formatted string, and you need to write the test to check if it is the string you
expect. The way to do is to create string named expectedResult, and
assign to it the string you expect. Then, call toString() and test the result
against the expectedResult.
18. Run your tester, and make sure it works.
19. Now, you are going to build a tester for the Mailbox class. Create the unit test
class the same way as above. Make sure Setup is checked.
20. In the generated setup method, create instance of your mailbox.
box = new Mailbox(owner);
This will cause a fresh Mailbox object to be created before each test case. The
Mailbox reference should be an instance variable of the test class.
21. Now, create tests for each of the Mailbox methods. For example, to test
the isEmpty method, simply call isEmpty and test that it returns true, using
assertTrue.
public void testIsEmpty() {
assertTrue(box.isEmpty());
}
This works because a newly created Mailbox is empty. You should also
create test method that tests the return value of isEmpty when the Mailbox
is NOT empty (add a message to the Mailbox and then test the return value
of isEmpty).
22. To test getNextMessage, create message with known values, add it to the
mailbox, then retrieve the next message by calling getNextMessage and test
that the retrieved message has the same values as the one you added. You
should also test that the retrieved message was removed from the mailbox.
Q: If you add one message to an empty Mailbox and then retrieve it from the
Mailbox, what method should you call to make sure the message was
removed?
Raw Version –
MailBox —–
import java.util.ArrayList;
/**
An email mailbox.
*/
public class Mailbox {
private String user;
private ArrayList
/**
* Constructs a Mailbox object.
*
* @param u the user
*/
public Mailbox(String person) {
user = person;
messages = new ArrayList
}
/**
* Gets the user.
*
* @return the user of this mailbox
*/
public String getUser() {
return user;
}
/**
* Adds a message to the list.
*
* @param m the message to add
*/
public void addMessage(Message m) {
messages.add(m);
}
public boolean isEmpty() {
if (messages.size() == 0)
return true;
else
return false;
}
public Message getNextMessage() {
Message msg = messages.remove(0);
return msg;
}
}
Message ——
/**
A message has a sender, a recipient, and text
*/
public class Message {
private String sender;
private String recipient;
private String text;
/**
* Constructs a Message object.
*
* @param from the sender of the message
*/
public Message(String from, String recip, String txt) {
sender = from;
text = txt;
recipient = recip;
}
/**
* Gets the recipient.
*
* @return the recipient
*/
public String getRecipient() {
return recipient;
}
public String getSender() {
return sender;
}
/**
* return the text of the message
* @return text
*/
public String getText() {
return text;
}
public String toString() {
return “From: ” + sender + “\n” + “To: ” + recipient + “\n” + text + “*****************\n”;
}
}