DataJpaTest fail to update in unit test

Viewed 3459

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)
1 Answers
Related