0

I have configured the test application properties to use Postgresql. All tests seem to work but that is not totally correct.

When I do actual persistent data tests, there is no data being saved to the postgresql db even though liquibase is run.

Relevant section of application.yml that I've configured with postgresql.

spring:
    application:
        name: stackoverflow
    jackson:
        serialization.write_dates_as_timestamps: false
    cache:
        type: none
    datasource:
            type: com.zaxxer.hikari.HikariDataSource
            url: jdbc:postgresql://127.0.0.1:5432/store_test?user=postgres
    jpa:
        database-platform: io.github.jhipster.domain.util.FixedPostgreSQL82Dialect
        database: POSTGRESQL
        show-sql: false
        generate-ddl: false
        hibernate:
          ddl-auto: none
        properties:
            hibernate.id.new_generator_mappings: true
            hibernate.cache.use_second_level_cache: false
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: true

As per normal there is a createEntity method for say an Entity Company

I have tried

em.persist(company)
em.flush(company)

also

companyRepository.saveAndFlush(company)

What am I missing to get JHipster to correct test against a real database or if I am saving data incorrectly.

Abhishek Dujari
  • 2,343
  • 33
  • 43
  • Show the code that you use to write data and to verify it is or isn't in the database. – Jens Schauder Jan 17 '18 at 05:46
  • 3
    By default, tests generated by JHipster are `@Transactional`annotated so data modification are rollbacked. – Gaël Marziou Jan 17 '18 at 10:45
  • That's what I understand but when I do a breakpoint at the middle of a test I see the entire table is missing and then it is recreated after the particular unit test is done. When I do a native query it returns no data, but Jpa queries seem to work. – Abhishek Dujari Jan 17 '18 at 14:10
  • Does your native query run in same transaction? I doubt it so it can't read your data even when app is stopped at a breakpoint. That's how I understand isolation. – Gaël Marziou Jan 17 '18 at 14:12
  • I didn't understand that statement and how it is applied. Can we talk on gitter? – Abhishek Dujari Jan 17 '18 at 14:25
  • I ended using a `.sql` file and using a SQl execution in test `@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,scripts = "classpath:importdata.sql")` This is how I was able to test `nativeQuery` and get the objects in database. All other tests do not create the objects in DB (perhaps in-memory?) but they seem to pass. – Abhishek Dujari Jan 24 '18 at 22:39
  • I've changed test properties file to use postgres also like you, but still give me errors in liquibase with h2 DB, Did change anything else? @AbhishekDujari – Ahmed E. Eldeeb May 07 '19 at 19:23
  • It's been awhile but this method is applicable only to postgresql and I am running actual sql native queries. You can't use H2 with it unless you change the queries to h2 queries. I dont have access to that code since I left that project over an year ago. However it worked fine in running tests. – Abhishek Dujari May 11 '19 at 12:57

1 Answers1

0

as said @gaël-marziou By default, tests generated by JHipster are @Transactional annotated so data modification are rollbacked. You should @Commit annotation for tetMethod to prevent it from rollback

Nurlan
  • 720
  • 7
  • 12
  • yes, on this he is correct. But even without transactions and calling commit, no actual data is written to DB. Only hibernate will acknowledge it. I am fairly new to Java/Spring and coming from Python/Rails I expected data to be actually written to DB so I can run native queries. That was not the case. Are you saying that my understanding is wrong? – Abhishek Dujari Mar 29 '18 at 13:56
  • Yes, whitout @Transactional it should save data, may be there some cases that makes your test transactional(super class, some xml config I am not sure)?! I did the same, tried `flush,update, save`! My class annotated @Transactional and method annotated @Commit, after this test method leaves data saved in db. – Nurlan Mar 30 '18 at 05:48