dimanche 17 janvier 2021

Spring Boot Rollback for mongoRepository testing

I wrote a Test for a MongoRepository in Spring Boot, and the test works fine. The only problem is that when the test is over, I want a rollback, so that there will be no change in the database caused by the test.

@RunWith(SpringRunner.class)
@DataMongoTest
@Transactional
public class AccountTypeRepositoryTest {

   @Autowired
   AccountTypeRepository accountTypeRepository;

 @Test
 @Rollback(true)
 public void test_save(@Autowired MongoTemplate mongoTemplate){
    AccountType accountType=new AccountType();
    accountType.setName("test");
    AccountType expected=accountTypeRepository.save(accountType);
    assertNotNull(expected);
    assertNotNull(expected.getId());
 }

}

1 commentaire: