I have the next hierarchy:
public class A extends B {
@Valid
public A(String languageCode, C token) {
super(languageCode, token);
}
}
The B class has a property call language code that should be validated with @NotEmpty only if a service has the @Validated({RequiringLanguageCode.class})
public class B extends D {
private String languageCode;
@Valid
public B(String languageCode, C token) {
super(token);
this.languageCode = languageCode;
}
@NotEmpty(groups = {RequiringLanguageCode.class})
public String getLanguageCode() {
return languageCode;
}
}
Now, D is the base class that has a C property that should be validated as NotNull and also the values that are inside the C class.
public class D {
private C token;
@Valid
public D(C token) {
this.token = token;
}
@NotNull
public C getToken() {
return token;
}
}
C class contains two String that are validated as @NotEmpty:
public class C {
private String value1;
private String value2;
public C(String value1,
String value2) {
this.value1 = value1;
this.value2 = value2;
}
@NotEmpty
public String getValue1() {
return value1;
}
@NotEmpty
public String getValue2() {
return value2;
}
}
When trying to test this using mockito the values of C class aren’t validated if the token values (value1 and value2) are empty.
Can somebody help me? Does somebody have any idea about what is happening?
The test are as followed:
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
public class ATest {
@Autowired
private AAssembler aAssembler;
@Mock
A request;
@Mock
C token;
@Test
public void test() {
when(token.getValue1()).thenReturn("");
when(token.getValue2()).thenReturn("key");
when(request.getToken()).thenReturn(token);
assertThatIllegalArgumentException()
.isThrownBy(() -> aAssembler.build(request));
}
}
The AAssembler is annotated as @Validated({RequiringLanguageCode.class})
The reason why I launch an IllegalArgumentException instead of ConstraintViolationException is something out of scope of this problem. I catch the constraint violation an throw the IllegalArgumentException instead.
The Assembler build method has also a constraint annotated as :
public Response build(@Valid @NotNull A request) {
....
}
I would be very thankful if somebody can help me. Thanks anyway.