Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Junit tests, setting up tests without having to create a billion methods

Thread view: 
xyzzy12@hotmail.com - 15 Feb 2006 17:14 GMT
Is there a more dynamic way of setting up tests?  We have legacy test
code that we are trying to convert to junit.

I think it is silly to be required to set up a "testFunc(...)" method
for each test.

We'd prefer:

   abstract void runTests(Results x);

In your code you implement that and populate Results.  If the runTests
blows up, the caller catches the exception and populates TestsResults
on the methods behalf.  Easy, breezy, and would have worked just fine
with Java 1.18.

Right now we just have runAllTests, but that makes the junit report too
boring, we want to see each test.  It will be too tedious to make a
separate method for each test.
Andrew McDonagh - 15 Feb 2006 19:42 GMT
> Is there a more dynamic way of setting up tests?  We have legacy test
> code that we are trying to convert to junit.
[quoted text clipped - 14 lines]
> boring, we want to see each test.  It will be too tedious to make a
> separate method for each test.

it would be better to post this message to the JUnit yahoo group where
there is a huge wealth of experience and help available to you.

As it is, what you are asking for is either:

1) Parametrized TestCase - (Google-able)
  - If you are testing the same functionality with different data

or

2) Not Applicable to JUnit
   - If you are trying to test multiple functionalities with one
method. As each test case (the test method) is supposed be decriptive of
the functionality its testing and so lumping everything into one big
'testTheWorld(stuff)' defeats that.

HTH

Andrew
Raymond DeCampo - 15 Feb 2006 20:10 GMT
>> Is there a more dynamic way of setting up tests?  We have legacy test
>> code that we are trying to convert to junit.
[quoted text clipped - 30 lines]
> functionality its testing and so lumping everything into one big
> 'testTheWorld(stuff)' defeats that.

I hate that "not applicable to JUnit" crap.  (No offense Andrew.)  It
just bothers me that people try to push you into using the tool only one
way.  What if I want an automated integration test?  JUnit is able to do
this, even if it goes against the philosophy of its creator.

This is especially true when you've inherited an "engine" that doesn't
already have unit tests.  You only really care about the external
interface; you do not have time to reverse engineer every class to
create unit tests.  Automated integration tests based on the macro,
known desired behavior of the system is useful here.

The OP might want to look into creating a custom TestRunner.

Ray

Signature

This signature intentionally left blank.

Andrew McDonagh - 15 Feb 2006 20:25 GMT
>>> Is there a more dynamic way of setting up tests?  We have legacy test
>>> code that we are trying to convert to junit.
[quoted text clipped - 32 lines]
>
> I hate that "not applicable to JUnit" crap.  (No offense Andrew.)

None taken

I was merely trying to point out that JUnit isn't designed for that
scenario (in fact I think the OP has seen this by the remark that the
'test failure message was boring'). But you are right, a custom
TestRunner might be able to do what the OP wants.

>  It
> just bothers me that people try to push you into using the tool only one
> way.  What if I want an automated integration test?  JUnit is able to do
> this, even if it goes against the philosophy of its creator.

Sure any of the xUnit frameworks can be used for integration, System,
UAT, SAT, etc tests - it usually comes down to how the test is described
that makes one tool more appropriate than another.

Personally, I would use any of the xUnit frameworks for any testing
other that unit testing.  The other tests will (in my situation) has to
be readable by my customers - they don't know Java. But they can read
something like....

Start IE
Browse to http://www.google.com
Enter 'Watir' into searchBox
Click Search button
Check Page contains 'Watir: Web Application Testing in Ruby'

Now in JUnit this test would not be anywhere near as short nor readable
to a non-programmer (be they Customer or tester).

Its Horses-for-courses rather than 'that-aint-what-I-created-it-for'

Andrew

> This is especially true when you've inherited an "engine" that doesn't
> already have unit tests.  You only really care about the external
> interface; you do not have time to reverse engineer every class to
> create unit tests.  

Agreed, in fact I'd recommend that we don't retrofit unit tests.  I
would add a few integration/acceptance tests if possible, using a DSL
something like above.

> Automated integration tests based on the macro,
> known desired behavior of the system is useful here.
>
> The OP might want to look into creating a custom TestRunner.
>
> Ray
James McGill - 15 Feb 2006 22:11 GMT
> > Is there a more dynamic way of setting up tests?  We have legacy test
> > code that we are trying to convert to junit.

I really enjoyed using Fitnesse.  A whole different idiom for testing,
but very flexible and accessible to nonprogrammers.  

http://fitnesse.org/

Anyway, it sounds like you're working backwards.  You've already written
the code you want to test, which is backwards from TDD, which sort of
means JUnit isn't exactly the right tool for what you're doing.  Still
it occurs to me that you should be able to automatically generated
wrappers for your legacy tests, and put those into a testsuite, and be
done with the legacy stuff and move forward with JUnit.
Andrew McDonagh - 15 Feb 2006 23:00 GMT
>>>Is there a more dynamic way of setting up tests?  We have legacy test
>>>code that we are trying to convert to junit.
[quoted text clipped - 10 lines]
> wrappers for your legacy tests, and put those into a testsuite, and be
> done with the legacy stuff and move forward with JUnit.

Hi James,

You seem have have attributed the OPs remarks to me...

That being said, the OP is specifically talking about the case where we
have a legacy code base which wasn't tested with Junit - not that it
wasn't tested. There's no mention of whether TDD was used or not -
though I think we both suspect it wasn't and that the code base was
merely Unit Tested.

Unfortunately, working with legacy code is currently the most common
starting point on projects that adopt TDD. Greenfield projects using TDD
are still rare.

Also, keep in mind, whilst JUnit was developed as a tool to Aid TDD,
Beck et al do realise that some people won't or can't do TDD but still
need a decent Unit Testing tool.

As I'm sure you know - but as an aid for others - TDD is not Unit Testing.

TDD is actually a design methodology that happens to use unit tests to
describe the design. Much like RUP uses UML, etc. In fact this usage of
the term Test has causes so many to concentrate on the testing side of
TDD that some are now starting to question whether a better name could
be found.

e.g.
Behavior Driven Development (http://behaviour-driven.org/)
Specification Driven Development
etc.

Andrew
James McGill - 15 Feb 2006 23:31 GMT
> Hi James,
>
> You seem have have attributed the OPs remarks to me...

Didn't mean to, sorry.

> That being said, the OP is specifically talking about the case where we
> have a legacy code base which wasn't tested with Junit - not that it
> wasn't tested.

I still don't see how you'd need "a billion" methods.  Your starting
place has some kind of testing.  Just wrap all that into a test suite
and be done with it, right?  I could be underestimating the size of the
project, but if it's that big, maybe a whole new approach to testing
would be a good thing anyway.  

But if the legacy tests work, don't fix 'em!
Andrew McDonagh - 16 Feb 2006 00:35 GMT
>>Hi James,
>>
[quoted text clipped - 11 lines]
> project, but if it's that big, maybe a whole new approach to testing
> would be a good thing anyway.  

No I don't see why the Op does either...

> But if the legacy tests work, don't fix 'em!

Exactly!

:)
xyzzy12@hotmail.com - 28 Feb 2006 20:59 GMT
I found code that works:

Let's say you need to write a junit test that tests several method
calls.

For example, let's say you wrote a method called square(int x) which
takes x and multiples it by itself.

public static Test suite() {
    TestSuite        suite = new TestSuite();
    for(int i=0;i<65535;i++){
                       final int x=i;
        TestCase test = new TestCase(i+"*"+i) {
                   public void runTest() {
                       assertEqual(""+square(x),""+(x*x));
                   }
             };
             suite.addTest(test);
            }
    return suite;
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.