1

This seems too easy and obvious, but I cannot find out how to test the following:

My Java method:

public void doSth(final Foo foo) {
    assert foo != null;
    ..
}

My test:

 @Test
 public void testAssertion() {
      doSth();
      doSth(new Foo());
 }

I can test quite many things with assertEquals etc., but how to create a test case that gives true if that assert there on foo != null; gives negative?

The test I have now goes on green in both cases of assertion, but I cannot catch the assertion fails or not.

I want my code test coverage to 100% and would like to test something meaningful on this line.

mico
  • 12,730
  • 12
  • 59
  • 99
  • 1
    If `doSth();` is part of your production code, it is bad practice to use asserts because they are disabled by default. it should be refactored to use exceptions. – Ferrybig Feb 11 '16 at 08:46
  • Did you try with assertNotNull(foo) ? – Nikolay Tomitov Feb 11 '16 at 08:46
  • The thing is assertion gives now "3/4 branches not covered" if I run the given test, so I would like to know are there any chances to get 4/4. How ever useful that is but for curiosity. I would introduce here term "coverageTesting" and it is what I aim with this. – mico Feb 11 '16 at 08:49
  • Possible duplicate of [How do you assert that a certain exception is thrown in JUnit 4 tests?](http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests) – Jérémie B Feb 12 '16 at 19:29
  • The thing is not exactly about asserting exceptions, but testing the assert command is fired or not. Maybe I miss something, but testing that assert is failing is different than test some exception. Although it is not desired thing to happen normally (=highly exceptional) but for my eyes assert and exception are different concepts. – mico Feb 12 '16 at 19:50

2 Answers2

7

assert throws an AssertionError if the assertion is false.

If you want to test that an assertion occurs when the argument is null, you can do it with the expected argument of the @Test annotation (requires JUnit 4+):

@Test(expected = AssertionError.class)
public void testAssertion() {
    doSth(null);
}
Raphaël
  • 3,646
  • 27
  • 28
  • This did not affect to coverage percent, but now I at least know how to "test" asserts. Thanks for insights. – mico Feb 11 '16 at 09:13
2

Using assert in your code is a bad practice.

Try to use Java 8 Objects.requireNonNull()

Java Class

public void doSth(final Foo foo) {
    Objects.requireNonNull(foo);
    ..
}

Test Class

@Test(expected = NullPointerException.class)
public void testNPE() {
     doSth(null);
}
  • It's not good to use `assert` in this particular case, i.e. for argument checking in public methods (see [Oracle guide](https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html)), but it's definitively not bad practice in general. Have a look at [this](http://stackoverflow.com/questions/2758224/what-does-the-java-assert-keyword-do-and-when-should-it-be-used) question. – beatngu13 Oct 25 '16 at 19:22