简体   繁体   English

Mapstruct测试无法在SpringBoot测试中自动连线

[英]Mapstruct test could not autowire in SpringBoot test

I've just added Mapstruct library to my project and I'm trying to figure out how it works. 我刚刚将Mapstruct库添加到我的项目中,并且试图弄清楚它是如何工作的。 When trying to run test case, my mapper object is null and I can't figure out why. 尝试运行测试用例时,我的mapper对象为null,我不知道为什么。 This is how I've defined the interface: 这就是我定义接口的方式:

@Mapper(componentModel = "spring")
public interface ReviewMapper {
    RideReview toRideReviewEntity(ReviewDTO reviewDTO);
}

So from what I've read in other topics, the componentModel="spring" should do the trick with instantiating the mapper in my test. 因此,从我在其他主题中阅读的内容来看, componentModel="spring"应该通过在测试中实例化映射器来解决问题。 That's not working for some reason. 由于某种原因,这没有用。 This is the simple test class I've written just to check whether instantiating works or not: 这是我编写的简单测试类,仅用于检查实例化是否有效:

@SpringBootTest
public class UtilityTest {

    @Autowired
    private ReviewMapper reviewMapper;

    @Test
    public void test() {
        assertTrue(reviewMapper != null);
    }

}

Even before running the test, IDE says "Could not autowire. No beans of 'ReviewMapper' type found.". 甚至在运行测试之前,IDE都会说“无法自动装配。找不到'ReviewMapper'类型的bean。”。 What can be the problem? 可能是什么问题?

Also, this is my pom.xml file if it can be the case: 另外,如果可能的话,这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.powderize</groupId>
    <artifactId>powderize</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>powderize</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- POST INITIALIZR DEPENDENCIES -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher -->
            <version>${org.mapstruct.version}</version>
        </dependency>
        <!-- tests -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source> <!-- or higher, depending on your project -->
                    <target>1.8</target> <!-- or higher, depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

I've finally found a solution. 我终于找到了解决方案。 I'm not sure about this, but it looks like Mapstruct is somehow connected to Lombok(?) - I'm guessing it because there have been some conflicts between them - adding the plugin also caused the compiler to fail on Lombok's getters. 我不确定这一点,但看起来Mapstruct某种程度上已连接到Lombok(?)-我猜是因为它们之间存在一些冲突-添加插件也导致编译器在Lombok的getter上失败。 Also, the GitHub link below connects Lombok and Mapstruct, not sure if it's just a coincidence. 另外,下面的GitHub链接将Lombok和Mapstruct连接起来,不确定是否只是巧合。

Instead of adding plugin like the documentation suggests, I've removed it and created two dependencies instead. 我没有像文档中建议的那样添加插件,而是删除了它并创建了两个依赖项。 Although it's described in the documentation , the solution with true, like suggested there, didn't work for me either. 尽管已在文档中进行了描述,但如我所建议的那样,true的解决方案对我也不起作用。 I've found a solution on GitHub . 我在GitHub上找到了一个解决方案。

So, instead of the pom.xml presented in the question, my full pom.xml now looks this way: 因此,现在我的完整pom.xml看起来不是问题中显示的pom.xml那样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.powderize</groupId>
    <artifactId>powderize</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>powderize</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- POST INITIALIZR DEPENDENCIES -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
            <scope>provided</scope>
        </dependency>
        <!-- tests -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Also, working solution confirmed in the test class, both tests passed: 另外,在测试课程中确认工作解决方案后,两个测试均通过了:

@SpringBootTest
@RunWith(SpringRunner.class)
public class UtilityTests {

    @Autowired
    private ReviewMapper reviewMapper;

    @Test
    public void reviewMapperIsNotNull(){
        assertThat(reviewMapper, notNullValue());
    }

    @Test
    public void testMapper(){
        RideReviewDTO reviewDTO = new RideReviewDTO();
        reviewDTO.setDescription("Test RideReviewDTO description");
        reviewDTO.setUserRating(5.0F);
        RideReview rideReview = reviewMapper.toEntity(reviewDTO);
        assertThat(rideReview.getUserRating(), equalTo(5.0F));
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM