Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the following JUnit test to simulate a Test Driven Development environment.P

ID: 3664958 • Letter: U

Question

Use the following JUnit test to simulate a Test Driven Development environment.Place the test class into a project, and write a class so that the test compiles.

Run the test, and modify your class until it passes the first test.

Uncomment the next test, and modify your class until it passes that test.

Repeat this process until your class support all of the tests in the test class.

import static org.junit.Assert.*;

import org.junit.Test;

public class CounterTest {

   // FIRST
   // Write a class Counter such that the following test works
   @Test
   public void testZero() {
       Counter cnt = new Counter(0);
       assertEquals("initial value of (0) failed", 0, cnt.getCount());

       cnt.increase();
       assertEquals("increased value of (0) failed", 0, cnt.getCount());

       cnt.decrease();
       assertEquals("decreased value of (0) failed", 0, cnt.getCount());
   }

   // SECOND
   // Uncomment the following method,
   // modify your Counter class so that this test works.
   /*
   @Test
   public void testIncrease() {
       Counter cnt = new Counter(7);
       assertEquals("initial value of (7) failed", 0, cnt.getCount());

       cnt.increase();
       assertEquals("increased once value of (7) failed", 7, cnt.getCount());

       cnt.increase();
       assertEquals("twice increased once value of (7) failed", 14,
               cnt.getCount());
   }
   */


   // THIRD
   // Uncomment the following method,
   // modify your Counter class so that this test works.
   /*
   @Test
   public void testDecrease() {
       Counter cnt = new Counter(11);
       cnt.decrease();
       assertEquals("decreased value of (11) failed", -11, cnt.getCount());

       cnt.decrease();
       assertEquals("twice decreased value of (11) failed", -22,
               cnt.getCount());
   }
   */


   // FOURTH
   // Uncomment the following method,
   // modify your Counter class so that this test works.
   /*
   @Test
   public void testNegative() {
       Counter cnt = new Counter(-1);
       cnt.decrease();
       assertEquals("decreased value of (-1) failed", 1, cnt.getCount());

       cnt.increase();
       assertEquals("decreased/increased value of (-1) failed", 0,
               cnt.getCount());

       cnt.increase();
       assertEquals("decreased/increased/increased value of (-1) failed", -1,
               cnt.getCount());
   }
   */
}

Explanation / Answer

package countertest;

import static org.junit.Assert.*;
import org.junit.Test;
import sun.management.counter.Counter;
import sun.management.counter.Units;
import sun.management.counter.Variability;


/**
*
* @author
*/
public class CounterTest {

    /**
     * @param args the command line arguments
     */
  
    @Test
    public void testZero() {
        Counter cnt = new Counter(0) {
        assertEquals("initial value of (0) failed", 0, cnt.getCount());
        cnt.increase();
        assertEquals("increased value of (0) failed", 0, cnt.getCount());
        cnt.decrease();
        assertEquals("decreased value of (0) failed", 0, cnt.getCount());
            @Override
            public String getName() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public Units getUnits() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public Variability getVariability() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public boolean isVector() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public int getVectorLength() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public Object getValue() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public boolean isInternal() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public int getFlags() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };
      
      
    } // end testZero()
  
    @Test
    public void testIncrease() {
      
                Counter cnt = new Counter(7);
       assertEquals("initial value of (7) failed", 0, cnt.getCount());

       cnt.increase();
       assertEquals("increased once value of (7) failed", 7, cnt.getCount());

       cnt.increase();
       assertEquals("twice increased once value of (7) failed", 14,
               cnt.getCount());
    }
  
     @Test
   public void testDecrease() {
       Counter cnt = new Counter(11);
       cnt.decrease();
       assertEquals("decreased value of (11) failed", -11, cnt.getCount());

       cnt.decrease();
       assertEquals("twice decreased value of (11) failed", -22,
               cnt.getCount());
   }

    @Test
   public void testNegative() {
       Counter cnt = new Counter(-1);
       cnt.decrease();
       assertEquals("decreased value of (-1) failed", 1, cnt.getCount());

       cnt.increase();
       assertEquals("decreased/increased value of (-1) failed", 0,
               cnt.getCount());

       cnt.increase();
       assertEquals("decreased/increased/increased value of (-1) failed", -1,
               cnt.getCount());
   }
  
    public static void main(String[] args) {
        // application logic here
      
      
      
      
      
      
    }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote