Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
Java Programming

An Early Look at JUnit 4 147

An anonymous reader writes "Elliotte Harold, proclaimed 'obsessive code tester', took an early look at JUnit4 and shows how to best utilize the framework in your own projects. Many feel that this is one of the most important third-party Java libraries ever developed. It promises to simplify testing by exploiting Java 5's annotation feature to identify tests rather than relying on subclassing, reflection, and naming conventions."
This discussion has been archived. No new comments can be posted.

An Early Look at JUnit 4

Comments Filter:
  • More on Elliotte (Score:5, Informative)

    by tcopeland ( 32225 ) * <tom AT thomasleecopeland DOT com> on Wednesday September 14, 2005 @05:23PM (#13560946) Homepage
    For those who haven't heard of him, Elliotte Rusty Harold is a big name in the Java world - he maintains a very popular blog/news site [ibiblio.org] and has written a slew of excellent books.

    He's also a committer on the open source Jaxen [jaxen.org] XPath engine; my static analysis utility PMD [sf.net] is among the many satisfied Jaxen customers.
  • by CadmannWeyland ( 609987 ) on Wednesday September 14, 2005 @05:33PM (#13561027) Homepage
    Actually, there are a number of xUnit implementations out there. JUnit is just one of many for many languages. NUnit is by no means a rip off of JUnit than JUnit is of pyUnit, or cppUnit, etc.

    For more info on xUnit testing frameworks for many different languages and platforms see (way down the page is a table):
    http://www.xprogramming.com/software.htm/ [xprogramming.com]

    Cadmann
  • Re:Testing (Score:3, Informative)

    by ashridah ( 72567 ) on Wednesday September 14, 2005 @05:39PM (#13561071)

    I've also been recently poking at Jmock [jmock.org] and CGLib [sf.net] for my testing system as well.

    Jmock's built on top of junit, so it uses the same mechanisms, and can produce test cases the same way (ie, it works in eclipse's junit view :) ).

    Using JMock, one can create mock objects for interfaces that expect various functions to be called some number of times with particular sets of arguments. I believe they can even be configured to throw various exceptions

    This is handy in the junit sense so that you can test classes in isolation.

    Of course, this only works for classes with predefined java interfaces. So toss cglib into the mix, and jmock will happily use that to create mock classes out of concrete classes instead! (basically by using an alternative to the java reflections api). Doesn't require native libraries either, still all java based, and still all opensource.

    Like I say, looking very handy for my next major project which we're just finishing the design stage of at uni :).

    That said, I haven't tried anything too complex with it yet, and the mock objects toss out some weird error messages if you mistype a function name or parameter :) )


    ashridah

  • by digerata ( 516939 ) on Wednesday September 14, 2005 @05:43PM (#13561099) Homepage
    Many feel that this is one of the most important third-party Java libraries ever developed.

    Unless your application is database driven.

    As of several months ago, when I last looked, there is no easy way to do automated unit testing on an application that requires a existing dataset for each unit.

    DBUnit made an attempt but it was far from realistic and did not scale in anyway to the enterprise level. What?? You mean I have to store my schema as XML? That's re-goddamn-tarded.

    Everything ends up a kludge that is extremely difficult to maintain. If people have seen different, please share.

  • A famous rapper named 50 Cent has a click/gang/group called G-unit. They often say "G-G-G-G-G-G-UNIT" in their songs so people can recognize who they are. Saying "J-J-J-J-J-J-UNIT" is just a play off of this.
  • Super off-topic (Score:1, Informative)

    by Anonymous Coward on Wednesday September 14, 2005 @05:58PM (#13561204)
    Can anyone tell me why this story was rejected:

    With Visual Studio .NET 2005 and the .NET 2.0 run-time not even out of beta, Microsoft has released the C# Version 3.0 Specification (doc format) at their http://msdn.microsoft.com/events/pdc [anonymouse.org]">Professional Developers Conference 2005. http://msdn.microsoft.com/vcsharp/future/ [anonymouse.org]">New features include SQL-like and XQuery-like query expressions, implicit (on initialization) typing, lambda expressions and more. The primary focus of the language extensions are encompassed by http://msdn.microsoft.com/netframework/future/linq / [anonymouse.org]">The LINQ Project which aims to bridge the gap between the object-oriented and relational worlds in a simple and type-safe way.

    This is news and it is certainly stuff that matters to nerds. The summary is well written, well linked, and unbias. I also did some searching of slashdot and could not find evidence of the story being a dupe (not that the editors would have noticed).

    So why was it rejected?
  • Re:Java 5, JUnit4 (Score:3, Informative)

    by iabervon ( 1971 ) on Wednesday September 14, 2005 @06:30PM (#13561449) Homepage Journal
    I haven't looked at the details yet, but I think you're supposed to be able to affect compilation using annotations, and you can certainly have tools post-process class files using annotations. What that means is that you can have a "production build" which strips out everything annotated with the JUnit annotations, and have your tests actually in the class they're testing, but have them not end up in the final application. Then it's easy to find the tests for a class (they're in the same class) and the tests can even peek at private fields, which is really nice, because sometimes you don't want subclasses messing with some of the fields you want to test.
  • Re:Web / GUI (Score:3, Informative)

    by KenSeymour ( 81018 ) on Wednesday September 14, 2005 @06:35PM (#13561491)
    Someone once referred me to Cactus, [apache.org] which is an extension of JUnit.

    It only helps you for web apps, though.
  • by PostItNote ( 630567 ) on Wednesday September 14, 2005 @07:07PM (#13561730) Homepage
    JUnit was actually the first - it was written back when people were still figuring out to call it "unit testing" as distinct from functional testing, and it was certainly the first widely used framework that supported unit testing.

    In that sense, all the other xUnit stuff is decidedly a descendant of JUnit.
  • by vidnet ( 580068 ) on Wednesday September 14, 2005 @07:18PM (#13561823) Homepage
    simplify testing by exploiting Java 5's annotation feature

    Like JTiger [jtiger.org] has done for ages?

  • by CryBaby ( 679336 ) on Wednesday September 14, 2005 @07:18PM (#13561826)
    Looks like JUnit4 is adopting most of its ideas from TestNG [testng.org]. This is good, as JUnit feels highly constrained and somewhat crippled compared to TestNG, but why even bother with JUnit4? TestNG can run JUnit tests unaltered [theserverside.com], so backwards compatibility isn't really an issue.

    Unless JUnit is going to add quite a few more features, it still won't be nearly as flexible as TestNG. I think the JUnit developers are stuck on this idea of independent tests, which certainly has its merits but ends up excluding a lot of powerful options or forcing you to use ugly workarounds.

    TestNG is more of an all-purpose testing framework, equally adept at unit testing as well as higher level functional testing. As a developer, I want to be able to test things in whatever way fits the task at hand. For instance, sometimes it's easier or arguably makes more sense to test a multi-step process (say, user registration and verification) in a defined order. This is possible with JUnit, but it definitely goes against the grain of the framework, which does not support test dependencies and therefore doesn't support ordered tests. I don't appreciate being penalized by a framework because its developers have a very specific concept of "pure" unit testing.

    Perhaps I should elaborate: I'm sure the JUnit developers know far more about unit testing that I do, but I want to write more than just unit tests. I'm perfectly happy to admit to writing functional and acceptance tests (and even tests that talk to a real database) in addition to true, pure unit tests. I understand why the differences should be recognized, but the fact remains that JUnit simply does not accomodate a broader view of testing.

    I hate to be critical of something that's brought so much improvement to how we write code (JUnit), but I think we've all learned a lot about unit and other types of testing and it's time to move on to something that embodies those lessons.
  • by Curt Cox ( 199406 ) <curtcox@NOsPAm.gmail.com> on Wednesday September 14, 2005 @07:22PM (#13561861)
    Almost. The Smalltalk version (SUnit) was the original which inspired JUnit. It wasn't the watershed that JUnit was due to the relative popularity of the languages.
  • RE: Too bad (Score:2, Informative)

    by Slurm ( 147172 ) <slashdot@derekchiles.BLUEnet minus berry> on Wednesday September 14, 2005 @07:23PM (#13561870)
    After reading this, I noticed it was great but the most obvious omission is a GUI test runner. Fortunately, it is possible that if you want to see a comforting green bar when your tests pass or an anxiety-inducing red bar when they fail. you'll need an IDE with integrated JUnit support such as Eclipse. Neither the Swing nor the AWT test runners will be updated or bundled with JUnit 4.


    Why bother with GUI testrunners when you can just create a nice set of webpages containing your JUnit results in detail? That way anyone with a web browser can take a look. In your Ant buildfile, have JUnit output the results as XML, and use the junitreport task [apache.org] to automagically transform it to HTML.

    For a small amount of effort, you'll get something like this [apache.org].

Anyone can make an omelet with eggs. The trick is to make one with none.

Working...