|
| 1 | +package example; |
| 2 | + |
| 3 | +import io.micronaut.context.annotation.Property; |
| 4 | +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; |
| 5 | +import jakarta.inject.Inject; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 14 | + |
| 15 | +@MicronautTest |
| 16 | +@Property(name = "spec.name", value = "BookRepositoryTest") |
| 17 | +@Property(name = "datasources.default.name", value = "mydb") |
| 18 | +@Property(name = "datasources.default.transaction-manager", value = "springJdbc") |
| 19 | +@Property(name = "jpa.default.properties.hibernate.hbm2ddl.auto", value = "create-drop") |
| 20 | +class BookRepositoryTest { |
| 21 | + |
| 22 | + @Inject |
| 23 | + AbstractBookRepository bookRepository; |
| 24 | + |
| 25 | + @AfterEach |
| 26 | + void cleanup() { |
| 27 | + bookRepository.deleteAll(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void testBooksJdbcTemplate() { |
| 32 | + bookRepository.saveAll(Arrays.asList( |
| 33 | + new Book(null,"The Stand", 1000), |
| 34 | + new Book(null,"The Shining", 600), |
| 35 | + new Book(null,"The Power of the Dog", 500), |
| 36 | + new Book(null,"The Border", 700), |
| 37 | + new Book(null,"Along Came a Spider", 300), |
| 38 | + new Book(null,"Pet Cemetery", 400), |
| 39 | + new Book(null,"A Game of Thrones", 900), |
| 40 | + new Book(null,"A Clash of Kings", 1100) |
| 41 | + )); |
| 42 | + |
| 43 | + List<Book> result = bookRepository.findByTitle("The Shining"); |
| 44 | + assertEquals(1, result.size()); |
| 45 | + |
| 46 | + assertNotNull(result.get(0).id()); |
| 47 | + assertEquals("The Shining", result.get(0).title()); |
| 48 | + assertEquals(600, result.get(0).pages()); |
| 49 | + } |
| 50 | +} |
0 commit comments