1

This is my first JUnit test and I don't understand why is not throwing an AssertionError, what am I doing wrong??

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.JUnitCore;

public class MyFirstJUnitTest {

    public static void main(String[] args) {
        JUnitCore.runClasses(MyFirstJUnitTest.class);
    }

    @Test
    public void simpleAdd() {
        int a = 5;
        int b = 3;

        int c = a + b; //8

        Assert.assertTrue(c == 7);
    }
}
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154

3 Answers3

2

To run JUnit from the command line you need to call the main method.

JUnitCore.main("MyFirstJUnitTest");

You are not supposed to use JUnitCore unless you need to access the result in a programmatic way, for instance if you are writing a JUnit plugin for an IDE:

JUnitCore.runClasses(MyFirstJUnitTest.class).getFailures();

JUnitCore catches any exceptions and stores them in the Result, which is a class that your JUnit plugin will read.

Community
  • 1
  • 1
Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • `getFailures()` does the job! Still, this is not how it is described in the basic JUnit cookbook: http://junit.sourceforge.net/doc/cookbook/cookbook.htm ... Do you recommend running tests from the command line? – Marsellus Wallace Nov 04 '11 at 15:17
  • Personally, I like the [JUnit plugin for Eclipse](http://stackoverflow.com/questions/59128/what-gui-should-i-run-with-junitsimilar-to-nunit-gui) because it gives great graphical output. Most Java IDEs should have a JUnit plugin if you are using an IDE. Otherwise, I would just run tests from the command line unless you want to format your own output or create your own JUnit GUI. – Garrett Hall Nov 04 '11 at 15:24
1

AssertionError is in this case caught by test runner.

Normally, AssertionErrors are thrown by failing assertions made with assert keyword. This:

public static void main(String[] args) {
    int a = 5;
    int b = 3;

    int c = a + b; //8

    assert c == 7;
}

throws an AssertionError as expected, when running with assertion checks enabled.

socha23
  • 10,171
  • 2
  • 28
  • 25
  • Let me try to understand.. Isn't `assert` a Java keyword that is totally independent from the JUnit framework?? Also, your code above (with assert) does not throw an exception with me... :/ – Marsellus Wallace Nov 04 '11 at 15:02
  • Yes, assert is a Java keyword independent from the JUnit. How are you running my code? Did you pass `-ea` argument to the JVM? – socha23 Nov 04 '11 at 15:08
  • JUnit assertions also throw AssertionErrors, but those errors are caught by JUnit framework classes. As others said, JUnit allows access to failures in a different way, and normally you simply look if tests passed or failed in your IDE. – socha23 Nov 04 '11 at 15:11
  • Nope, I run it from my IDE, I was just following the basic instructions in the JUnit cookbook: http://junit.sourceforge.net/doc/cookbook/cookbook.htm What does `-ea` do? – Marsellus Wallace Nov 04 '11 at 15:15
  • It enables assertion checks created with `assert` keyword. Without this switch, they are not run. – socha23 Nov 04 '11 at 15:16
-1

Assertions are not for throwing exceptions but for checking if your condition is correct. So this will show you, that something went wrong (in your JUnit-view in your IDE) but not throw any exceptions.

dwalldorf
  • 1,379
  • 3
  • 12
  • 21
  • 1
    I'm using NetBeans, do you know where's the 'JUnit-view'? By the way, in JUnit4.10 javadoc there is the following about the assertTrue(boolean condition) function: `If it isn't (true) it throws an AssertionError without a message.` – Marsellus Wallace Nov 04 '11 at 14:52
  • Yes, but the AssertionError is what should be displayed in your JUnit-View. [Here](http://netbeans.org/kb/docs/java/junit-intro.html) is a tutorial for JUnit in NetBeans. Should help you to find the right view :) – dwalldorf Nov 04 '11 at 14:56
  • 1
    NB: It is correct that no exception is thrown. It is a subclass of `java.lang.Error` that is thrown. – DerMike Nov 04 '11 at 15:08
  • Who is downvoting without comment? Why? – dwalldorf Nov 04 '11 at 15:18