2017-08-10 13 views
2

ユニットテストが機能しない理由を調べようとしています。私は、Hibernateがアップデートの前に挿入を行うのを見ました。 それはなぜですか? テストが失敗する理由は何ですか?ユニットテストでDataJpaTestが更新されない

私はテスト環境用にhsqldbを設定しましたが、サービスはmysqlで正常に動作しているようです。

@Repository 
public interface UserDataRepository extends CrudRepository<UserData, Integer> { 

    @Transactional 
    @Modifying 
    @Query("UPDATE UserData ud SET chips = chips + :delta WHERE ud.id = :userId") 
    void addChipsToUser(@Param("userId") int userId, @Param("delta") long delta); 
} 

私のテストクラス:

@RunWith(SpringRunner.class) 
@DataJpaTest 
public class TestJPASlice { 

    @Autowired 
    private TestEntityManager entityManager; 

    @Autowired 
    private UserDataRepository repository; 

    @Test 
    public void testAddChipsToUser() { 
      UserData data = new UserData(); 
      data.setChips(100); 
      data.setId(13);     
      this.entityManager.persist(data);     

      System.err.println("pre u");     
      this.repository.addChipsToUser(13, 500);     
      System.err.println("post u"); 

      UserData two = this.repository.findOne(13); 
      assertThat(two.getId()).isEqualTo(13); 
      assertThat(two.getChips()).isEqualTo(600); 
    } 

これは、私が手に出力されます:

Hibernate: update user_player_data set chips=chips+? where user_id=? 
2017-08-10 11:33:37.794 WARN 2128 --- [main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: -1100, SQLState: 02000 
2017-08-10 11:33:37.795 WARN 2128 --- [main] o.h.engine.jdbc.spi.SqlExceptionHelper : no hay datos 
2017-08-10 11:33:37.796 INFO 2128 --- [main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [[email protected] 
2017-08-10 11:33:37.801 INFO 2128 --- [main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [[email protected] 
pre u 
Hibernate: insert into user_player_data (chips, user_id) values (?, ?) 
Hibernate: update user_player_data set chips=chips+? where user_id=? 
post u 
2017-08-10 11:33:37.874 INFO 2128 --- [main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [[email protected] 
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.073 sec <<< FAILURE! - in service.chipBank.TestJPASlice 
testAddChipsToUser(service.chipBank.TestJPASlice) Time elapsed: 0.074 sec <<< FAILURE! 
org.junit.ComparisonFailure: expected:<[6]00L> but was:<[1]00L> 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at service.chipBank.TestJPASlice.testAddChipsToUser(TestJPASlice.java:43) 

答えて

2

あなたが仕事にこれをしなければならないのは、clearAutomaticallyフラグを有効です。

@Modifying(clearAutomatically = true) 

を使用して、変更クエリを実行した後に基礎となる永続コンテキストをクリアします。フラグtake a look at the Spring Data JPA Documentationに関するいくつかの詳細については

2017-08-10 22:20:09.693 DEBUG 1948 --- [   main] org.hibernate.SQL      : insert into user_player_data (chips, user_id) values (?, ?) 
Hibernate: insert into user_player_data (chips, user_id) values (?, ?) 
2017-08-10 22:20:09.695 TRACE 1948 --- [   main] o.h.type.descriptor.sql.BasicBinder  : binding parameter [1] as [BIGINT] - [100] 
2017-08-10 22:20:09.696 TRACE 1948 --- [   main] o.h.type.descriptor.sql.BasicBinder  : binding parameter [2] as [INTEGER] - [13] 
2017-08-10 22:20:09.700 DEBUG 1948 --- [   main] org.hibernate.SQL      : update user_player_data set chips=chips+? where user_id=? 
Hibernate: update user_player_data set chips=chips+? where user_id=? 
2017-08-10 22:20:09.700 TRACE 1948 --- [   main] o.h.type.descriptor.sql.BasicBinder  : binding parameter [1] as [BIGINT] - [500] 
2017-08-10 22:20:09.701 TRACE 1948 --- [   main] o.h.type.descriptor.sql.BasicBinder  : binding parameter [2] as [INTEGER] - [13] 
post u 
2017-08-10 22:20:09.708 DEBUG 1948 --- [   main] org.hibernate.SQL      : select userdata0_.user_id as user_id1_0_0_, userdata0_.chips as chips2_0_0_ from user_player_data userdata0_ where userdata0_.user_id=? 
Hibernate: select userdata0_.user_id as user_id1_0_0_, userdata0_.chips as chips2_0_0_ from user_player_data userdata0_ where userdata0_.user_id=? 
2017-08-10 22:20:09.708 TRACE 1948 --- [   main] o.h.type.descriptor.sql.BasicBinder  : binding parameter [1] as [INTEGER] - [13] 
2017-08-10 22:20:09.712 TRACE 1948 --- [   main] o.h.type.descriptor.sql.BasicExtractor : extracted value ([chips2_0_0_] : [BIGINT]) - [600] 
2017-08-10 22:20:09.751 INFO 1948 --- [   main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [[email protected] testClass = TestJPASlice, testInstance = [email protected], testMethod = [email protected], testException = [null], mergedContextConfiguration = [[email protected] testClass = TestJPASlice, locations = '{}', classes = '{class com.example.demo.DemoApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[[[email protected] key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springfr[email protected]7a765367, org.springframework.boot.test.json.DuplicateJsonObje[email protected]3043fe0e, org.[email protected]0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationCon[email protected]c46bcd4, org.springframework.boo[email protected]351584c0, org.springframework.boot[email protected]ee27f40e, org.springframework.boot.test.autocon[email protected]cb51256], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]]. 
2017-08-10 22:20:09.753 INFO 1948 --- [  Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]4944252c: startup date [Thu Aug 10 22:20:07 BST 2017]; root of context hierarchy 
2017-08-10 22:20:09.754 INFO 1948 --- [  Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 
2017-08-10 22:20:09.754 INFO 1948 --- [  Thread-2] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export 
2017-08-10 22:20:09.755 DEBUG 1948 --- [  Thread-2] org.hibernate.SQL      : drop table user_player_data if exists 
Hibernate: drop table user_player_data if exists 

これで、ログに、後続の選択クエリを見ることができます。

+0

ありがとうございました!それはトリックでした –

+0

@dimitrisli多分あなたは他のオプションがあることを知っていますか? ClearAutomaticallyを追加すると私には合わないからです。 –

+0

@OrestKyrylchuk明らかにあなたの事件を推測するのは難しいです。あなたのユースケースをスタンドアロンの質問に定式化してください。 – dimitrisli

関連する問題