I've used TestNG and JUnit. What other frameworks are out there? What makes them special and/or different from the rest?

  • 1
    What does "best" mean to you? – user1249 Oct 18 '10 at 17:31
  • Community wiki based on "Whats your favorite"? – Chris Oct 18 '10 at 17:41
  • 'Best' to me means: 1. Easiest to implement 2. Easiest for someone else to understand. 3. Easiest to extend (in other words, to create extensive suites of tests). – codeyoung Oct 19 '10 at 1:55

Scala Specs gets my vote! :-)

Specs is a behavior-driven-development testing framework written in Scala. It can be used to write tests for Java and Scala. It was inspired by RSpec - a testing framework very popular in the Ruby world.

An example test written in Specs:

import org.specs._

object ElementSpecification extends Specification {
  "A UniformElement" should {
    "have a width equal to the passed value" in {
      val ele = elem('x', 2, 3)
      ele.width must be_==(2)

    "have a height equal to the passed value" in {
      val ele = elem('x', 2, 3)
      ele.height must be_==(3)

    "throw an IAE if passed a negative width" in {
      elem('x', 2, 3) must throwA(new IllegalArgumentException)

Impressive, isn't it? :-)

| improve this answer | |
  • Yep - gotta say I love using Scala Specs to test my Java apps at work. Works with Maven, great to write, great output. And you can write the spec without implementing it to serve as sort of a checklist, and it shows up in Hudson for all to see as expected functionality. – Janx Jan 6 '11 at 5:53
  • 1
    Wow, that is the most awesome thing I've seen in the last couple months. – Oliver Weiler Mar 23 '11 at 14:53
  • No matter how much I look at RSpec-like code I can't understand how people stand "should be" "must be" etc – alternative May 8 '11 at 18:14
  • Just in case sombody comes acros this. Specs is about to be 'replaced' by Specs2 etorreborre.github.com/specs2 – Nils Schmidt Mar 20 '13 at 11:35

I suggest to use TestNG as core unit test framework for Java project, because TestNG is more advance in parameterize testing, dependency testing and suite testing (Grouping concept). TestNG is meant for high-level testing and complex integration test. Its flexibility is especially useful with large test suites. In addition, TestNG also cover the entire core JUnit4 functionality. It’s just no reason for me to use JUnit anymore.

| improve this answer | |
  • 9
    I've read the TestNG book. They spend all their time saying how it is better than junit, which annoyed me so much that we went with JUnit. – user1249 Oct 18 '10 at 17:31
  • TestNG itself has almost no tests. And when I was debugging TestNG the quality of the code is... hard to read and trace. – dzieciou Jan 11 '16 at 10:03

I can recommend scalacheck (easily confused with scalatest).

You write Specifications, and the framework builds the testcases randomly for you, and when violating a check it will try to simplify the test, to find the simplest way to fail.

Sample code from the website:

object StringSpecification extends Properties("String") {
  property("startsWith") = Prop.forAll((a: String, b: String) => (a+b).startsWith(a))

  // Is this really always true?
  property("concat") = Prop.forAll((a: String, b: String) => 
    (a+b).length > a.length && (a+b).length > b.length

  property("substring") = Prop.forAll((a: String, b: String) => 
    (a+b).substring(a.length) == b
| improve this answer | |

A whole new world of testing frameworks for Java exists via JRuby. For example: Test::Unit, RSpec, Shoulda, ZenTest, RSpec Mocks, Mocha and Cucumber.

I think it is great to write Java tests in the elegant Ruby language. Of course, this depends if you are in the mood of having multiple languages in your SCM.

| improve this answer | |

If you need this for choosing a unit test framework, then I would suggest the following:

Choose the one the rest of your team uses.

If your team does not use one, or you do not have a team, use the one your IDE supports out of the box in the default configuration.

If you still have not decided, then start with JUnit 4 which is the de-facto standard.

| improve this answer | |

With recent JUnit versions, Hamcrest was integrated into the library. Hamcrest adds additional matchers which improve readability but otherwise doesn't change the semantics of your tests. It therefore might be a good addition for your test setup.

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;


Person tom = new Person("Tom");
Person jerry = new Person("Jerry");

assertThat(tom, is(not(equalTo(jerry))));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.