I'm writing unit tests to check if the input validation works in my Spring Repository, but it doesn't look like it does.
Keeping it simple, I have a Repository class:
@Repository
public class CustomerRepository {
// Java client for Redis, that I extend as JedisConnector in order to make it a Bean
private Jedis jedis;
@Autowired
public CustomerRepository(JedisConnector jedis) {
this.jedis = jedis;
}
private <S extends Customer> S save(@Valid S customer) throws CustomerException {
try {
this.jedis.set(...); // writing to Redis (mocked in test)
return customer;
} catch (JsonProcessingException e) {
throw new CustomerException(e.toString());
}
}
}
This uses the following model class:
@AllArgsConstructor
@NoArgsConstructor
@Data // from Lombok
public class Customer {
@Email(message = "Email must be valid.")
private String identifier;
@NotBlank(message = "Password cannot be null or empty string.")
private String password;
@URL(message = "URL must be a url.")
private String url;
}
So I wrote a unit test like this, expecting it to throw some exception that I could assert:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {JedisConnector.class})
public class CustomerRepositoryTest {
// Cannot autowire because dependent bean needs to be configured
private CustomerRepository customerRepository;
@MockBean
JedisConnector jedisConnector;
@Before
public void setUp() {
// Configure Mock JedisConnector
MockitoAnnotations.initMocks(this);
Mockito.when(jedisConnector.select(anyInt())).thenReturn("OK");
// Manually wire dependency
customerRepository = new CustomerRepository(jedisConnector);
}
@Test
public void saveShouldFailOnInvalidInput() throws CustomerException {
Mockito.when(jedisConnector.set(anyString(), anyString())).thenReturn("OK");
// Blatantly invalid input
Customer customer = new Customer("testemail", "", "testurl");
customerRepository.save(customer);
}
}
But it just runs through, only outputting debug messages (that I left out in this question). How do I enforce the validation? If possible, I'd like to avoid calling a validator explicitely in each method of the repository.
I've seen many examples online that I've tried to reproduce (From Baeldung to DZone, with of course a lot of questions on this very website, including this interesting one), still unsuccessful. What am I missing?