In my Spring Boot Application, I am writing integration tests using @SpringBootTest. I am using a config class for Configuration and used @ComponentScan on it but it's not loading components and tests are failing.
ApplicationConfig.java
@Configuration
@ComponentScan(basePackages = { "com.example.inventory" })
public class ApplicationConfig {
@Bean
public MapperFactory mapperFactory() {
return new DefaultMapperFactory.Builder().build();
}
}
Here DefaultMapperFactory is the part of Orika Mapper which I use to convert model to DTO and vice-versa.
ProductSupplierIntegrationTests.java
@SpringBootTest(classes = ApplicationConfig.class)
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class ProductSupplierIntegrationTests {
// tests
}
Product Supplier Model
@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
}
Supplier Model
@Entity
public class Supplier {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
// getters and setters
}
ProductSupplierDTO
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 Mapper Configuration
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();
}
}
When I run Application, spring loads mapper and register it in contexts an converts between pojo as I specified but When I run Integration test, the mapper is not loaded and pojos are converted with default orika configuration and the fields firstName, lastName & email are not mapped in DTO.
Can anyone tell what am I missing in test configuration ?