|
20 | 20 | import org.springframework.context.ApplicationContextInitializer; |
21 | 21 | import org.springframework.context.ConfigurableApplicationContext; |
22 | 22 | import org.springframework.data.mongodb.core.MongoTemplate; |
| 23 | +import org.springframework.data.mongodb.core.index.Indexed; |
23 | 24 | import org.springframework.test.context.ContextConfiguration; |
24 | 25 | import org.springframework.test.context.junit4.SpringRunner; |
25 | 26 |
|
|
33 | 34 | import static org.junit.Assert.assertTrue; |
34 | 35 |
|
35 | 36 | @Slf4j |
36 | | -@DataMongoTest |
| 37 | +@DataMongoTest(properties = {"spring.main.banner-mode=off", "data.mongodb.auto-index-creation=true"}) |
37 | 38 | @ContextConfiguration(initializers = MongoDBContainerDatabaseNameTest.Initializer.class) |
38 | 39 | @RunWith(SpringRunner.class) |
39 | 40 | public class MongoDBContainerDatabaseNameTest { |
@@ -65,8 +66,8 @@ public void shouldTestDatabaseName() { |
65 | 66 | isDatabaseInMongoDB(mongoSyncClient, DATABASE_NAME) |
66 | 67 | ); |
67 | 68 |
|
68 | | - //3. Perform an operation to create a new collection via mongoTemplate. |
69 | | - mongoTemplate.createCollection(Product.class); |
| 69 | + //3. Perform an operation to save a new Product via mongoTemplate. |
| 70 | + mongoTemplate.save(new Product(1L)); |
70 | 71 |
|
71 | 72 | //4. Now the database is created in MongoDB. |
72 | 73 | assertTrue( |
@@ -110,6 +111,7 @@ public static void main(String[] args) { |
110 | 111 | @NoArgsConstructor |
111 | 112 | @Setter(AccessLevel.NONE) |
112 | 113 | private static class Product { |
| 114 | + @Indexed(unique = true) |
113 | 115 | private Long article; |
114 | 116 | } |
115 | 117 | } |
0 commit comments