Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动测试未加载带有@Component注释的类_Java_Spring Boot_Integration Testing_Spring Boot Test_Orika - Fatal编程技术网

Java Spring启动测试未加载带有@Component注释的类

Java Spring启动测试未加载带有@Component注释的类,java,spring-boot,integration-testing,spring-boot-test,orika,Java,Spring Boot,Integration Testing,Spring Boot Test,Orika,在我的SpringBoot应用程序中,我正在使用@SpringBootTest编写集成测试。我正在使用一个配置类进行配置,并在其上使用了@ComponentScan,但它没有加载组件,测试失败 ApplicationConfig.java @Configuration @ComponentScan(basePackages = { "com.example.inventory" }) public class ApplicationConfig { @Bean public M

在我的SpringBoot应用程序中,我正在使用@SpringBootTest编写集成测试。我正在使用一个配置类进行配置,并在其上使用了@ComponentScan,但它没有加载组件,测试失败

ApplicationConfig.java

@Configuration
@ComponentScan(basePackages = { "com.example.inventory" })
public class ApplicationConfig {

    @Bean
    public MapperFactory mapperFactory() {
        return new DefaultMapperFactory.Builder().build();
    }
}
@SpringBootTest(classes = ApplicationConfig.class)
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class ProductSupplierIntegrationTests {
    // tests
}
这里DefaultMapperFactory是Orika Mapper的一部分,我使用它将模型转换为DTO,反之亦然

ProductSupplierIntegrationTests.java

@Configuration
@ComponentScan(basePackages = { "com.example.inventory" })
public class ApplicationConfig {

    @Bean
    public MapperFactory mapperFactory() {
        return new DefaultMapperFactory.Builder().build();
    }
}
@SpringBootTest(classes = ApplicationConfig.class)
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class ProductSupplierIntegrationTests {
    // tests
}
产品供应商模型

@Entity
public class ProductSupplier {

    @Id
    @GeneratedValue
    private Long id;

    private Long supplierId;

    @ManyToOne
    private Supplier supplier;

    private Double buyPrice;

    private boolean defaultSupplier;

    // getters and setters
}
@Entity
public class Supplier {

    @Id
    @GeneratedValue
    private Long id;

    private String firstName;

    private String lastName;

    private String email;

    // getters and setters
}
供应商型号

@Entity
public class ProductSupplier {

    @Id
    @GeneratedValue
    private Long id;

    private Long supplierId;

    @ManyToOne
    private Supplier supplier;

    private Double buyPrice;

    private boolean defaultSupplier;

    // getters and setters
}
@Entity
public class Supplier {

    @Id
    @GeneratedValue
    private Long id;

    private String firstName;

    private String lastName;

    private String email;

    // getters and setters
}
产品供应商到

public class ProductSupplierDTO {

    private Long supplierId;

    private String firstName;

    private String lastName;

    private String email;

    private Double buyPrice;

    private Boolean defaultSupplier;

    // getters and setters
}
Orika映射器配置

package com.example.inventory.mapper;

@Component
public class ProductSupplierMapper implements OrikaMapperFactoryConfigurer {

    @Override
    public void configure(MapperFactory orikaMapperFactory) {
        orikaMapperFactory.classMap(ProductSupplier.class, ProductSupplierDTO.class).exclude("id")
                .field("supplier.firstName", "firstName").field("supplier.lastName", "lastName")
                .field("supplier.email", "email").byDefault().register();
    }

}
当我运行应用程序时,spring加载映射器并在上下文中注册它,并按照我的指定在pojo之间进行转换,但当我运行集成测试时,映射器未加载,pojo使用默认orika配置进行转换,字段firstName、lastName和email未映射到DTO中


有人能告诉我在测试配置中缺少了什么吗?

您是否尝试过在现场不使用dot。我的意思是没有必要使用suplier。只需使用字段名。点用于在一个类中映射两个类的属性。我不认为orika是这里的问题,因为当我运行主应用程序时,它可以完美地工作。您的
@SpringBootTest
应该引用
@SpringBootApplication
注释类。@ComponentScan注释正确吗?您的类真的在“com.example.inventory”中吗?如果我使用
@SpringBootApplication
,那么它会为每个测试加载应用程序上下文。