aspose file tools*
The moose likes Testing and the fly likes How many Test classes can I create for a class ideally? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How many Test classes can I create for a class ideally?" Watch "How many Test classes can I create for a class ideally?" New topic
Author

How many Test classes can I create for a class ideally?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all,

I would like to know how many JUnit test classes can one write for a class. Recently I had to test my class which has 2 methods. I want to test method-1 with only one scenario: mean the method expects few mocks. I want to test method-2 with set of mocks.

For this I created 2 test classes one which tests method-1 and other which tests method-2. The Test class which tests method-2 is actually a parametrized class where I initialize an array which has 5 set of values.

Then I heard that we should not create 2 test classes for one java class because it messes the code coverage.

If I have to combine the two test classes then I have to write 6 methods. Or I have to find some way to have parametrized constructor and a default constructor and somehow train JUnit to test it.

Can anyone please help me. Thank you all in advance.


Love all, trust a few, do wrong to none.
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1234
    
  67

Nothing wrong with having multiple test classes for a single class. In fact in many cases it makes more sense to do that.

You want your test classes to be concerned with only a single test fixture. That is a single set of prerequisites for the class that you're testing. If you find you are having to create distinct blocks of test setup for different test cases then you probably want to refactor that out into a new test class.

There are no hard rules on how many test classes per class. For most cases just one would suffice but other times I've written two or three, although for these times I wonder if my class I'm testing is too complicated and should be refactored itself into simpler parts.

I heard that we should not create 2 test classes for one java class because it messes the code coverage

Never heard of this. Doubt it's true.


Tim Driven Development
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11526
    
100

I agree with Tim - there are many cases where I think it makes sense to have multiple classes and/or base classes extended by test classes all to cover a single class. I don't think there is (or can be) a hard rule about this - some times it might make sense to have a single class, and other times it might make sense to have multiple classes.

chaitanya karthikk wrote:Then I heard that we should not create 2 test classes for one java class because it messes the code coverage.


I can't imagine how.

Most code coverage tools work by instrumenting the classes to be tested (adding hooks into the actual code to see when it is really called). As such the code coverage tools have no knowledge of which test classes called the method under test, only that they were called.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you guys. I understood that there can be any number of test classes for a java class. But I din't understand the explanation given for code coverage. Can you please explain in a bit detail Andrew.
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1234
    
  67

Let's run through an example. Say we have the following class we want to test:

We can achieve full branch and line coverage with either a single test class or two test classes. First let's look at the single test class option.

Now let's look at the multiple test class option:

Both of these options achieve the same amount of code coverage. The coverage tool will keep track of what code has been executed throughout the running of all test classes so for our example here the fact that the code is being executed by a single class or multiple classes is irrelevant.

In relation to whether we should have a single or multiple test classes per class is mostly a matter of taste. The example here is pretty trivial in that it has only one very simple dependency so it's not a huge overhead to initialise it in each test case. However, if the creation of that dependency was quite convoluted, or if there were many dependencies then it may make some sense to split it out into multiple test classes. The advantage of doing this is that you extract the setup and initialisation into a separate method that gets run before every test case, which then leaves your actual test cases free to get straight down to the business of making assertions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many Test classes can I create for a class ideally?