11

I haven't looked at this in a while, but if I recall correctly both ant and maven still rely on JUnit 3 for unit tests (as of maven 2.09 the default POM file still has JUnit 3.81).

Does it still make sense to stick to JUnit 3 instead of using the latest and greatest? Any good reason I might be missing?

agnul
  • 12,608
  • 14
  • 63
  • 85

6 Answers6

9

I don't see a reason to stick to the 3.x versions. Most tools have been compatible with 4.x for a while now. The only reason I would stick to 3.x is in a java 1.4 environment (because there is no other way).

By the way, maven is switching to Java 5 in 2.1, so there is a chance they will propose junit 4.x

Mike Pone
  • 18,705
  • 13
  • 53
  • 68
Somatik
  • 4,723
  • 3
  • 37
  • 49
8

JUnit 4 has lots of advantages over 3.x. The most important is that you no longer have to extend TestCase, nor do your test methods have to begin with "test." It's all annotation-based now. You can also add the Hamcrest matchers, which gives you a really nice and expressive way of writing test assertions.

If you're stuck on a pre-Java-1.5 project, you may have to stick with JUnit 3.x, though.

Joey Gibson
  • 7,104
  • 1
  • 20
  • 14
6

There are some reasons for JUnit 4.x and some against it.

Pros:

  • The annotations are easy to understand and increase readability.
  • Ignoring tests are easier.
  • You can name methods as you like.

Cons:

  • You need Java 5 (annotations).
  • Migration probably needed(?).

I use JUnit 4, because it's nicer, but that's my personal opinion.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
guerda
  • 23,388
  • 27
  • 97
  • 146
4

Ant doesn't reply on JUnit 3.

I'd recommend moving to the new JUnit for the simple reason that you're going to see more and more tools and examples that are JUnit4 only. Sticking with JUnit3 means living in a dwindling ecosystem.

Jeffrey Fredrick
  • 4,493
  • 1
  • 25
  • 21
4

Both Ant and Maven can run JUnit 4 tests just fine. The only reasons to use JUnit 3 are legacy systems: Either you're force to use a Pre-Java 5 JDK or you have extensive tooling that depends on JUnit 3 and doesn't support JUnit 4.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
3

Maven has no explicit binding to Junit 3.81. You can switch to any version of junit you want. If you're running java 5 I think you should switch to at least 4.4 Spring 2.5 is not compatible with 4.5.

krosenvold
  • 75,535
  • 32
  • 152
  • 208
  • Also, if you're using maven, you should probably explicitly state what source/target settings you want, by explicitly configuring the maven-compiler-plugin in your POM – toolkit Dec 01 '08 at 14:01
  • I know I can setup maven to use whatever version of JUnit I prefer, but the default for archetype:create is still 3.81. Just wondering if there's any good reason. – agnul Dec 01 '08 at 14:12