---
title: "JUnit 4 feels like NUnit with attribute-like syntax"
url: "https://osherove.com/blog/2007/3/7/junit-4-feels-like-nunit-with-attribute-like-syntax.md"
canonical: "https://osherove.com/blog/2007/3/7/junit-4-feels-like-nunit-with-attribute-like-syntax.html"
published: "2007-03-07T16:07:11"
updated: "2007-03-07T16:07:11"
tags: ["Agile", "Unit Testing"]
author: "Roy Osherove"
description: "I'm doing Test-Driven-Development with Java course, after having not done one for a long time, and I was browsing through my samples and online to see if anything interesting in JUnit land has happened since last time.…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# JUnit 4 feels like NUnit with attribute-like syntax

By Roy Osherove March 7, 2007 In [Agile](https://osherove.com/blog/category/agile), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published March 7, 2007; technical details may be outdated.

I'm doing Test-Driven-Development with Java course, after having not done one for a long time, and I was browsing through my samples and online to see if anything interesting in JUnit land has happened since last time. And there sure is!

JUnit 4 is out and allows using Java Annotations (Java's version of .NET Attributes if you will) to write tests. No need for inheritance anymore, or to begin test names with 'test". simply use annotations:

Here is an example takes from [this blog](http://java-x.blogspot.com/2007/01/unit-testing-with-junit-40.html), which has a more elaborate list of changes with examples:

**Test Annotations:**

```
@Test
public void emptyTest() {
 stack = new Stack<String>();
 assertTrue(stack.isEmpty());
}
```

(I'm using the cool [Code formatter live writer plugin](http://dunnhq.com/codeformatterforwindowslivewriter) )

Also, you get matching annotations for **setup and teardown**:

```
public class OutputTest {

        private File output;

        @Before
        public void createOutputFile() {
            output = new File(...);
        }

        @After
        public void deleteOutputFile() {
            output.delete();
        }

        @Test
        public void testSomethingWithFile() {
            ...
        }
    }
```

For some weird reason, it's possible to put this annotation on multiple methods in the same test fixture, and they will all run in an unknown order. Not sure why that is useful, or at all contributes to readability..

**You also get to test for expected exceptions more easily:**

```
@Test(expected=IndexOutOfBoundsException.class)
public void testIndexOutOfBoundsException() {
  ArrayList emptyList = new ArrayList();
   Object o = emptyList.get(0);
 }
```

**Parameterized Tests**

One feature I was pretty surprised to see, but rather glad that I saw it, the fact that, a bit like in [MbUnit](http://www.mbunit.com/), JUnit 4 has support for tests that can take parameters, and thus being able to run the test multiple times with different inputs.

Differently than JUnit though, you need to create a class for each Parameterized test you'd like to create, and put annotations on the class, and on the source of the parameters (data source, if you will). this allows a more data-driven-testing kind of way, which is a bit like what you can find in VSTS Unit Testing support.

```
@RunWith(value = Parameterized.class)
public class StackTest {
 Stack<Integer> stack;
 private int number;

 public StackTest(int number) {
   this.number = number;
 }

 @Parameters
 public static Collection data() {
   Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
   return Arrays.asList(data);
 }
 ...
}
```

**Test Suites**

Creating Test Suites with JUnit 4 is rather a weird task. You create an empty class with the sole takes of carrying annotations that point to the class list to run:

```
@RunWith(Suite.class)
@Suite.SuiteClasses({StackTest.class})
    public class AllTests 
    {
    }
```

## Lessons on this topic

- [Introduction to JUnit](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/introduction-to-junit/) · The Art of Unit Testing and TDD in Java

## More in Agile

[All posts in Agile](https://osherove.com/blog/category/agile)

- Mar 13, 2007

    ### [Test Reviews Vs. Code Reviews - Some Helpful Tips](https://osherove.com/blog/2007/3/13/test-reviews-vs-code-reviews-some-helpful-tips.html)

- Mar 9, 2007

    ### [Adobe uses Agile Development Successfully to build Photoshop CS3 product](https://osherove.com/blog/2007/3/9/adobe-uses-agile-development-successfully-to-build-photoshop.html)

- Mar 2, 2007

    ### [Testable Designs - Round 2: Tooling, Design Smells and Bad Analogies](https://osherove.com/blog/2007/3/2/testable-designs-round-2-tooling-design-smells-and-bad-analo.html)

- Feb 25, 2007

    ### [Why you should think about TOOP- Testable Object Oriented Programming](https://osherove.com/blog/2007/2/25/why-you-should-think-about-toop-testable-object-oriented-pro.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
