服务测试在 VSCode IDE 中成功,但在 DAO 上使用 Mvn install 和 classnotfoundException 失败

Service test succeeds in VSCode IDE but fails using Mvn install with classnotfoundException on DAO

提问人:Coffee Bean 提问时间:8/22/2023 更新时间:8/22/2023 访问量:13

问:

以下服务测试失败,因为它无法使用 Springboot3 找到 DAO

当使用 MVN install 执行测试时,它会失败,但通过 IDE 使用 VSCode/Run test 执行会成功。

由于 VSCode 成功执行,我相信大多数组件都已到位,但是,由于 MVN 失败,也许我没有配置 MVN,但不确定要查看什么

这是一个多模块结构,父 POM 包含所有常见依赖项,而子 POM 包含特定依赖项。以下测试引用了另一个 maven 依赖项 jar 中的 DAO,如下所示。

服务 Pom/依赖关系:

<dependencies>
    <dependency>
        <groupId>au.com.cs.inventory</groupId>
        <artifactId>inv-data</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

服务测试类:

    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.Matchers.equalTo;
    import static org.junit.jupiter.api.Assertions.assertArrayEquals;
    import static org.mockito.ArgumentMatchers.any;
    import static org.mockito.ArgumentMatchers.anyLong;
    import static org.mockito.Mockito.when;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Optional;
    
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.junit.jupiter.MockitoExtension;
    
    import au.com.cs.inventory.data.dao.InventoryDao;
    import au.com.cs.inventory.data.model.ContainerDefinitionModel;
    
    @ExtendWith(MockitoExtension.class)
    public class InventoryServiceTest {
        @InjectMocks
        InventoryService inventoryService;
    
        @Mock
        private InventoryDao repository;
    
        @Test
        void get_containerDefintion() {
    
            final Optional<ContainerDefinitionModel> existingCdf = Optional
                    .of(ContainerDefinitionModel.builder().build());
            when(repository.findById(anyLong())).thenReturn(existingCdf);
    
            ContainerDefinitionModel foundCdf = inventoryService.getContainerDefinition(Long.valueOf(1));
    
            assertThat(foundCdf, equalTo(existingCdf.get()));
        }

...
...
}

错误:

java.lang.NoClassDefFoundError: au/com/cs/inventory/data/dao/InventoryDao
        at java.base/java.lang.Class.getDeclaredFields0(Native Method)
        at java.base/java.lang.Class.privateGetDeclaredFields(Class.java:3397)
        at java.base/java.lang.Class.getDeclaredFields(Class.java:2468)
        at org.junit.platform.commons.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:1473)
        at org.junit.platform.commons.util.ReflectionUtils.findAllFieldsInHierarchy(ReflectionUtils.java:1202)
        at org.junit.platform.commons.util.ReflectionUtils.findFields(ReflectionUtils.java:1190)

...


Caused by: java.lang.ClassNotFoundException: au.com.cs.inventory.data.dao.InventoryDao
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 43 more

DAO 类:

package au.com.cs.inventory.data.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import au.com.cs.inventory.data.model.ContainerDefinitionModel;

@Repository
public interface InventoryDao extends JpaRepository<ContainerDefinitionModel, Long> {

}

ContainerDefintionModel(实体):

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;


@Builder
@Entity
public class ContainerDefinitionModel {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String definitionName;

    public Long getId() {
        return id;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public String getDefinitionName() {
        return definitionName;
    }


    public void setDefinitionName(String definitionName) {
        this.definitionName = definitionName;
    }

}

我没有想法,所以任何想法将不胜感激。

TIA系列

java spring-boot maven 集成测试 noclassdeffounderror

评论


答: 暂无答案