Nested classes with JUnit

Recently I was playing with JUnit 4.X.  I wanted to be able to define tests in nested classes as I had before with NUnit.  This was to facilitate BDD-ish test definitions, where I break up unit tests by test context.


My first attempt failed (of course) because JUnit was unable to see the nested classes and the failed to run.  After a bit of digging, I ran across a sparsely documented feature that allowed me to do what I wanted. 


Namely by using: @RunWith(Enclosed.class)


Below is an example, hope it’s of use:


package junitSampleTests;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Enclosed;
import junitSample.
*;

@RunWith(Enclosed.class)
public class SimpleFractionTest {
public static class WhenConstructingSimpleFractions {
protected SimpleFraction f1, f2;

@Before
public void setUp() throws Exception {
f1
= new SimpleFraction(15, 25);
f2
= new SimpleFraction(-27, 6);
}

@Test(expected=InvalidOperationException.class)
public void shouldNotAllowZeroForDenominator() throws Exception {
f1
= new SimpleFraction(1, 0);
}

@Test
public void shouldEnsureOnlyTheNumeratorHoldsTheFractionSign() throws Exception {
f1
= new SimpleFraction(1, -1);

assertEquals(-1, f1.getNumerator());
assertEquals(
1, f1.getDenominator());
}
}

public static class WhenUtilizingAccessors {
protected SimpleFraction f1, f2;

@Before
public void setUp() throws Exception {
f1
= new SimpleFraction(15, 25);
f2
= new SimpleFraction(-27, 6);
}

@Test
public void shouldGetNumerator() {
assertEquals(
15, f1.getNumerator());
assertEquals(
-27, f2.getNumerator());
}

@Test
public void shouldGetDenominator() {
int result = f1.getDenominator();
assertTrue(
getDenominator() returned + result + instead of 25., result == 25);
result
= f2.getDenominator();
assertEquals(
6, result);
}
}

public static class WhenSimplifying {
protected SimpleFraction f1, f2;

@Before
public void setUp() throws Exception {
f1
= new SimpleFraction(15, 25);
f2
= new SimpleFraction(-27, 6);
}

@Test
public void shouldSimplify() {
f1.simplify();
assertEquals(
3, f1.getNumerator());
assertEquals(
5, f1.getDenominator());
}

@Test
public void shouldSimplifyWhenNoSimplificationIsPossible() throws Exception {
f1
= new SimpleFraction(15, 29);

f1.simplify();
assertEquals(15, f1.getNumerator());
assertEquals(
29, f1.getDenominator());
}

@Test
public void shouldSimplifyNegativeFractions() {
f2.simplify();
assertEquals(
-9, f2.getNumerator());
assertEquals(
2, f2.getDenominator());
}

@Test
public void shouldSimplifyWhenNumeratorIsZero() throws Exception {
f1
= new SimpleFraction(0, 29);
f1.simplify();
assertEquals(
0, f1.getNumerator());
assertEquals(
29, f1.getDenominator());
}
}
}

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Joshua Lockwood

I code stuff and people pay me for it.
This entry was posted in Java, JUnit, Unit Tests. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>