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."
I don't know if I am the only one thinking this... (Score:5, Funny)
Re:I don't know if I am the only one thinking this (Score:1, Funny)
Re:I don't know if I am the only one thinking this (Score:1)
Marcy: "Say it! Say it!!!"
Jefferson: "J-J-J...Joooob"!
?
Re:I don't know if I am the only one thinking this (Score:3, Interesting)
Your not the only one (Score:4, Funny)
Envy me, I'm programming's MVP!
Re:Your not the only one (Score:2, Funny)
Re: (Score:1)
Re:I don't know if I am the only one thinking this (Score:5, Informative)
Re:I don't know if I am the only one thinking this (Score:2)
Too bad (Score:5, Interesting)
Re:Too bad (Score:1)
Re:Too bad (Score:2)
The parent is referring to a GUI to run the tests [informit.com], not a test to test a GUI.
Re:Too bad (Score:1)
Re:Too bad (Score:2)
This is annoying enough that somebody's going to just hack one out, unless there's some technical reason why it can't be done that's eluded me...
RE: Too bad (Score:2, Informative)
Why bother with GUI testrunners when you can just create a nice set of webpages containing your JUnit results in detail? That
Re:Too bad (Score:2)
It was my silent protest against the culture where it was "ok" if not quite all the tests passed.
"How are the tests? - still pink!"
Testing (Score:1)
Re:Testing (Score:3, Informative)
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
Re:Testing (Score:3, Insightful)
Re:Testing (Score:3, Insightful)
Look around you (Score:2)
Where was the headline when NUnit was released? (Score:1, Insightful)
Re:Where was the headline when NUnit was released? (Score:3, Informative)
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:Where was the headline when NUnit was released? (Score:2)
In other words, Java added a new feature to the language in Java 5 (called "annotation") which was something that
Re:Where was the headline when NUnit was released? (Score:1)
I was addressing the fact that the xUnit community (at least the various folks that like/use/support the xUnit-style implementations, don't look at NUnit being a "rip-off" of JUnit. I think its great that NUnit initially learned from JUnit (and they from the smalltalk, c++ versions,
Re:Where was the headline when NUnit was released? (Score:2)
I try to stay neutral on the "X" ripped off "Y" arguments. It turns out that a lot of the time when people "reinvent the wheel", they end up doing a slightly better job since they have the benefit of hindsight.
If you do any
Re:Where was the headline when NUnit was released? (Score:1)
When Microsoft copies something from Apple, there's a huge dustup. When the Java VM starts catching up to CLR features (I have used both extensively) it's "innovating". They both copy each other a lot, obviously, but I get a bit tired of the one-sidedness sometimes.
Re:Where was the headline when NUnit was released? (Score:2, Informative)
In that sense, all the other xUnit stuff is decidedly a descendant of JUnit.
Re:Where was the headline when NUnit was released? (Score:3, Informative)
Re:Where was the headline when NUnit was released? (Score:2)
The original poster's point was, NUnit (and csUnit) both support unit tests marked up with
The point was lost in the vitriol and flame but I think it still stands.
Just when I'd almost gotten comfortable (Score:2, Funny)
Actually: Nice work, guys. I'll probably appreciate this once I get a chance to use it and wrap my head around it.
Try TestRe:Just when I'd almost gotten comfortable (Score:1)
http://testng.org/ [testng.org]
Java 5, JUnit4 (Score:3, Interesting)
I'm looking at the samples and am left scratching my head. I don't see any difference in the various example tests they show. Maybe someone can explain this "annotation" and how it is better (it's certainly more verbose!) than the normal way of doing things.
Re:Java 5, JUnit4 (Score:3, Interesting)
a) reduces the method name length slightly, there are various tools that aren't up to handling super long method names.
b) more importantly, can simplify your naming conventions
way way way more important is that
extends TestCase
is no longer necessary, which since Java is single inhertiance model now means you can extend something else with your test classes. As one significant example, think about writing te
Re:Java 5, JUnit4 (Score:3, Informative)
JUnit4 ? Goddess ? (Score:2, Funny)
Just kidding, JUnit [wikipedia.org], one with capital U.
The Holy Grail (Score:3, Interesting)
Re:The Holy Grail (Score:4, Funny)
It would have to use microphones because, in my experience, you don't get a written requirements spec. Or if you do, customers don't feel constrained by it.
It would also have to raise a red flag when the customer contradicts themselves in the same sentence or paragraph.
But all kidding aside, JUnit is cool.
For intricate portions of my code I write tests that represent specific scenarios and run regression tests whenever I have finished implementing the new rule of the day.
Re:The Holy Grail (Score:2)
Yes, JUnit is nice. All of the XUnits are nice. They address one of the major problems in software development, which is the constant divergence of tests and code. But there are other ways to address that problem. The method that I subscribe to is this: don't allow developers to check in code until it passes all existing tests. It takes come descipline, but it works.
By the way don't forget to patent that microphone idea.
Re:The Holy Grail (Score:5, Funny)
Then you just write the tests and let the code "evolve" until it passes them. Meanwhile, you get to sit around and drink beer.
More on Elliotte (Score:5, Informative)
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.
Could we cut down at manager speak here (Score:2, Insightful)
Re:Could we cut down at manager speak here (Score:4, Funny)
Re:Could we cut down at manager speak here (Score:2)
No, it simply doesn't leverage any paradigm leading to go-to market, customer focused synergies.
Re:Could we cut down at manager speak here (Score:1)
Americana (Score:2)
Re:Americana (Score:2)
On a related note, the word 'soccer' was introduced by the British, as well. It seems that the move towards "-ise" and football are attempts by the Brits to appear more like the French.
Re:Americana (Score:2)
JUnit and the people who don't use it... (Score:3, Insightful)
What continues to stun me about the "professional" developers out there is how few do Unit Testing even when it is so easy. People complain about jobs moving offshore and pressure on delivery and people not understanding how hard coding is... but they don't even Unit testing.
If you don't unit test then you aren't a software engineer, you are a typist who understands a programming language.
Re:JUnit and the people who don't use it... (Score:4, Insightful)
Re:JUnit and the people who don't use it... (Score:1, Troll)
Normally, writing junit tests is less then or equal to the amount of work in writing the actual code
Where as fixing the bugs as a result of a lack of unit testing doesn't take any effort at all.
You muppet.
Re:JUnit and the people who don't use it... (Score:2)
Re:JUnit and the people who don't use it... (Score:2)
Fixing bugs as a result of a lack of unit testing does take effort. In some cases, bugs make it to production. But in the case of enterprise scale web applications backed by complex databases, Unit Testing takes 2 - 3 times as much work. It slows down development as you are not only updating test code along with your application code, but you are also updating your schema *and* your test data.
Don't think I'm not for
Re:JUnit and the people who don't use it... (Score:2)
- you have complex web GUI that cannot be emulated with test frame work.
- you have complex database schema backing your application.
It certainly can work in those situations, as developers have been doing this for years.
You can use HTTPUnit to automatically generate http requests, form data, and to valid
Re:JUnit and the people who don't use it... (Score:3, Insightful)
(* No, nothing is
mock objects (Score:3, Interesting)
Using mock objects makes unit testing servlet code pretty easy. Keeping your objects reasonable in size and single purposed in function also simplifies testing. If a servlet is so complex that its hard to test then, that is a sign that it should be broken up and delegate to smaller classes with simpler methods that are easy to test.
JSP's should not have any logic in them, or if they do, it should be
Try Selenium (Score:1)
http://selenium.thoughtworks.com/index.html [thoughtworks.com]
Re:JUnit and the people who don't use it... (Score:2)
It's incredibly easy. One of the reasons why I like the framework so much.
Re:JUnit and the people who don't use it... (Score:2)
Anyway, a great tool for testing web applications is Selenium. Look for the link in another response to your post.
Fh
I'm opposed to Unit Testing (Score:3, Insightful)
However, I see very little effort put into end-to-end system tests, and that's a shame. The really tricky bugs come from module/process interaction. Furthermore, unlike Unit Tests, system tests reflect the end-user experience. At one place where I worked, the software was just p
Re:I'm opposed to Unit Testing (Score:2)
the customers loved the product
You call a thoroughly tested piece of software loved by its customers crap?
Do you work at Microsoft where buggy, hated sofware is called high quality?
Re:JUnit and the people who don't use it... (Score:2)
I think it's the other way around -- if you think passing unit tests means your code is perfect, then you are a typist who understands a programming language.
Here's a simple example:
int add(int a, int b) { return a * b; }
boolean testAdd(){ if(add(2,2) == 4) return pass; else return fail; }
Hey, the test passed, your code must be correct, right?
If you do manage to test all possible cases th
Not yet with Java5 (Score:2)
But why do they not want to ship this new JUnit 4 with the JUnit GUI runner? Did they decide to split the projects?
From the blog I can see that JUnit 4 will not be able to differentiate between the anticipated errors from asserts and unanticipated errors from code - now that will prevent me from converting to JUnit 4 even if I move to Java 5.
Re:Not yet with Java5 (Score:1)
Re:Not yet with Java5 (Score:2)
I support at least one application that runs on 1.2.
Maybe, but it doesn't work with databases... (Score:3, Informative)
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.
Re:Maybe, but it doesn't work with databases... (Score:2)
As for the XML...I have mixed feelings about it but it makes sense when you think it through. In any event, DBUnit will export the XML from your db for you.
Re:Maybe, but it doesn't work with databases... (Score:5, Interesting)
It requires you to define the way you get your database connection through Spring, but that abstraction is necessary for unit testing DB-driven apps anyway. On one of my projects, I have one set of bean descriptions for unit testing which connects right to the DB and one set of beans for when the app is running in a Tomcat container. It's not a perfect method, that's for sure, but it allows me to unit test my code pretty painlessly once it's set up.
Re:Maybe, but it doesn't work with databases... (Score:2)
Huh? When you get above the data access layer, you create a mock DAO that implements the interface and use that to test your business objects. Christ, that's the whole damned reason why Spring is such an excellent unit test enabler: you can easily mock out interfaces and test components in isolation. Which is the very definition of unit test (as opposed to integration or system-level test).
Re:Maybe, but it doesn't work with databases... (Score:2)
It's not so bad. The application I'm working on at work is backed by a database, and all we do is keep around a bunch of precooked databases, take a copy of the database (copying the files is enough) and run all the tests on that.
One of the ways you can cheat the system is if you have the capability to control the transactions from the unit tests, you can set a save point before the unit test and roll back to it after the unit test, so that the next tests don't get trash data from the previous ones.
And y
Re:Maybe, but it doesn't work with databases... (Score:1)
Regards,
Steve
Re:Maybe, but it doesn't work with databases... (Score:1)
Re:Maybe, but it doesn't work with databases... (Score:2)
Get your hands on Working Effectively with Leagacy Code by Michael C. Feathers. And do it soon.
Don't let the term "legacy code" in the title throw you off. Feathers calls any code that is not covered under automated testing "legacy code." On p. 17 of the book, Feathers gives an example of how to test code that currently requires hitting a databa
Web / GUI (Score:2)
Can anyone recommend a good framework for testing these components?
Re:Web / GUI (Score:2)
We usually design the entire GUI to be properly separated into model/view in the first place, so that we can thoroughly test the model to the point where we know that no matter what bug might exist in the UI itself, the user won't be able to find it. ;-)
What you're looking for for web stuff is probably HttpUnit. There is an equivalent for Swing apps too, but we've tried it and it really took more time (10 times more) to write the tests than to write the code, so we decided against it.
Re:Web / GUI (Score:3, Informative)
It only helps you for web apps, though.
Re:Web / GUI (Score:2)
I have used JUnitEE [junitee.org] for some time. It provides a Servlet runtime for your tests/test suites. It makes JUnit alot easier to deal with for some type of testing.
Of course, if you are smart you will write an Ant script that will fire off tests for you (using conventional JUnit)... but that only works for some things and it depends on how well you write your tests.
Re:Web / GUI (Score:1)
It's better than HTTPUnit as it runs your app in a browser, rather than trying to emulate a HTTP client and failing to support complex JavaScript.
Supports Fit Test scripts.
Re:Web / GUI (Score:2)
Haskell.
Super off-topic (Score:1, Informative)
With Visual Studio
Re:Super off-topic (Score:2)
Probably because the links are broken.
Useless (Score:3, Interesting)
Fixtures is something that JUnit has been ignoring since its inception and thus it is much less appealing than it could be if the test fixture dillemma is ever solved.
Re:Useless (Score:2)
Your tests may be beyond the scope of "unit" tests. Integration tests maybe?
The code needs to be broken up into simpler units that are easier to test individually.
Regarding the first case, how would you validate the code without testing?
The second is just some code that requires refactoring.
Re:Useless (Score:2)
this is a cliche ofter repeated by the proponents of JUnit. Often times refactoring your code to make it compatible with JUnit results in the code structure more complex than if JUnit had been more transparent. This is bad refactoring as it flies in the face of simplicity as the overriding principle in software design.
I do have some ideas about how to dramatically overhaul the JUnit paradigm with Aspect Oriented unit
pre-junit (Score:2)
Maybe that "bad refactoring" isn't so bad after all. Code shouldn't be refactored to JUnit, it should be refactored to be well structured
Re:Useless (Score:2)
Re:Useless (Score:2)
Take your database example. In my experience using
Unit tests are becoming irrelevant (Score:1)
http://www.theserverside.com/news/thread.tss?thre
http://beust.com/weblog/archives/000319.html [beust.com]
Re:Unit tests are becoming irrelevant (Score:1)
been there, done that (Score:3, Informative)
Like JTiger [jtiger.org] has done for ages?
Re:been there, done that (Score:1)
Re:been there, done that (Score:2)
Java has had Javadoc annotations for years before
Re:been there, done that (Score:2)
Re:been there, done that (Score:2)
Why not just use TestNG? (Score:4, Informative)
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.
Re:Why not just use TestNG? (Score:2, Insightful)
Re:Why not just use TestNG? (Score:1)
I think there are places where XML makes sense and places where it doesn't. Overall though, I share your bias because it all adds up.
Having said that, TestNG's configuration file is almost trivial, especially compared to the XML I'm sure you already deal with if you're using any kind of frameworks, O/RM or any J2EE stuff.
Technically, TestNG does not even require an XML file - but I've found that in practic
Annotations use reflection, yes? (Score:1)
Check this paper (Score:5, Interesting)
I have used this tool during some time and it is amazing. It generates graphs of the code you are testing and it can be integrated with junit.
Check this paper for more details: http://portal.acm.org/citation.cfm?id=1072118.107
Re:JUnit4 (Score:1, Offtopic)
It at least deserves a "funny" mod