ãã¹ãæ¸ãã¦ã¾ããï¼ç§ã¯å¥½ãã§ãï¼å«ããªäººã¯å¤ãã¿ããã§ããã©...
Springã®ãã¹ãé¨åã®ãªãã¡ã¬ã³ã¹èªãã§å¿ããªããã¡ã«ã¾ã¨ãããç²¾ç¥ãåããã®ã§æ¸ãã¾ãã ä»åã¯ã@DataJpaTestã¨ããã¬ãã¸ããªã®ãã¹ããæ¸ãããã®ã¢ããã¼ã·ã§ã³ãç´¹ä»ãã¾ãã
@DataJpaTestã¢ããã¼ã·ã§ã³ã¯ãããã©ã«ãã§ã¤ã³ã¡ã¢ãªDBã®è¨å®ããããã@Entityãã¤ããã¯ã©ã¹ãBeanç»é²ãããã@Repositoryã¤ããã¯ã©ã¹ãBeanç»é²ãããªã©Spring Data JPAã¬ãã¸ããªã®è¨å®ãã¦ãããããã¾ãã @DataJpaTestã®ã½ã¼ã¹ã³ã¼ããã®ããã¦ã¿ãã¨ãããªæãã®è¨å®ãè¡ããã¦ããããã§ãã
... @AutoConfigureDataJpa @AutoConfigureTestDatabase @AutoConfigureTestEntityManager ...
Data JPAãã¹ãã¯ããã©ã³ã¶ã¯ã·ã§ãã«ã§åãã¹ãã®çµããã«ãã¼ã«ããã¯ãã¾ãã ãããããããããªãå ´åã¯ã次ã®ããã«æ¸ãã¦ãã ããã
@Transactional(propagation = Propagation.NOT_SUPPORTED)
ã¾ããdata JPAãã¹ãã§ã¯ãæ¨æºçãªJPA EntityManagerã®ä»£ããã«TestEntityManagerã®Beanãã¤ã³ã¸ã§ã¯ãããã¾ãã
ã½ã¼ã¹ã³ã¼ã
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.2.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>1.2.0</version> <scope>test</scope> </dependency> </dependencies>
- Java8
- SpringBoot 2.0.3.RELEASE
- H2
- JUnit5
Brand.java
@Entity public class Brand implements Serializable { enum Gender { MAN, WOMAN, UNISEX } @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(nullable = false) private String name; @Enumerated(EnumType.STRING) private Gender gender; public Brand(String name, Gender gender) { this.name = name; this.gender = gender; } ... getter, setter }
idããã©ã³ãåããã©ã³ãã®å¯¾è±¡æ§å¥ããã¤Brandã¨ã³ãã£ãã£ã§è©±ãé²ãã¦ããã¾ãã
BrandRepository.javaï¼ä»åã®ãã¹ã対象ï¼
@Repository public interface BrandRepository extends JpaRepository<Brand, Integer> { List<Brand> findByGender(Gender gender); Integer countByGender(Gender gender); }
æ§å¥ã§ãã©ã³ããæ¤ç´¢ãã findByGenderã¡ã½ããã¨æ§å¥ãã¨ã®ãã©ã³ãæ°ãæ¤ç´¢ãã countByGenderã¡ã½ãããæºåãã¾ããã
BrandRepositoryTest.java
// SpringExtention.classã¯ãJunit 5ä¸ã§Spring TestContext Frameworkã使ããããã«ãã¦ãã @ExtendWith(SpringExtension.class) class BrandRepositoryTest { @Nested @DataJpaTest class FindByGender { @Autowired private TestEntityManager entityManager; @Autowired private BrandRepository brandRepository; @BeforeEach void beforeEach() { entityManager.persist(new Brand("STOF", Gender.UNISEX)); entityManager.persist(new Brand("ETHOSENS", Gender.MAN)); entityManager.persist(new Brand("dulcamara", Gender.UNISEX)); } @Test void man() { List<Brand> brands = brandRepository.findByGender(Gender.MAN); org.assertj.core.api.Assertions.assertThat(brands) .extracting(Brand::getName, Brand::getGender) .containsExactly(Tuple.tuple("ETHOSENS", Gender.MAN)); } @Test void woman() { List<Brand> brands = brandRepository.findByGender(Gender.WOMAN); org.assertj.core.api.Assertions.assertThat(brands) .hasSize(0); } } @Nested @DataJpaTest class CountByGender { @Autowired private TestEntityManager entityManager; @Autowired private BrandRepository brandRepository; @BeforeEach void beforeEach() { entityManager.persist(new Brand("STOF", Gender.UNISEX)); entityManager.persist(new Brand("ETHOSENS", Gender.MAN)); entityManager.persist(new Brand("dulcamara", Gender.UNISEX)); } @Test void man() { int count = brandRepository.countByGender(Gender.MAN); Assertions.assertEquals(1, count); } @Test void woman() { int count = brandRepository.countByGender(Gender.WOMAN); Assertions.assertEquals(0, count); } } }
æ¤è¨¼ã«ã¯ä¸é¨AssertJã使ã£ã¦ã¾ããAssertJã«ã¤ãã¦ã¯ããã¡ãã§ç°¡åã«è§¦ãã¦ã¾ãã
BrandRepositoryTest.javaå®è¡ãã
å®è¡æã®ãã°ãæç²ãã¦ç´¹ä»ãã¾ãã
... 2018-07-05 00:20:30.440 INFO 5456 --- [ main] o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:393e83d1-908e-4fb0-91d2-8becd045235b;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa' ...
H2ãã¼ã¿ãã¼ã¹ã使ã£ã¦ããã¨ããããã¾ãã
... Hibernate: drop table brand if exists Hibernate: drop sequence if exists hibernate_sequence Hibernate: create sequence hibernate_sequence start with 1 increment by 1 Hibernate: create table brand (id integer not null, gender varchar(255), name varchar(255) not null, primary key (id)) ...
brandãã¼ãã«ãä½ã£ã¦ã¾ãã
... Hibernate: insert into brand (gender, name, id) values (?, ?, ?) Hibernate: insert into brand (gender, name, id) values (?, ?, ?) Hibernate: insert into brand (gender, name, id) values (?, ?, ?) ...
beforeEachã®å¦çãè¡ããã¦ããæ§åã§ãã
... Hibernate: select count(brand0_.id) as col_0_0_ from brand brand0_ where brand0_.gender=? ... Hibernate: select brand0_.id as id1_0_, brand0_.gender as gender2_0_, brand0_.name as name3_0_ from brand brand0_ where brand0_.gender=? ...
countByGenderã¨findByGenderãå®è¡ããã¦ãããã¨ããããã¾ãã
... Hibernate: drop table brand if exists Hibernate: drop sequence if exists hibernate_sequenceã§ãã¼ãã«ã¨hibernate_sequenceãåé¤ããã¦ãããã¨ããããã¾ãã
ãã¹ããçµããã¨ãã¼ãã«ã¨hibernate_sequenceãåé¤ãã¦ãã¾ãã ç°¡åã«ã¬ãã¸ããªã®ãã¹ããã§ãããã¨ãããã£ã¦ããã ãããã¨æãã¾ãã
hibernate_sequenceã¯æå¾ã«ããæ¶ãããªãã®ã§ããã£ã¨é£çªãæ¯ãããç¶ããã®ã§ãèªåæ¡çªã®IDã®å¤ã®æ¤è¨¼ããå ´åã¯ãæ°ãã¤ãã¦ãã ããããã¹ããã¨ã«åæãã¼ã¿ãinsertããã®ç¾ãããªãã®ã§ãã¾ãæ¹æ³è¦ã¤ãããã§ãã
åè
43.3.11 Auto-configured Data JPA Tests
追è¨
2018/7/12 create tableãdrop tableãFlywayã使ã£ã¦è¡ãªã£ã¦ãããããapplication.propertiesã«ä»¥ä¸ã®ããã«æ¸ãã¦ããã¨ãã¼ãã«ãä½æãããã«ãxxxãã¼ãã«ãããã¾ãããã¿ããã«æããã¾ãã®ã§ãFlywayã¯ä½¿ããããã«ãã¦ãã ããã
spring.flyway.enabled=false