We help IT Professionals succeed at work.

JUnit Test Infected: Programmers Love Writing Tests

chima
chima asked
on
534 Views
Last Modified: 2012-06-27
I am hoping to get an "overview" of this junit test example.  I have used junit before, but I am getting confused with this example;
http://junit.sourceforge.net/doc/testinfected/testing.htm#class%20MoneyTest

These are my questions, text in quotes come from the web site:
"start simple and define a class Money",
Am I to make a Project with a class Money?  If so, this class Money will have a;
public static void main(String[] args){...
Althrough the code shown does not show the main() statment.  
So then how do I start?

"therefore define MoneyTest as a subclass of TestCase"
I know what this means; it is make a Project with JUnit Test Case:
public class MoneyTest extends TestCase   I did and added the code shown.

Now the next code shown "public void testEquals() {..."
Should go in which Project/file?

Again, where should the next set of code go;
"public boolean equals(Object anObject) {..."

By this statement; "Now that we have implemented two test cases" I would assume that the code discussed thus far belongs in;
public class MoneyTest extends TestCase   Yet, this code is shown;

public class MoneyTest extends TestCase {
    private Money f12CHF;
    private Money f14CHF;  

    protected void setUp() {
        f12CHF= new Money(12, "CHF");
        f14CHF= new Money(14, "CHF");
    }
}

Is the "public class MoneyTest extends TestCase" code complete?

By this time, I should know where the test cases should go; "can rewrite the two test case methods"?
public void testEquals() {...
public void testSimpleAdd() {...

I know that this is worth more than 500 points, but I will catch you on the next round.
I do enjoy a chanllenge, so I think once I understand the above I can take it from the
"Two additional steps are needed to run the two test cases:" point.  If not I start a new question.
Thanks in advance.  I need to know this stuff tonight so I'll be here.


Comment
Watch Question

Commented:
Hi chima,

> Althrough the code shown does not show the main() statment.  
> So then how do I start?
You do not need to start it.  Money is an object class, the MoneyTest class is the one that will be run.

> Now the next code shown "public void testEquals() {..."
> Should go in which Project/file?
The testEquals is a JUnit test to see if two Money objects are equal - it should go in the MoneyTest class.


> Again, where should the next set of code go;
> "public boolean equals(Object anObject) {..."
As indicated in the article "Next let's write the equals method in Money:" - it goes in the Money class.


> Is the "public class MoneyTest extends TestCase" code complete?
No - here they are demonstration how to use the "setUp" method.  The two test cases they describe are then re-written below using the private variables f12CHF and f14CHF.  The complete class would be as follows:

public class MoneyTest extends TestCase {
    private Money f12CHF;
    private Money f14CHF;  

    protected void setUp() {
        f12CHF= new Money(12, "CHF");
        f14CHF= new Money(14, "CHF");
    }

    public void testEquals() {
        Assert.assertTrue(!f12CHF.equals(null));
        Assert.assertEquals(f12CHF, f12CHF);
        Assert.assertEquals(f12CHF, new Money(12, "CHF"));
        Assert.assertTrue(!f12CHF.equals(f14CHF));
    }

    public void testSimpleAdd() {
        Money expected= new Money(26, "CHF");
        Money result= f12CHF.add(f14CHF);
        Assert.assertTrue(expected.equals(result));
    }

}

Let me know if this didn't make sense...


Cheers,
Hillwaaa

Author

Commented:
Hello Hillwaaa
I have inserted the code you provided into my;
public class MoneyTest extends TestCase {

I knew this next problem might show up, if indeed I need the following import statement (which I believe that I do need);
import junit.framework.TestCase;

The problem I am having is; "import junit is not resolved."

Relative to Eclipse, might you be able to address this.  I say this because junit is bundled with eclipse.

Author

Commented:
Hillwaaa
Please read inserted comments >>>:
> Althrough the code shown does not show the main() statment.  
> So then how do I start?
You do not need to start it.  Money is an object class, the MoneyTest class is the one that will be run.
>>> Okay, so you are saying that Money is part of Junit.  Let's say a jar

> Now the next code shown "public void testEquals() {..."
> Should go in which Project/file?
The testEquals is a JUnit test to see if two Money objects are equal - it should go in the MoneyTest class.
>>> Okay, got you.


> Again, where should the next set of code go;
> "public boolean equals(Object anObject) {..."
As indicated in the article "Next let's write the equals method in Money:" - it goes in the Money class.
>>> But you mentioned that the Money class was an object class?  Do I have to make a second file?

> Is the "public class MoneyTest extends TestCase" code complete?
No - here they are demonstration how to use the "setUp" method.  The two test cases they describe are then re-written below using the private variables f12CHF and f14CHF.  The complete class would be as follows:

>>> I'll look for your reply to the questions above.
thanks

Author

Commented:
I will have to take a short leave from coresponding.  I will get back asap.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hillwaaa, thanks for the help.  I'll be closing this question and openning up another.
Please see my questions at "Programming/Java" channel.

Author

Commented:
I'll be studying the entire code now.
Hillwaaa, thanks for the help.  I'll be closing this question and openning up another.
Please see my questions at "Programming/Java" channel.

I am repeating this statement, because I am not sure which statement you get to see?
Maybe both!

Commented:
No worries - and yes, I see both statements :)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.