2016-08-30 31 views
1

我有一個單元測試,它包含一些數據庫的簡單操作。使用Spring Data和JPA實現與數據庫的交互。我的環境是Spring Boot 1.4在裝有數據庫的單元/集成測試中僅加載Spring Boot Data/JPA層

我的測試看起來像:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class) 
@SpringBootTest(classes = {Application.class}) 
@Transactional 
public class OrderRepositoryTest { 
    @Autowired 
    private OrderRepository orderRepository; 

    @Autowired 
    EntityManager entityManager; 

    @Test 
    public void can_create_and_find_one_order() { 
     String orderId = "XX_YY"; 

     Order order1 = createAnOrder(); 
     orderRepository.save(order1); 

     entityManager.flush(); 
     entityManager.clear(); 

     Order order2 = orderRepository.findOne(orderId); 
     Assertions.assertThat(order2).isEqualTo(order1); 
    } 
} 

應用是Spring引導入口點,並且包含EnableAutoConfiguration註釋:

@EnableAutoConfiguration 
@EnableConfigurationProperties 
@ComponentScan(basePackageClasses = { 
    WebMvcConfig.class, 
    ServicesConfiguration.class, 
    WebSecurityConfiguration.class 
}) 
public class Application extends SpringBootServletInitializer { 
    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) { 
     return applicationBuilder.sources(Application .class); 
    } 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

的測試並加載JPA層,並執行操作。但它也會加載MVC配置,安全配置以及大量其他未測試的圖層。

問題:我該如何替換EnableAutoConfiguration並只加載我需要的JPA圖層?

理想的情況下,它會類似的方式,我可以用它從單元測試和應用程序的入口點延伸'WebMvcConfigurerAdapter',配置類,所以我的單元測試也驗證配置:

@Configuration 
@EnableJpaRepositories(basePackages = "class.path.to.my.repositories") 
public class PersistenceConfiguration { 
    // Whatever needs to be here 
    // ... 
} 

回答

2

如果你只是想測試JPA層,只需要@DataJpaTest,它將配置一個內存嵌入式數據庫,掃描@Entity類並配置Spring Data JPA存儲庫。常規的@Component bean不會被加載到ApplicationContext中。

例如:

@RunWith(SpringRunner.class) 
@DataJpaTest 
@Transactional 
public class SpringBootJPATest { 

    @Autowired 
    private BlogEntryRepository blogEntryRepository; 

    @Autowired 
    private TestEntityManager entityManager; 

    @Test 
    public void jpa_test() { 
     BlogEntry entity = new BlogEntry(); 
     entity.setTitle("Test Spring Boot JPA Test"); 
     BlogEntry persist = entityManager.persist(entity); 
     System.out.println(persist.getId()); 
    } 

    @Test 
    public void jap_test_repo() { 
     BlogEntry entity = new BlogEntry(); 
     entity.setTitle("Test Spring Boot JPA Test"); 
     BlogEntry persist = blogEntryRepository.save(entity); 
     System.out.println(persist.getId()); 
    } 
} 

BlogEntryRepository只是擴展了JpaRepository

輸出一個空接口(I刪除其他信息的日誌只留下所有的Hibernate日誌):

Hibernate: drop table blog_entry if exists 
Hibernate: drop table comment if exists 
Hibernate: create table blog_entry (id bigint generated by default as identity, content varchar(255), created_on timestamp, title varchar(255), updated_on timestamp, primary key (id)) 
Hibernate: create table comment (id bigint generated by default as identity, comment varchar(255), blog_entry_id bigint, primary key (id)) 
Hibernate: alter table comment add constraint FK5f23qjwyt0ffjboqu3go58buu foreign key (blog_entry_id) references blog_entry 

...... 

Hibernate: insert into blog_entry (id, content, created_on, title, updated_on) values (null, ?, ?, ?, ?) 
1 

...... 

Hibernate: drop table blog_entry if exists 
Hibernate: drop table comment if exists 
+0

由於原始海報顯然需要事務性測試,因此您應該刪除'(propagation = Propagation.NOT_SUPPORTED)'(並重命名測試類)。否則,你的答案看起來不錯。 –

+0

@SamBrannen是的,謝謝你的提醒。 –

+0

如果我只是沒有任何修改就使用這個代碼,我得到錯誤'無法找到一個@SpringBootConfiguration,你需要使用@ContextConfiguration或@SpringBootTest(classes = ...)與你的測試' – jmgonet

2

由於spring-boot document提到,使用@DataJpaTest是收集方式。 嘗試下面的代碼,它適用於我。

@RunWith(SpringRunner.class) 
@Import(PersistenceConfiguration.class) 
@ContextConfiguration(classes = YourOrderRepositoryImpl.class) 
@DataJpaTest 
@Transactional 
public class OrderRepositoryTest { 
    @Autowired 
    private OrderRepository orderRepository; 

    @Autowired 
    TestEntityManager entityManager; 

    @Test 
    public void foo() { 
    ... 
    } 
} 

使用@DataJpaTest還需要使用@ContextConfiguration在租賃Config中的一個彈簧管理豆,否則你會得到錯誤Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test。您可以使用TestEntityManager代替EntityManager。它的文檔也是there

相關問題