CS 261 University of Puget Sound Spring, 2020
 
Computer Science II
Abstract Data Types and their Implementations, Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency
 

Warning: This course is under development. Although the basic structure of this course is largely established, nothing on this Web site should be considered official or even possibly correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.

Lab: Unit Testing

Summary

This laboratory provides practice with basic elements of the JUnit framework.

Background Reading

Before working on this lab, be sure you have

Acknowledgments

This page is a slightly edited from material from Samuel Rebelsky.

Getting Started

  1. Create a new package sampleTesting and class SampleMethods in Eclipse.

  2. Copy the file SampleMethods to become the body of the SampleMethods class in Eclipse.

  3. Create a JUnit Test Case for the c2f method in the SampleMethods class. That is,

  4. Click the Run button and observe what happens.
    Caution: Depending upon the environment the following may or may not work on your local computer account.
    If JUnit does not work for your account, please skip the remainder of this lab.

  5. Replace the body of the testC2F (or just test) method with assertEquals(0,0). Then click the Run button and observe what happens.

  6. Create a second method that looks like the following:

      public void 
      test2() 
      {
        fail("Not yet implemented");
      } // test2()
        
  7. What do you expect to happen when you run your test?

  8. Check your answer experimentally.

  9. Insert the annotation @Test before the declaration of test2. Then determine what happens when you run your test code.

  10. Change the body of test2 to the following. Then observe what happens when you run your test code.

            assertEquals("stupid test", 10, 3*5);
        

    If/when you get a compilation error, change the import statement to

          import static org.junit.Assert.*;
        

    Now try running the test code again.

    Yes, this test is supposed to fail. It's there to demonstrate that (a) you can add a message to assertEquals, (b) you can include computations in the body of assertEquals, and (c) assertEquals treats the first non-message value as the expected value and the second value as the received value.

Temperature Conversion

You've seen how Eclipse lets you create and run tests. Now it's time to write a few of your own.

  1. We know that 0 degrees Celsius is 32 degrees Fahrenheit. Write a test that verifies that SSampleMethods.c2f computes the expected value.

  2. We know that 100 degrees Celsius is 212 degrees Fahrenheit. Write a test that verifies that SampleMethods.c2f computes the expected value.

  3. Run your tests.

  4. If your tests reveal errors in c2f, correct the code. Then run the tests again.

  5. Write any other tests you think are relevant. If the tests reveal errors, correct the code and then run the tests again.

Testing Simple Sums

  1. Read the documentation for sum

  2. Create a new test class and write some simple tests for sum. Note that you can create an array of integers with the following instruction.

        int[] values = { 1, 2, 3 };
        
  3. Attempt to fix any errors that your tests revealed.

  4. Write a few more tests. Fix any more errors that you notice.

  5. Consider the following test. Does it meet the preconditions of sum? Do you expect your code to pass the test?

      public void 
      testExtremes() 
      {
         int tmp = Integer.MAX_VALUE - 10;
         int[] values = { tmp, tmp, -tmp, -tmp };
         assertEquals("extreme test", 0, sum(values));
      } // testExtremes
        
  6. If your code does not pass the test (or if you believe that your code should not pass the test because integer overflows are supposed to break things), you have multiple options. One option is to fix your code to handle situations like this. Another is to rewrite the preconditions (e.g., "For every subset of ints, the sum of that subset is greater than Integer.MIN_VALUE and less than Integer.MAX_VALUE". Another is to say that the test is so stupid that it shouldn't matter. Which approach do you prefer and why?

Writing Multiple Test Cases

Read the documentation for the expt method.

Rather than writing individual test cases for this function, we should be able to write a loop that does multiple test cases, something like the following:

Let expected be 1
For power = 0 to K
    Confirm that expected = expt(2,power)
    expect = expected * 2
  1. Create a new test class and then add a test that follows that strategy.

  2. Determine if expt works as advertised. It probably won't. But debugging the code is a task for another day. Instead, we're going to write more tests.

  3. Right now, your test only uses a base of 2. Rewrite your test so that it tries multiple bases, both positive and negative.

For those with Extra Time

  1. Test removeAs: Write tests for the removeAs method. Fix the method if necessary.

  2. Test removeBs: Write tests for the removeBs method. Fix the method if necessary.

  3. Sharing Tests: Incorporate tests from other groups for removeAs and removeBs and see if your repaired code passes their tests, too.