CS-2510 Lab 11 - Optional

Purpose:

To see what real software engineers use to write tests and test-suites.




As we have seen in class, JUnit is a Java library for writing tests. The only problem (i.e., the reason we haven't mentioned it until now) is that you (yes you) are responsible for implementing equality correctly for any classes that you wish to use in tests.


Part 1: Some Setup

Below is a class I've created that you can use to explore testing with JUnit. I've written an equals method to spare you the wasted time... but I'm not sure if the other methods are implemented correctly?!

Your job is to find and fix any bugs, while at the same time learning a little about JUnit.

/** Represents a 2D Position */
public class Posn{
    double x;
    double y;
    
    Posn(double x, double y){
        this.x = x;
        this.y = y;
    }
    /** Compute the distance between this and the given Posns */
    double dist(Posn that){
        return Math.sqrt(Math.pow(this.x-that.x, 2)+
                         Math.pow(this.x-that.x, 2));
    }
    /** Move this Posn in the given direction */
    void move(double dx, double dy){
        this.x += dx;
        this.y += dx;
    }
    
    /** Is this Posn the same as the given Object */
    public boolean equals(Object o){
        if(!(o instanceof Posn))return false;
        Posn p = (Posn)o;
        return p.x == this.x && p.y == this.y;
    }

    /** Compute a hash code for this Posn */
    public int hashCode(){
        return new Double(this.x + this.y * 3).hashCode();
    }
}
          

Exercise 1:

  1. Create a new Project and don't import any libraries (yet). Call it Lab11 or something.
  2. Create a new file in the src for the project named Posn.java, and add the code above for the Posn class to the file.
  3. Right click your src directory in the Package Explorer and select New > JUnit Test Case. Give your test case a name (e.g., JUnitTest. This should create a new Java file with a public class (e.g., JUnitTest.java with a JUnitTest class).

    If it asks you if you want to import the JUnit library into your build path, say yes.




Part 2: Your first Test method

Exercise 2:

Add the following method to your newly created test class:

    public void testPosn(){
        assertEquals(Math.sqrt(16), 4.0);
        assertEquals(Math.pow(2, 10), 1024.0);
        assertEquals(new Posn(2, 4), new Posn(2, 4));
        
        // Tests for the Dist method
        /* ... */
        
        // Tests for the Move method
        /* ... */
    }              
              
Right-click on your JUnit test-class and select Run As > JUnit Test.

You should see a new Window/Tab showing the results of the tests. Hopefully you only see a full green bar, meaning that all the tests I gave you worked correctly.

Exercise 3:

Add tests for the dist method. Is there an error? Fix it if there is. Otherwise make sure you have tests that thoroughly show it is correct.

Exercise 4:

Add tests for the move method. Just like with mutation and the Tester, you'll need to create Posns, mutate one, and compare the changed one against another, unchanged one. Is there an error? Fix it if there is. Otherwise make sure you have tests that thoroughly show it is correct.

So, when you want to test, you would create a JUnit Test Case class, create some test methods, and run them. Get the idea?




Part 3: Working on your Project

Use this time to work with your partner to write tests for your Final Project. You may obviously work on any other parts of your project as well if you wish.