7

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

3
  • 1
    What does "best" mean to you?
    – user1249
    Oct 18, 2010 at 17:31
  • Community wiki based on "Whats your favorite"?
    – Chris
    Oct 18, 2010 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, 2010 at 1:55

6 Answers 6

12

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? :-)

4
  • 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, 2011 at 5:53
  • 1
    Wow, that is the most awesome thing I've seen in the last couple months. Mar 23, 2011 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 May 8, 2011 at 18:14
  • Just in case sombody comes acros this. Specs is about to be 'replaced' by Specs2 etorreborre.github.com/specs2 Mar 20, 2013 at 11:35
6

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.

2
  • 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, 2010 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, 2016 at 10:03
2

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
  )
}
2

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.

1

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.

0

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))));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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