Home > Mobile >  Why aren't rows being inserted into the H2 Db from my script even though the drop table and cre
Why aren't rows being inserted into the H2 Db from my script even though the drop table and cre

Time:01-15

Why aren't rows being inserted into the H2 Db from my script even though the drop table and create table is happening?

Hibernate: drop table if exists encouragement CASCADE 
Hibernate: create table encouragement (id bigint not null, category varchar(255), message varchar(255), tone varchar(255), topic varchar(255), primary key (id))
2023-01-13 18:39:08.297  INFO 18108 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]

No Inserts after this!

Details:

I am testing my SpringBoot application which uses the H2 db. It is running in IntelliJ IDE.

When I launch the SpringBoot application in the IDE, I can query the console http://localhost:8080/h2-console in the browser. When I stop the IDE, I am unable to. The DB url in application.properties is spring.datasource.url=jdbc:h2:~/test.

My other JUnit tests are working (mocked repository).

When I test the repository, I run the SpringBoot application so that the DB is present, but I get a zero length list back.

@ExtendWith(SpringExtension.class)
@DataJpaTest
class EncouragementRepositoryTest {

    @Autowired
    EncouragementRepository repository;

    @BeforeEach
    void setUp() {
    }

    @Test
    public void find_all() {
        List<Encouragement> list = repository.findAll();
        assertEquals(33,list.size());
    }

}

I don't know why it is failing and returning zero length list.

So I thought perhaps the DB isn't seeded.

I changed the db url to

spring.datasource.url=jdbc:h2:~/test;INIT=RUNSCRIPT FROM '~/Documents/Projects/regular-encourager/encourage/src/test/resources/data.sql'

In src/test/resources/data.sql I included a script like this:

drop table ENCOURAGEMENT if exists;
drop TYPE Category if exists;
drop TYPE Tone if exists;
drop TYPE Topic if exists;
CREATE TYPE Category AS ENUM('WhoIAmInChrist','Default');
CREATE TYPE Tone AS ENUM('Default', 'Uplifting', 'Urging', 'Warning', 'Soothing', 'Comforting', 'Inspiring', 'Centering', 'Balanced');
CREATE TYPE Topic AS ENUM('Default', 'AcceptedInChrist', 'SignificantInChrist', 'SecureInChrist', 'NoAnxietyInChrist');
CREATE TABLE Encouragement(ID INT PRIMARY KEY, CATEGORY Category, TOPIC Topic, TONE Tone, MESSAGE VARCHAR(512));
INSERT INTO Encouragement VALUES(-1, 'Default', 'Default', 'Default', 'We walk by Faith, not by Sight');
INSERT INTO Encouragement VALUES(0,'WhoIAmInChrist','AcceptedInChrist','Uplifting','John 1:12 I am God''s child.');
INSERT INTO Encouragement VALUES(1,'WhoIAmInChrist','AcceptedInChrist','Uplifting','John 15:15 As a disciple, I am a friend of Jesus Christ.');
    
.....

Here is the IDE console:

loads jars here and then...

com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.dropaflame.encourage.data.EncouragementRepositoryTest
18:39:05.330 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
18:39:05.341 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
18:39:05.388 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.dropaflame.encourage.data.EncouragementRepositoryTest] from class [org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper]
18:39:05.409 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.dropaflame.encourage.data.EncouragementRepositoryTest]: class path resource [com/dropaflame/encourage/data/EncouragementRepositoryTest-context.xml] does not exist
18:39:05.410 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.dropaflame.encourage.data.EncouragementRepositoryTest]: class path resource [com/dropaflame/encourage/data/EncouragementRepositoryTestContext.groovy] does not exist
18:39:05.410 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.dropaflame.encourage.data.EncouragementRepositoryTest]: no resource found for suffixes {-context.xml, Context.groovy}.
18:39:05.411 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.dropaflame.encourage.data.EncouragementRepositoryTest]: EncouragementRepositoryTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
18:39:05.487 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.dropaflame.encourage.data.EncouragementRepositoryTest]
18:39:05.586 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [C:\Users\Me\Documents\Projects\regular-encourager\encourage\target\classes\com\dropaflame\encourage\EncourageApplication.class]
18:39:05.599 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.dropaflame.encourage.EncourageApplication for test class com.dropaflame.encourage.data.EncouragementRepositoryTest
18:39:05.605 [main] DEBUG org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - @TestExecutionListeners is not present for class [com.dropaflame.encourage.data.EncouragementRepositoryTest]: using defaults.
18:39:05.606 [main] INFO org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
18:39:05.631 [main] INFO org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@64e7619d, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@495ee280, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@4fa1c212, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@6ea2bc93, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@3116c353, org.springframework.test.context.support.DirtiesContextTestExecutionListener@f627d13, org.springframework.test.context.transaction.TransactionalTestExecutionListener@4e928fbf, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@352ff4da, org.springframework.test.context.event.EventPublishingTestExecutionListener@3224a577, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@2e32ccc5, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@748741cb, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@3e44f2a5, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@295cf707, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@1130520d, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@5f77d0f9]
18:39:05.637 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@20d7d6fb testClass = EncouragementRepositoryTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@4ef782af testClass = EncouragementRepositoryTest, locations = '{}', classes = '{class com.dropaflame.encourage.EncourageApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@11d8ae8b 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.sql.init.SqlInitializationAutoConfiguration, 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@130c12b7, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@5d534f5d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@6340e5f0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6c1a5b54, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@9e45b0fa, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@543295b0, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null].
18:39:05.660 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[DefaultTestContext@20d7d6fb testClass = EncouragementRepositoryTest, testInstance = com.dropaflame.encourage.data.EncouragementRepositoryTest@59d4cd39, testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@4ef782af testClass = EncouragementRepositoryTest, locations = '{}', classes = '{class com.dropaflame.encourage.EncourageApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@11d8ae8b 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.sql.init.SqlInitializationAutoConfiguration, 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@130c12b7, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@5d534f5d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@6340e5f0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6c1a5b54, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@9e45b0fa, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@543295b0, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]].

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.5)

2023-01-13 18:39:06.184  INFO 18108 --- [           main] c.d.e.data.EncouragementRepositoryTest   : Starting EncouragementRepositoryTest using Java 17.0.4.1 on Me-LAPTOP with PID 18108 (started by me in C:\Users\Me\Documents\Projects\regular-encourager\encourage)
2023-01-13 18:39:06.186  INFO 18108 --- [           main] c.d.e.data.EncouragementRepositoryTest   : No active profile set, falling back to 1 default profile: "default"
2023-01-13 18:39:06.538  INFO 18108 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-01-13 18:39:06.587  INFO 18108 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 40 ms. Found 1 JPA repository interfaces.
2023-01-13 18:39:06.701  INFO 18108 --- [           main] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version
2023-01-13 18:39:06.964  INFO 18108 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:6f3c6875-d9bc-4c2c-b11f-3c25021050d9;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
2023-01-13 18:39:07.347  INFO 18108 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-01-13 18:39:07.412  INFO 18108 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.12.Final
2023-01-13 18:39:07.600  INFO 18108 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-01-13 18:39:07.690  INFO 18108 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect

    Hibernate: drop table if exists encouragement CASCADE 
    Hibernate: create table encouragement (id bigint not null, category 

varchar(255), message varchar(255), tone varchar(255), topic varchar(255), primary key (id))
2023-01-13 18:39:08.297  INFO 18108 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-01-13 18:39:08.304  INFO 18108 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-01-13 18:39:08.775  INFO 18108 --- [           main] c.d.e.data.EncouragementRepositoryTest   : Started EncouragementRepositoryTest in 3.055 seconds (JVM running for 4.262)
2023-01-13 18:39:08.818  INFO 18108 --- [           main] o.s.t.c.transaction.TransactionContext   : Began transaction (1) for test context [DefaultTestContext@20d7d6fb testClass = EncouragementRepositoryTest, testInstance = com.dropaflame.encourage.data.EncouragementRepositoryTest@59d4cd39, testMethod = find_all@EncouragementRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@4ef782af testClass = EncouragementRepositoryTest, locations = '{}', classes = '{class com.dropaflame.encourage.EncourageApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@11d8ae8b 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.sql.init.SqlInitializationAutoConfiguration, 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@130c12b7, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@5d534f5d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@6340e5f0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6c1a5b54, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@9e45b0fa, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@543295b0, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@50f05307]; rollback [true]
Hibernate: select encouragem0_.id as id1_0_, encouragem0_.category as category2_0_, encouragem0_.message as message3_0_, encouragem0_.tone as tone4_0_, encouragem0_.topic as topic5_0_ from encouragement encouragem0_
2023-01-13 18:39:09.244  INFO 18108 --- [           main] o.s.t.c.transaction.TransactionContext   : Rolled back transaction for test: [DefaultTestContext@20d7d6fb testClass = EncouragementRepositoryTest, testInstance = com.dropaflame.encourage.data.EncouragementRepositoryTest@59d4cd39, testMethod = find_all@EncouragementRepositoryTest, testException = org.opentest4j.AssertionFailedError: expected: <33> but was: <0>, mergedContextConfiguration = [MergedContextConfiguration@4ef782af testClass = EncouragementRepositoryTest, locations = '{}', classes = '{class com.dropaflame.encourage.EncourageApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@11d8ae8b 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.sql.init.SqlInitializationAutoConfiguration, 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@130c12b7, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@5d534f5d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@6340e5f0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6c1a5b54, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@9e45b0fa, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@543295b0, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]

**org.opentest4j.AssertionFailedError: 
Expected :33
Actual   :0**

CodePudding user response:

The problem is you are mixing together concepts from different testing domains.

The purpose of @DataJpaTest annotation is to delegate all "infrastructure magic" to spring-boot:

By default, tests annotated with @DataJpaTest are transactional and roll back at the end of each test. They also use an embedded in-memory database (replacing any explicit or usually auto-configured DataSource). The @AutoConfigureTestDatabase annotation can be used to override these settings.

You may actually observe that @DataJpaTest replaces database configuration - your logs contain the following:

Replacing 'dataSource' DataSource bean with embedded version
Starting embedded database: url='jdbc:h2:mem:6f3c6875-d9bc-4c2c-b11f-3c25021050d9;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'

The options are:

  • disable that replacement via marking test class with @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) or setting spring.test.database.replace=none in application properties
  • learn how bootstrap embedded database

CodePudding user response:

H2 database is just dummy database, it is just used for practice purpose, when you stop the server, I mean when your spring boot application stops, all the data dissappears, and you can't access even the H2 database then

  • Related