This discussion is archived
2 Replies Latest reply: Jul 28, 2008 12:46 AM by 843785 RSS

Problem with jUnit

843785 Newbie
Currently Being Moderated
This is my first time using jUnit so I think I may be missing something.

I created a simple class that calculates the hypotenuse
public class Hypotenuse {

    public double hypt(double adj, double opp)
    {
        return Math.sqrt(sumOfSquares(adj, opp));
    }

    public double sumOfSquares(double adj, double opp)
    {
        return square(adj) + square(opp);
    }

    public double square(double side)
    {
        return side * side;
    }

}
And the jUnit test
@Test
    public void testHypt() {
        System.out.println("hypt");
        double adj = 3.0;
        double opp = 4.0;
        Hypotenuse instance = new Hypotenuse();
        double expResult = 5.0;
        double result = instance.hypt(adj, opp);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }

    /**
     * Test of sumOfSquares method, of class Hypotenuse.
     */
    @Test
    public void testSumOfSquares() {
        System.out.println("sumOfSquares");
        double adj = 3.0;
        double opp = 4.0;
        Hypotenuse instance = new Hypotenuse();
        double expResult = 25.0;
        double result = instance.sumOfSquares(adj, opp);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }

    /**
     * Test of square method, of class Hypotenuse.
     */
    @Test
    public void testSquare() {
        System.out.println("square");
        double side = 3.0;
        Hypotenuse instance = new Hypotenuse();
        double expResult = 9.0;
        double result = instance.square(side);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }
All the tests fail. When I run it through the main method the method hypt() works fine.
public static void main(String[] args) {
        Hypotenuse h = new Hypotenuse();
        System.out.println(h.hypt(3.0, 4.0));
    }
OUTPUTS: 5.0 (as expected)


What am i doing wrong?

Thanks
Glenn