0

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.

Daniel Rch.
  • 150
  • 1
  • 7

1 Answers1

0

you can easily use @Rollback(false) annotation. see the same question on page here

ChenZhou
  • 102
  • 9