5

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 ?

Harshit
  • 1,334
  • 1
  • 9
  • 23
  • Have you tried without dot in the field. I mean there is no need use suplier. simply use field names. – mallikarjun Dec 29 '17 at 10:04
  • dot is used to map two class's properties in one class. I don't think that orika is the issue here as it works perfectly when I run Main Applicaton. – Harshit Dec 29 '17 at 10:09
  • Your `@SpringBootTest` should reference the `@SpringBootApplication` annotated class. – M. Deinum Dec 29 '17 at 10:12
  • Is that @ComponentScan annotation correct - are your classes really in "com.example.inventory"? – Riaan Nel Dec 29 '17 at 10:14
  • If I use `@SpringBootApplication` then it loads application context for each test. – Harshit Dec 29 '17 at 10:19
  • Yes `@ComponentScan` annotation is correct and my class is in `com.example.inventory.mapper` package. – Harshit Dec 29 '17 at 10:20
  • Is `ApplicationConfig` specific to your test or not? If we want to load a specific configuration, we can use the classes attribute of `@SpringBootTest`, otherwise it will attempt to load `@Configuration` from any inner-classes, and if that fails, it will search for your primary `@SpringBootApplication` class. – Luay Abdulraheem Jan 02 '18 at 03:20
  • I just created Application class with `@SpringBootApplication` in Test Package and used `@ComponentScan` there. it's working perfect now. Thanks – Harshit Jan 02 '18 at 13:12

0 Answers0