1

I'm trying to get into testing using JUnit and I struggle to understand what I'm doing wrong. I made something pretty simple:

A class that should be able to create something. So far I'm just testing if the object it gets is null or not.

public class TestedClass {
    public void create(Object o) {
        if (o == null) {
            throw new IllegalArgumentException();
        } else {
            System.out.println("Not null!");
        }
    }
}

The tester class, it creates a new TestedClass object and tries to create something with null. It expects a IllegalArgumentException.

import org.junit.Test;
public class Tester {
    @Test(expected = IllegalArgumentException.class)
    public void createWithNullShouldThrowException() {
        TestedClass t = new TestedClass();
        t.create(null);
    }
}

Just the main class.

public class Main {
    public static void main(String[] args) {
        Tester test = new Tester();
        test.createWithNullShouldThrowException();
        System.out.println("passed all tests!");
    }
}

The way I understand it, it should terminate properly if there's an IllegalArgumentException thrown during the whole testing procedure. Which is the case as my program is terminating with:

Exception in thread "main" java.lang.IllegalArgumentException
    at TestedClass.create(TestedClass.java:4)
    at Tester.createWithNullShouldThrowException(Tester.java:6)
    at Main.main(Main.java:7)

Which shouldn't happen since it should get caught by the createWithNullShouldThrowException() method in the Tester class, or am I not understanding this correctly? I know that I could probably do this with a try catch block instead but I just want to know what's wrong in this case. If it's any help, I'm using IntelliJ IDEA 16.1. Any help would be appreciated.

skulpt
  • 527
  • 2
  • 6
  • 25
  • you dont have to create an instance of tester in main method. In eclipse you can run the test class by right clicking the file and then Run As->JUnit test. I am pretty sure there is something similar is Intellij. – Rahul Sharma Mar 22 '16 at 13:41
  • Yup. Don't run it through `main()`, run it through jUnit. It works perfectly when run correctly. – Erick G. Hagstrom Mar 22 '16 at 13:42
  • An aside: don't use `@Test(expected = IllegalArgumentException.class)`. Use `ExpectedException` instead. See http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests?rq=1, but ignore the accepted answer. This gives you better control over detecting the exception. – Erick G. Hagstrom Mar 22 '16 at 13:45

1 Answers1

5

You don't need the main class/method. Unit tests should be run via the unit test runner. In Intellij Idea you can right click on a class with @Test annotations and click the 'Run' menu item or click on the little run icon by each test or click at the root of the tests folder and click 'Run all tests'.

In a maven project, if the test is in the right place you can type mvn test. Other build tools will contain a way to run the test suite. As will other IDEs.

If you want to have a main method then you should start up the junit test framework by calling the correct test runner. However it is very very unlikely you actually want to do this.

Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81