Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataJpaTest fail to update in unit test

I'm trying to find out why my unit test does not work. I saw that Hibernate does an insert before the update. Why does it do that? Is the reason why the test fails?

I have set hsqldb for the test environment, the service seems to work fine in 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);
}

My test class:

@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);
    }

This is the output I get:

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 [DefaultTestContext@29626d54... 
2017-08-10 11:33:37.801  INFO 2128 --- [main] o.s.t.c.transaction.TransactionContext   : Began transaction (1) for test context [DefaultTestContext@29626d54... 
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 [DefaultTestContext@29626d54... 
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)
like image 680
Axx Silvera Avatar asked Aug 10 '17 14:08

Axx Silvera


1 Answers

All you have to do for this to work is enable the clearAutomatically flag:

@Modifying(clearAutomatically = true)

in order to clear the underlying persistence context after executing the modifying query.

You can then see in the logs the subsequent select query:

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 [DefaultTestContext@ca263c2 testClass = TestJPASlice, testInstance = com.example.demo.TestJPASlice@2145433b, testMethod = testAddChipsToUser@TestJPASlice, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@589b3632 testClass = TestJPASlice, locations = '{}', classes = '{class com.example.demo.DemoApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@45f45fa1 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.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@7a765367, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3043fe0e, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@c46bcd4, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@ee27f40e, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@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.springframework.context.annotation.AnnotationConfigApplicationContext@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

For some more information about the flag take a look at the Spring Data JPA Documentation.

like image 79
dimitrisli Avatar answered Oct 18 '22 17:10

dimitrisli