Maven 对所有类运行测试错误,但对某个类工作正常

问题描述 投票:0回答:1

我有一个测试类,我想测试 JPA 查询。

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

当我跑步时运行良好

mvn clean test "-Dtest=JPAUnitTest"
。 然而,当我运行
mvn clean test
时,该类给出了以下异常

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=?]

该架构似乎不起作用,但我不明白为什么。 奇怪的是,上述错误仅在我对所有类运行测试时才会发生。另请注意,如果我在 IDEA (IntelliJ) 中运行测试,所有测试用例都会正常工作。

我正在使用

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"

这是我的设置

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

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>

如果您需要更多信息,请告诉我,谢谢。

maven mockito spring-boot-test maven-surefire-plugin
1个回答
0
投票

通过将

@TestPropertySource(locations = "classpath:application.yaml")
添加到
JPAUnitTest
类解决了问题:

@SpringBootTest
@Transactional
@TestPropertySource(locations = "classpath:application.yaml")
class JPAUnitTest {
  // all same
}

不过,我还是不知道是什么原因导致了这个问题。默认情况下,测试类应使用 application.yaml 中的配置,但在运行所有测试类时,情况似乎并非如此。

© www.soinside.com 2019 - 2024. All rights reserved.