JUnit-Nested - Testing tool for Java
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea Update dependencies (junit-4.11 and others) Aug 14, 2013
.classpath Imported template from avh4/java-1.6-archetype Oct 21, 2011
.gitignore Ignore .idea/workspace.xml Apr 23, 2012
.project Rename project to junit-nested May 23, 2012
.travis.yml Configure project for travis-ci. Jun 17, 2012
LICENSE.txt Change license to Common Public License May 23, 2012
README.md Update README for 1.0.1 release Aug 14, 2013
junit-nested.iml Update dependencies (junit-4.11 and others) Aug 14, 2013
pom.xml [maven-release-plugin] prepare for next development iteration Aug 14, 2013


Build Status

Nested tests for JUnit

Add the following dependencies to your pom.xml:


Now write nested tests as shown below. Your test class can have non-static inner classes, each of which may contain @Test methods. The inner classes themselves can also have inner classes, nested to any depth (though you may want to restructure your tests if you find yourself needing more than two levels). @Before and @After blocks at all levels will be executed as you would expect (for example, Outer-Before, Inner-Before, Test, Inner-After, Outer-After). @Rules at all levels will also be applied in the appropriate order.

import net.avh4.test.junit.Nested;

public class QueueTest {
    private Queue<String> subject;

    public void setUp() {
        subject = new Queue<String>();

    public void push_shouldIncreaseCount() {
        assertThat(subject.count(), is(1));

    public class WhenEmpty {
        @Test(expected = NoSuchElementException.class)
        public void pop_shouldThrow() {

    public class WithOneItem {
        public void setUp() {

        public void pop_shouldReturnTheItem() {
            assertThat(subject.pop(), is("First"));

What works

  • @Before methods at all levels
  • @After methods at all levels
  • @Rule fields at all levels
  • @Test(expected) for expected exceptions
  • @Test(timeout) for test timeouts
  • @Ignore for pending tests

What doesn't work (yet)

  • @BeforeClass methods for outer classes
  • @AfterClass methods for outer classes
  • @Rule methods (however, @Rule fields do work) for outer classes
  • @RunWith on inner classes


junit-nested is licensed under the Eclipse Public License version 1.0. If you have need of a different license, please contact me.


Below are some resources I investigated before writing junit-nested, which offer some different approaches to behavioral testing in Java (and other languages).