|
| 1 | +package com.baeldung.transaction; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.FixMethodOrder; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.junit.runners.MethodSorters; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.data.mongodb.MongoTransactionException; |
| 15 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 16 | +import org.springframework.data.mongodb.core.query.Criteria; |
| 17 | +import org.springframework.data.mongodb.core.query.Query; |
| 18 | +import org.springframework.test.context.ContextConfiguration; |
| 19 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 20 | +import org.springframework.transaction.annotation.Transactional; |
| 21 | + |
| 22 | +import com.baeldung.config.MongoConfig; |
| 23 | +import com.baeldung.model.User; |
| 24 | +import com.baeldung.repository.UserRepository; |
| 25 | +import com.mongodb.MongoCommandException; |
| 26 | + |
| 27 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 28 | +@ContextConfiguration(classes = MongoConfig.class) |
| 29 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 30 | +public class MongoTransactionalIntegrationTest { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private MongoTemplate mongoTemplate; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private UserRepository userRepository; |
| 37 | + |
| 38 | + @Test |
| 39 | + @Transactional |
| 40 | + public void whenPerformMongoTransaction_thenSuccess() { |
| 41 | + userRepository.save(new User("John", 30)); |
| 42 | + userRepository.save(new User("Ringo", 35)); |
| 43 | + Query query = new Query().addCriteria(Criteria.where("name") |
| 44 | + .is("John")); |
| 45 | + List<User> users = mongoTemplate.find(query, User.class); |
| 46 | + |
| 47 | + assertThat(users.size(), is(1)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test(expected = MongoTransactionException.class) |
| 51 | + @Transactional |
| 52 | + public void whenListCollectionDuringMongoTransaction_thenException() { |
| 53 | + if (mongoTemplate.collectionExists(User.class)) { |
| 54 | + mongoTemplate.save(new User("John", 30)); |
| 55 | + mongoTemplate.save(new User("Ringo", 35)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test(expected = MongoCommandException.class) |
| 60 | + @Transactional |
| 61 | + public void whenCountDuringMongoTransaction_thenException() { |
| 62 | + userRepository.save(new User("John", 30)); |
| 63 | + userRepository.save(new User("Ringo", 35)); |
| 64 | + userRepository.count(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @Transactional |
| 69 | + public void whenQueryDuringMongoTransaction_thenSuccess() { |
| 70 | + userRepository.save(new User("Jane", 20)); |
| 71 | + userRepository.save(new User("Nick", 33)); |
| 72 | + List<User> users = mongoTemplate.find(new Query(), User.class); |
| 73 | + |
| 74 | + assertTrue(users.size() > 1); |
| 75 | + } |
| 76 | + |
| 77 | + // ==== Using test instead of before and after due to @transactional doesn't allow list collection |
| 78 | + |
| 79 | + @Test |
| 80 | + public void setup() { |
| 81 | + if (!mongoTemplate.collectionExists(User.class)) { |
| 82 | + mongoTemplate.createCollection(User.class); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void ztearDown() { |
| 88 | + mongoTemplate.dropCollection(User.class); |
| 89 | + } |
| 90 | +} |
0 commit comments