-2

into a JUnit test cla I have this extremelly simple method:

public void stupidTest() {
    assert(false);
}

The strange thing is that performing this method I obtain a GREEN BAR and not a red one as I expected (I think that assert(true) have to give me a green bar and assert(false) a red bar).

Why? What am I missing?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • to follow @assylias : you can use assert, but by default, they're disabled. Either enable the asserts (not recommended) or use the assert methods provided by your testing framework (here, JUnit) – Stultuske Jul 19 '16 at 10:48
  • 1
    Possible duplicate of [What does the Java assert keyword do, and when should it be used?](http://stackoverflow.com/questions/2758224/what-does-the-java-assert-keyword-do-and-when-should-it-be-used) – kryger Jul 19 '16 at 11:06
  • 1
    Hint: if you are just starting with JUnit, there is only **one** assert you want to learn about, and that is **assertThat**. No need to look any assertTrue, ...False, ...Whatever. – GhostCat Jul 19 '16 at 12:37

2 Answers2

3

You are thinking in assertTrue and assertFalse. Those one checks if the parameter is true or false when running a JUnit.

Check the API

assert in java, as you are using it, it's only taken into consideration when compiling with an special option, not when running the test

SCouto
  • 7,808
  • 5
  • 32
  • 49
2

assert is a Java keyword: junit doesn't care about it (and it will only be considered by the JVM if you run with the -ea option).

If you want to run a junit test, you should use assertTrue(false); which is a junit method. That will give you a red bar.

assylias
  • 321,522
  • 82
  • 660
  • 783