0

Something's missing in my test case linked below to get back some ConstraintViolation after setting null to a method parameter annotated with @NotNull.

https://github.com/sourcefranke/fibonacci/blob/master/src/test/java/fibonacci/FibonacciTest.java

At the moment there's no ConstraintViolation at all I get back a result, but there should be at least one violation related to the @NotNull constraint. What did I forget here? Thank you!

cmb28
  • 421
  • 9
  • 24

1 Answers1

0

Although you generate a validator in your test you don't make use of it. As your annotations are on a method call it's a bit fiddly. You have to use reflection to get the method and pass the values in via an array.

@Test
public void generateList_lessThan3() throws NoSuchMethodException, SecurityException {
    // when(fibonacci.generateStream(any())).thenReturn(Stream.of(BigInteger.ONE, BigInteger.ONE));

    Method method = Fibonacci.class.getMethod("generateList", Integer.class);
    // fibonacci.generateList(null);

    Set<ConstraintViolation<Fibonacci>> violations = validator.forExecutables().validateParameters(fibonacci,
            method, new Object[] {null});
    Assertions.assertEquals(1, violations.size());
    System.out.println(violations);
}
pcoates
  • 2,102
  • 1
  • 9
  • 20