• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Listing >> OOCalculator >> OOCalcTest

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic