*
The moose likes Testing and the fly likes Confused about junit tests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Confused about junit tests" Watch "Confused about junit tests" New topic
Author

Confused about junit tests

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I'm trying to learn unit testing (In Eclipse), but I'm getting a little confused by the tutorials I'm reading.

This is what I know so far:
Suppose I have a project Proj, with package pack, containing class SomeClass.



And now I have created a class SomeClassTest (extends TestCase) in same package as shown:


When I run this it works fine, and there are no errors.
But am I going about it right?
Should I put the assertEquals calls inside any random method, or is there a special method for them?

And how does ordering work with methods, I don't think they necessarily run in order, but I'm not sure.

Any general guidelines and advice is much appreciated, Thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Colin,
There's not a right and wrong - just some things are more common than others.

My thoughts:
1) You are correct that tests are not guaranteed to run in a particular order.
2) Usually the test case name is descriptive. So I would favor test_initializedValues() rather than test1().
3) Typically classes have private fields and you test through the methods.
4) It's good to test different concepts in different test methods. For example, the assertion(s) related to x would go in a different method than those relating to v.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks,

In the TestCase class, there is a runTest() method.
So I tried to override this, and put my assertions in there,
and it seemed to work the same as when I had test1(),
I don't see what the point of it is?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You typically have more than one test method in a test case class. The framework automatically generates an instance for each method, and the runTest method will determine which test method to actually execute. (Beware: I'm not sure whether that's still true in JUnit 4.)

Overwriting the runTest method will work, but if you want to have more than one test in a testcase class, you will have to reimplement that behaviour. That's something you normally don't want to do.

Your best option when you are new to JUnit is probably to ignore it.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, are there any basic examples of simple classes, that are tested, along with the class that extends TestCase.

All the stuff I've looked at seems complicated, and doesn't really give examples.

Any ideas, thanks.
M Easter
Ranch Hand

Joined: Feb 11, 2007
Posts: 133
The following article is about Emma, which is typically used _after_ JUnit.

However, in order to illustrate Emma, it has examples of a simple Java class with JUnit test classes.

http://ociweb.com/jnb/jnbFeb2007.html


M Easter
Software Composer - http://codetojoy.blogspot.com
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, I think I found what I was looking for..
There is a class Card (implements Comparable)
and a test class PokerHandTestCase (extends TestCase)

I think thats what you meant I should look at.
Thanks
M Easter
Ranch Hand

Joined: Feb 11, 2007
Posts: 133
Yes... though PokerHand is the primary class for the example. Card is just a utility class

Mike

ps. I am the author of the article. If you find it interesting or useful, i would appreciate it if you "digg" it on Digg. (if not, then no worries)
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi again, I found it very interesting, but also confusing...

In the PokerHandTestCase class, the first method is..



We can see this isn't called (except from inside the other methods),
but when we look at the next method...


Then this is automatically called, because I have a '1' printed out.
Now, when I change the argument list so it has at least one argument, eg int x, then this method is no longer called.

This is what I have established:
If method is private and has no arguments, it creates an error.
If the method is private and has arguments, it will only be called when you explicitly call it, as with the first method.
If the method is public and has no arguments, it is called automatically.
If the method is public and has arguments, it will only be called when you explicitly call it.

Thats probably wrong, but please can someone clarify whats going on in this unit test.
Also, when the methods are automatically called, are they called in the order of which they are written (this seems to be the case from my output).

Thanks very much for any help!.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Ok, seems I have been a bit dumb, but I see the methods beginning with 'test' are automatically called, and ones that aren't aren't.

Please add any necessary information that you think I will benefit from regarding these methods.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41147
    
  45
There are a couple of good introductury articles on the JUnit web site, A Cook's Tour and Test Infected. Those contain a good overview of how to name and organize methods, what to override and what not, and how to generally get started with JUnit.


Ping & DNS - my free Android networking tools app
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by colin shuker:
Ok, seems I have been a bit dumb, but I see the methods beginning with 'test' are automatically called, and ones that aren't aren't.


In short, the JUnit framework will execute all public, void, no-argument methods as tests. That is, methods that look like this:



If the method throws any kind of an exception, that test is considered to fail. That's how the assertXxx() methods work--they throw an unchecked exception if the assertion doesn't pass.

Before JUnit invokes the test* method, it first invokes the test class instance's setUp() method. Overriding this method lets you do common "pre" stuff such as create objects for the test methods to operate with.

After JUnit has invoked the test* method, it invokes the tearDown() method (regardless of whether the test method passed or failed). Overriding this method lets you perform any common cleanup after your test methods, such as releasing any shared resources your test may have used.

That's the gist of it. However, I strongly suggest reading the links posted above by Ulf.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
I think to appreciate how much (or how little) JUnit does for you, it might be worth reading my lecture notes on automated testing, where my students learned how to write their own, admittedly trivial, test framework. They're fairly short, and there's no ads, this isn't a commercial plug.

http://cime.net/~ricky/netsim/oldham/automated_testing.html

Some of them did go off and write something more like JUnit after that. You always get people who overdesign..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about junit tests
 
Similar Threads
add component to a visible component
noobie junit issue
Unsupported major.minor version 49.0
Junit Compilation errors
method names in jUnit