I don't think anyone would do this, but I've suffered so much so I'll make a note of it.
: grin: 1.4: How to do it in case of 1.4. : smirk: 1.2: 1.2 is how to do it.
@EntityScan
:grin: 1.4: org.springframework.boot.autoconfigure.domain.EntityScan :smirk: 1.2: org.springframework.boot.orm.jpa.EntityScan
DI
: grin: 1.4: If it is self-evident, DI will be done without adding @Autowired
.
: smirk: 1.2: Let's add @Autowired
.
:grin: 1.4: @RunWith(SpringRunner.class)
:smirk: 1.2: @RunWith(SpringJUnit4ClassRunner.class)
: grin: 1.4: Add @SpringBootTest
: smirk: 1.2: Add @SpringApplicationConfiguration (classes = application classes)
http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/SpringApplicationConfiguration.html
: grin: 1.4: Add @DataJpaTest
to the test class.
: smirk: 1.2: It's hard.
@DataJpaTest
seems to do this.
@EntityScan
I thought I had to do everything, but it seems that it's okay if I do all this.
@EntityScan
and @EnableJpaRepositories
.TestEntityManager
class (starting from 1.4)TestEntityManager
(starting from 1.4)@Transactional
and @SpringApplicationConfiguration
to the test class.I will do it in order.
@EntityScan
, @EnableJpaRepositories
TestApplication.java
@SpringBootApplication
@EnableJpaRepositories(Location of repository class)
@EntityScan(Entity class location)
@EnableJpaAuditing //This is irrelevant this time
public class TestApplication {
}
TestEntityManager
class (starting from 1.4)The original source is here. https://github.com/spring-projects/spring-boot/blob/master/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/
TestEntityManager.java
public class TestEntityManager {
private final EntityManagerFactory entityManagerFactory;
public TestEntityManager(EntityManagerFactory entityManagerFactory) {
Assert.notNull(entityManagerFactory, "EntityManagerFactory must not be null");
this.entityManagerFactory = entityManagerFactory;
}
public <E> E persist(E entity) {
getEntityManager().persist(entity);
return entity;
}
public void flush() {
getEntityManager().flush();
}
public <E> E persistAndFlush(E entity) {
persist(entity);
flush();
return entity;
}
public void detach(Object entity) {
getEntityManager().detach(entity);
}
public <E> E find(Class<E> entityClass, Object primaryKey) {
return getEntityManager().find(entityClass, primaryKey);
}
public final EntityManager getEntityManager() {
EntityManager manager = EntityManagerFactoryUtils
.getTransactionalEntityManager(this.entityManagerFactory);
Assert.state(manager != null, "No transactional EntityManager found");
return manager;
}
}
TestEntityManager
(starting from 1.4)TestEntityManagerConfigure.java
@Configuration
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
public class TestEntityManagerConfigure {
@Bean
@ConditionalOnMissingBean
public TestEntityManager testEntityManager(
EntityManagerFactory entityManagerFactory) {
return new TestEntityManager(entityManagerFactory);
}
}
@Transactional
and @SpringApplicationConfiguration
to the test classMySomeRepositoryTest.java
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class)
@Transactional
public class MySomeRepositoryTest {
@Autowired
private MySomeRepository instance;
@Autowired
private TestEntityManager em;
@Test
public void testSomeMethod {
// ...
}
}
: grin: 1.4: Add @MockBean
to the instance variable you want to mock
: smirk: 1.2: Define a @Bean
that returns a mocked instance and add @Primary
MySomeTest.java
@MockBean
private MyTargetClass mock;
@Profile is added so that only a specific @Configuration class is valid.
MySomeTest.java
@ActiveProfiles("MySomeTest")
public class MySomeTest {
@Autowired
private MyTargetClass mock;
}
MySomeTestConfig.java
@Configuration
@Profile("MySomeTest")
public class MySomeTestConfig {
@Bean
@Primary
public MyTargetClass myTargetClass() {
return Mockito.mock(MyTargetClass.class);
}
}
If you find something, I will add it.
Recommended Posts