aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Listing >> OOCalculator >> OOCalcTest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Listing >> OOCalculator >> OOCalcTest" Watch "Listing >> OOCalculator >> OOCalcTest" New topic
Author

Listing >> OOCalculator >> OOCalcTest

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

See this thread for discussion.
This thread shows the progression of work on OOCalcTest.java
Step 1: First cut for initial green light:
<pre>
package test;
import junit.framework.*;
public class OOCalcTest extends TestCase {
public OOCalcTest(String name) {
super(name);
}
}
</pre>
Next step
[ March 13, 2002: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Step 2 - Yellow Light
<pre>
package test;

import junit.framework.*;
import calc.OOCalc;

public class OOCalcTest extends TestCase {
public OOCalcTest(String name) {
super(name);
}

private OOCalc calc;

protected void setUp() {
calc = new OOCalc();
}

protected void tearDown() {
}

}
</pre>
Save and compile. We get errors because the compiler can't find the calc.OOCalc class.
Next step
[ March 13, 2002: Message edited by: Junilu Lacar ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Step 4 - First test case
<pre>
package test;

import junit.framework.*;
import calc.OOCalc;

public class OOCalcTest extends TestCase {
public OOCalcTest(String name) {
super(name);
}

private OOCalc calc;

protected void setUp() {
calc = new OOCalc();
}

protected void tearDown() {
}

public void testInitialResult() {
assertTrue("0".equals(calc.getResult()));
}

}
</pre>
Continue...
[ March 13, 2002: Message edited by: Junilu Lacar ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Step 7 - Refactoring
<pre>
package test;

import junit.framework.*;
import calc.OOCalc;

public class OOCalcTest extends TestCase {
public OOCalcTest(String name) {
super(name);
}

private OOCalc calc;

protected void setUp() {
calc = new OOCalc();
}

protected void tearDown() {
}

public void testInitialResult() {
assertTrue("0".equals(calc.getResult()));
assertEquals(0.0, calc.getResult(), 0.0);

}
}
</pre>
Continue...
[ March 13, 2002: Message edited by: Junilu Lacar ]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1757
    
    3
Junilu,
When I run the step 4 test code, I get a NullPointerException at the line:
assertTrue("0".equals(calc.getResult()));
It seems "calc" is not getting initialized in setUp(). Actually, I think setUp() isn't getting run at all: I added
assertTrue( false ) ;
after the assignment and it runs fine.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1757
    
    3
Also, could you explain the method
void assertEquals( double expected ,double actual , double delta )
I'm not sure I understand the point of the delta parameter.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Michael,
I'm not sure what's going on with the NullPointerException. setUp() is invoked before each testXXX() method and you could try putting a System.out.println just to prove to yourself that it's actually invoked. Try compiling both classes again. And make sure you don't have any stray .class files with the same names lying around your classpath.
As for delta: by nature, floating point comparisons are imprecise. 'delta' is the degree of tolerance for slight differences in the expected and actual values. If the difference is greater than the tolerance then the assertion will fail. In the Step 7 test, we have specified a 0.0 tolerance.
Junilu
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1757
    
    3
I'm all sorted out. Since we weren't very far along, I just deleted everything and started over. No exceptions thrown this time, so I'm all caught up.
By the way, adding print statements to setUp() didn't help (tried that even before I posted). JUnit must be redirecting standard output somewhere, but I haven't figured out where yet. That's why I had to add assertTrue( false ) ; originally to verify that setUp() was being called.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Step 8 - Test Addition
<pre>
package test;

import junit.framework.*;
import calc.OOCalc;

public class OOCalcTest extends TestCase {
public OOCalcTest(String name) {
super(name);
}

private OOCalc calc;

protected void setUp() {
calc = new OOCalc();
}

protected void tearDown() {
}

public void testInitialResult() {
assertEquals(0.0, calc.getResult(), 0.0);
}

public void testAddition() {
calc.in(10.0);
calc.in("+");
calc.in(5.0);
assertEquals(15.0, calc.getResult(), 0.0);
}

}
</pre>
Continue...
[ March 24, 2002: Message edited by: Junilu Lacar ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Step 9 - Refactoring
<pre>
public void testAddition() {
calc.in(10.0);
calc.in("+"OOCalc.ADD</blue>);
calc.in(5.0);
assertEquals(15.0, calc.getResult(), 0.0);
}</pre>

[ March 26, 2002: Message edited by: Junilu Lacar ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Step 10 - Tests for SUB and MUL added
<pre>
public void testSubtraction() {
calc.in(10.0);
calc.in(OOCalc.SUB);
calc.in(2.0);
assertEquals(8.0, calc.getResult(), 0.0);
}

public void testMultiplication() {
calc.in(5.0);
calc.in(OOCalc.MUL);
calc.in(3.0);
assertEquals(15.0, calc.getResult(), 0.0);
}
</pre>
Then write stubs in OOCalc. Compile and test (Fail - Red Light). Then copy/paste/modify to quickly get a green bar with this code
[ March 29, 2002: Message edited by: Junilu Lacar ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Tests for last refactoring in Step 10
<pre>
package test;

import junit.framework.*;
import calc.*;
public class OOCalcTest {
public OOCalcTest(String name) {
super(name);
}

private OOCalc calc;
private static final double DELTA = 0.00001;

protected void setUp() {
calc = new OOCalc();
}

public void testInitialResult() {
assertEquals(0.0, calc.getResult(), DELTA);
}

public void testAddition() {
calc.in(10.0);
calc.in(Addition.getInstance());
calc.in(5.0);
assertEquals(15.0, calc.getResult(), DELTA);
}

public void testSubtraction() {
calc.in(10.0);
calc.in(Subtraction.getInstance());
calc.in(2.0);
assertEquals(8.0, calc.getResult(), DELTA);
}

public void testMultiplication() {
calc.in(5.0);
calc.in(Multiplication.getInstance());
calc.in(2.0);
assertEquals(10.0, calc.getResult(), DELTA);
}

public void testDivision() {
calc.in(20.0);
calc.in(Division.getInstance());
calc.in(5.0);
assertEquals(4.0, calc.getResult(), DELTA);
}


}
</pre>
Continue...
[ March 29, 2002: Message edited by: Junilu Lacar ]
 
 
subject: Listing >> OOCalculator >> OOCalcTest