please read all steps and also explain the answers
I also posted the code photos and text version
thank you
Part1
1. On Bitbucket, fork the project CUS1156Fall2022Lab3. Then go to your git
folder and clone it. Create project in Eclipse and link in the files from the git
folder.
2. Take a look at Counter.java to see what it does.
3. You are going to create simple JUnit test class for this class. Right click on
Counter, and choose New->JUnit Test Class.
4. In the dialog box, make sure that JUnit5 is selected. The default name of the
class will be CounterTest, which is fine. Make sure that the setup() option is
checked. Select the class you will be testing (Counter).
5. Click Next. On the next dialog box, choose the methods you want to test:
increment and decrement. Click Finish. You might get a message about
adding JUnit5 to the build path. Click OK.
6. What is the automatically generated code? Try running it and see what
happens.
7. Now, fill in the methods. Use the CalculatorTest code as a model. The setup()
method should create Counter object, and the testIncrement and
testDecrement methods should test to see if the code works. Run the unit
test. What do you see? How do you know if the tests worked or not?
8. Introduce an error into one of the methods and return the tests. Now what
happens? How do you know there is an error?
9. Since your CounterTest.java is a new file, created in Eclipse, it is located in
your Eclipse workspace, and Git doesn’t know about it. So when you have
finished your changes, copy the file from your Eclipse workspace to your lab3
project in your Git folder. Use this command to make Git aware of it (staging
the file)
git add CounterTest.java
Check that it was added by using
git status
If the file is listed, you are ready to commit.
10. Commit your code and push to Bitbucket.
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 –
Counter —-
/**
* A counter class. Maintains a number
* representing a count
* @author Bonnie MacKellar
*
*/
public class Counter {
private int count = 0;
/**
* increment count by one
*
* @return incremented count
*/
public int increment() {
count = count + 1;
return count;
}
/**
* decrement count by one
*
* @return decremented count
*/
public int decrement() {
count = count – 1;
return count;
}
public int getCount() {
return count;
}
}
CounterTest —–
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CounterTest {
@BeforeEach
void setUp() throws Exception {
}
@Test
void testIncrement() {
fail(“Not yet implemented”);
}
@Test
void testDecrement() {
fail(“Not yet implemented”);
}
}
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”;
}
}