• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listing >> OOCalculator >> OOCalcTest

 
Junilu Lacar
Bartender
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Lacar
Bartender
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic