Reputation: 55
I have a test class which I want to test for JPA query.
@SpringBootTest
@DirtiesContext
class JPAUnitTest {
@Autowired
private OrganizationRepository organizationRepository;
@Autowired
private SubscriberRepository subscriberRepository;
@Autowired
private DistrictRepository districtRepository;
@BeforeAll
static void setUp() {
Locale.setDefault(Locale.ENGLISH);
}
@Test
@Transactional
@Rollback
public void deleteSubscriber() {
Organization organization = Organization.builder()
.id(UUID.fromString("112a28e3-9254-47d5-8d96-e9f979106f07"))
.contactPerson("TestPerson")
.name("Yunex Traffic")
.email("[email protected]")
.mobileNumber("9168912345")
.country("Germany")
.region("Berlin")
.districts(new HashSet<>())
.subscribers(new ArrayList<>())
.build();
Subscriber subscriber = getSubscriber();
subscriber.setOrganization(organization);
subscriber.setName("Another name");
subscriber.setEmail(null);
subscriber.setMobileNumber(null);
subscriber.setNotifyByEmail(false);
subscriber.setNotifyBySMS(false);
organization.addSubscriber(subscriber);
organizationRepository.saveAndFlush(organization);
subscriberRepository.saveAndFlush(subscriber);
subscriberRepository.delete(subscriber);
}
}
which running fine when I run mvn clean test "-Dtest=JPAUnitTest"
.
However when I run mvn clean test
, the class given following exception
JPAUnitTest.deleteSubscriber:155 » InvalidDataAccessResourceUsage could not prepare statement [Table "ORGANIZATION" not found (this database is empty); SQL statement:
select o1_0.id,o1_0.contact_person,o1_0.country,o1_0.created_by,o1_0.creation_time,o1_0.email,o1_0.mobile_number,o1_0.name,o1_0.region,o1_0.updated_by,o1_0.updated_time,e1_0.organization_id,e1_0.id,e1_0.description,e1_0.is_active,e1_0.is_system_defined,e1_0.name,e1_0.object_type_id,ot1_0.id,ot1_0.classification,ot1_0.data_struct,ot1_0.enabled,f1_0.id,f1_0.enabled,f1_0.i18n_key_name,f1_0.name,ot1_0.i18n_key_name,ot1_0.name,ot1_0.scs,ot1_0.type,ot1_0.version,e1_0.operator_id,o2_0.id,dt1_0.id,dt1_0.i18n_key_name,dt1_0.name,dt1_0.type_enum,dt1_0."values",o2_0.description,o2_0.feel_expression,o2_0.i18n_key_name,o2_0.name,o2_0.operator_enum,o2_0.pretty_expression from config_api_test.organization o1_0 left join config_api_test.expression e1_0 on o1_0.id=e1_0.organization_id left join config_api_test.object_type ot1_0 on ot1_0.id=e1_0.object_type_id left join config_api_test.object_type_family f1_0 on f1_0.id=ot1_0.family_id left join config_api_test.operator o2_0 on o2_0.id=e1_0.operator_id left join config_api_test.data_type dt1_0 on dt1_0.id=o2_0.data_type_id where o1_0.id=? [42104-224]] [select o1_0.id,o1_0.contact_person,o1_0.country,o1_0.created_by,o1_0.creation_time,o1_0.email,o1_0.mobile_number,o1_0.name,o1_0.region,o1_0.updated_by,o1_0.updated_time,e1_0.organization_id,e1_0.id,e1_0.description,e1_0.is_active,e1_0.is_system_defined,e1_0.name,e1_0.object_type_id,ot1_0.id,ot1_0.classification,ot1_0.data_struct,ot1_0.enabled,f1_0.id,f1_0.enabled,f1_0.i18n_key_name,f1_0.name,ot1_0.i18n_key_name,ot1_0.name,ot1_0.scs,ot1_0.type,ot1_0.version,e1_0.operator_id,o2_0.id,dt1_0.id,dt1_0.i18n_key_name,dt1_0.name,dt1_0.type_enum,dt1_0."values",o2_0.description,o2_0.feel_expression,o2_0.i18n_key_name,o2_0.name,o2_0.operator_enum,o2_0.pretty_expression from config_api_test.organization o1_0 left join config_api_test.expression e1_0 on o1_0.id=e1_0.organization_id left join config_api_test.object_type ot1_0 on ot1_0.id=e1_0.object_type_id left join config_api_test.object_type_family f1_0 on f1_0.id=ot1_0.family_id left join config_api_test.operator o2_0 on o2_0.id=e1_0.operator_id left join config_api_test.data_type dt1_0 on dt1_0.id=o2_0.data_type_id where o1_0.id=?]; SQL [select o1_0.id,o1_0.contact_person,o1_0.country,o1_0.created_by,o1_0.creation_time,o1_0.email,o1_0.mobile_number,o1_0.name,o1_0.region,o1_0.updated_by,o1_0.updated_time,e1_0.organization_id,e1_0.id,e1_0.description,e1_0.is_active,e1_0.is_system_defined,e1_0.name,e1_0.object_type_id,ot1_0.id,ot1_0.classification,ot1_0.data_struct,ot1_0.enabled,f1_0.id,f1_0.enabled,f1_0.i18n_key_name,f1_0.name,ot1_0.i18n_key_name,ot1_0.name,ot1_0.scs,ot1_0.type,ot1_0.version,e1_0.operator_id,o2_0.id,dt1_0.id,dt1_0.i18n_key_name,dt1_0.name,dt1_0.type_enum,dt1_0."values",o2_0.description,o2_0.feel_expression,o2_0.i18n_key_name,o2_0.name,o2_0.operator_enum,o2_0.pretty_expression from config_api_test.organization o1_0 left join config_api_test.expression e1_0 on o1_0.id=e1_0.organization_id left join config_api_test.object_type ot1_0 on ot1_0.id=e1_0.object_type_id left join config_api_test.object_type_family f1_0 on f1_0.id=ot1_0.family_id left join config_api_test.operator o2_0 on o2_0.id=e1_0.operator_id left join config_api_test.data_type dt1_0 on dt1_0.id=o2_0.data_type_id where o1_0.id=?]
The schema seem not working but I don't understand why. It is weird that above error only happen when I run test for all classes. Also note that all test cases working nice if I run test in my IDEA (IntelliJ).
I am using
Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: C:\Users\base\Desktop\apache-maven-3.9.6-bin\apache-maven-3.9.6
Java version: 17.0.10, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-17
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
This is my settings application.yaml
spring:
sql:
init:
mode: always
main:
allow-bean-definition-overriding: true
jpa:
database-platform: org.hibernate.dialect.H2Dialect
properties:
hibernate:
format_sql: true
auto_quote_keyword: true
id:
db_structure_naming_strategy: legacy
default_schema: config_api_test
show-sql: true
hibernate:
ddl-auto: create-drop
liquibase:
enabled: false
h2:
console:
enabled: true
datasource:
platform: h2
initialization-mode: embedded
driver-class-name: org.h2.Driver
jdbc-url: jdbc:h2:mem:notification;DB_CLOSE_DELAY=-1
username: sa
password: sa
name: config_api_test
and pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
If you need more information please let me know, thanks.
Upvotes: 0
Views: 50
Reputation: 55
The problem was fixed by added @TestPropertySource(locations = "classpath:application.yaml")
to JPAUnitTest
class:
@SpringBootTest
@Transactional
@TestPropertySource(locations = "classpath:application.yaml")
class JPAUnitTest {
// all same
}
Still, I don't know what cause the problem in the first place. The test class should use configuration from application.yaml by default but when running all test classes, it seem not the case.
Upvotes: 0