Using TestNG with Spring

Testing Spring in JUnit is fairly simple given the test runner support. Using TestNG is a little bit more complex, but this tutorial will quickly get you up to speed.

Moving to TestNG from JUnit

A few weeks ago I published a Simple Introduction to Spring Unit testing using JUnit. We’ve been using JUnit quite a bit in our examples so far, but as some of you may already know, JUnit is not the only unit test framework for Java. Another framework, TestNG, offers a lot of features that JUnit lacks, such as the ability to run parameterized tests in which a given test is run against an array of different values, and the ability to break tests down into categories coupled with fine-grained configuration control over what tests to run. Indeed, the author of TestNG (“Next Generation”) very much had the limitations of JUnit in mind in writing the new framework. You can read about some of the problems with JUnit that TestNG was designed to solve on it the TestNG web site.

Of course, the choice of which technology to use is often a matter not of what’s best, but what’s most popular (especially if you’re early in your career and need to get certain key technologies added to your resume). So for my wife, who’s just beginning her technical career as a tester, I’d be inclined to recommend JUnit, whereas for myself, old fart programmer that I am, I have the luxury of picking the technology I consider better in many cases. For example, compared to IntelliJ Idea, for me Eclipse is just too painful — free or not, big market share or not. So I use IntelliJ. So if you’re playing “Dice Keyword Bingo”, stick with JUnit, otherwise do yourself a favor and take a look at TestNG.

Setting Up The Maven Dependency for TestNG

Rather than repeat all the sample bean code from our [original JUnit article](=), let’s focus here on the tests. The original sample code is, after all, not very interesting — it only gave us something to test. To migrate the JUnit sample, the first thing I needed to do was change the dependency section in our POM file slightly. Remove the JUnit dependency:

Now add the dependency for TestNG:

For the benefit of those of you starting from scratch, the here is the full POM file.

Some Simple AbstractTestNGSpringContextTests Examples

Next, let’s see what we need to do to use Spring’s basic TestNG testing support class, AbstractTestNGSpringContextTests. Don’t let the “Abstract” in the name scare you off here, there’s really nothing much to implement. All you need to do is add a ContextConfiguration annotation to your class and it just works. Here’s the Hello-World-simple example, which only uses the ContextConfiguration annotation to get it to compile and run.

Modifying our JUnit tests to work with TestNG was just as easy. The @RunWith annotation was no longer needed, being replaced by the fact that we’re extending AbstractTestNGSpringContextTexts. So pretty much the only migration we did involved changing JUnit imports to TestNG ones. We removed:

We replaced it with:

Here for example is a little bit longer example, that exercises some of the test beans we’ve wired up in our application context:

Getting started using the Spring TestNG support turned out to be really easy. Check our our IntelliJ project and the full source on GitHub.

Leave a Reply