Kate T.

asked • 09/25/21

Writing Unit test for mutator

Writing unit tests for mutators are only slightly different from that of constructors. The steps used are:

  1. Create an object with initial values.
  2. Call the mutator method that will change the object
  3. Use assertEquals and call the accessor method for each of the instance variables or calculations to make sure they contain the expected (new) values.

Given the following constructor for a fictitious Time class:


public Time(int hours, int minutes, int seconds);


Write the unit test statements to test each method as indicated below. Note that the first part, creating an object to test, has been done for you in each method.


a.

public void setHours(int hours);

public int getHours();


@Test

public void testSetHours() {

Time time = new Time(4, 14, 43);



1 Expert Answer

By:

Brady B. answered • 09/27/21

Tutor
5 (1)

College Graduate with 4 years of Java and Programming Experience

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.