Context
I am testing a method with @Transactional anotation.
Tested Method inside Business class
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRES_NEW, rollbackFor = UniquekeyException.class)
public void saveContact(ContactDto dto) throws BadRequestException, UniquekeyException {
Customer customer =this.authenticationFacade.getCustomerLoggedIn();
customer.setFromDto(dto);
this.customerRepository.save(customer);
}
Unit Test
@Autowired
private CustomerRepository customerRepository;
@Autowired
private BisinessCustomer bisinessCustomer;
@Autowired
private CustomerRepository customerRepository;
@Test
@WithMockUser(username=USERNAME, password = PASSWORD)
public void saveContactDtoTest(){
//setup
ContactDto contact = this.getContactDto();
//exec
this.bisinessCustomer.saveContactDto(contact);
//assert
Assert.assertEquals(this.customerRepository.findByUsername(USERNAME).getContact.getAdress(), contact.getAdress());
//First value return null!!! When remove transactional anotation return the same that contact.getAdress()
}
Problem
When I call it from the @Test method, it doesn't persist anything.
When I remove de @Transactional anotation from the target method all works fine and persist ok from the test.
The database is in memory when I run the tests and I use migrations flyway, mysql, spring, hirbernate, springboot.
Question
Can I configure @Transactional anotation for exclude it when I run the tests?
Thanks.