J Harish
J Harish

Reputation: 195

Spring 3.2.2 combined with Embeded mongoDB throws Auto configuration error

The application is currently undergoing an upgrade from Spring 2.7 to Spring 3.2.2. However, after upgrading, the integration tests started to report an error. Below are the dependencies for both versions of Spring:

Spring 2.7 dependencies:

 <properties>
        <spring-boot-version>2.7.15</spring-boot-version>        
        <json-path-version>2.8.0</json-path-version>        
        <openpojo-version>0.9.1</openpojo-version>
        <embedmongo-version>3.5.4</embedmongo-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot-version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>        
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>${json-path-version}</version>
            <scope>compile</scope>
        </dependency>        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <dependency>
            <groupId>com.openpojo</groupId>
            <artifactId>openpojo</artifactId>
            <scope>test</scope>
            <version>${openpojo-version}</version>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <version>${embedmongo-version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Spring 3.2.2 dependencies:

    <properties>
        <spring-boot-version>3.2.2</spring-boot-version>        
        <json-path-version>2.9.0</json-path-version>        
        <openpojo-version>0.9.1</openpojo-version>
        <embedmongo-version>4.11.0</embedmongo-version>
    </properties>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot-version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>       
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>${json-path-version}</version>
            <scope>compile</scope>
        </dependency>        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>
        <dependency>
            <groupId>com.openpojo</groupId>
            <artifactId>openpojo</artifactId>
            <scope>test</scope>
            <version>${openpojo-version}</version>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo.spring30x</artifactId>
            <version>${embedmongo-version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

As for the context code,

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = ProposalController.class)
@AutoConfigureDataMongo
@TestPropertySource(properties = "de.flapdoodle.mongodb.embedded.version=4.4.19")
public class CarrierAdapter_IT {

    @ComponentScan("com.adapter")
    @SpringBootApplication
    static class mainForTest {
    }

    @MockBean
    public RestTemplate restTemplate;

    @MockBean
    Validator validator;

    @Autowired
    public MockMvc mockMvc;

    @Test
    void test(){}

}

@Configuration
@ConditionalOnProperty(
        value = "adapter.persistence.default.enabled",
        havingValue = "true"
)
@EnableMongoRepositories(basePackages = "com.package.adapter")
public class MongoConfig {
    @Bean
    public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory factory, MongoMappingContext context, BeanFactory beanFactory) {
        context.setAutoIndexCreation(true);
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
        return mappingConverter;
    }

    @Bean
    public MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    }

}

@Repository
@ConditionalOnProperty(
        value = "adapter.persistence.default.enabled",
        havingValue = "true"
)
public interface DefaultWorkflowStepExecutionStateRepository extends MongoRepository<WorkflowStepExecutionStateDocument, String> {   

    default void createWorkflowDocument(String proposalId String responsePayload) {
        WorkflowStepExecutionStateDocument workflowStepExecutionStateDocument = new WorkflowStepExecutionStateDocument();
        workflowStepExecutionStateDocument.setProposalId(proposalId);        
        workflowStepExecutionStateDocument.setCarrierResponses(Collections.singletonList(responsePayload));       
        save(workflowStepExecutionStateDocument);
    }    
}

@RestController
public class ProposalController{

    @Lazy
    @Autowired
    private ProposalService proposalService;

    @Lazy
    @Autowired
    private List<WorkflowStepService> workflowStepServices;

    
    @PostMapping(value = "/proposals", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Proposal> createProposal(@RequestHeader Map<String, String> headers, @RequestBody Proposal proposal) {
        Proposal createdProposal = proposalService.createProposal(headers, proposal);
        return new ResponseEntity<>(createdProposal, HttpStatus.CREATED);
    }
}

Currently, I am attempting to locate the issue with Autoconfiguration, but I have been unable to find it so far. below is the error

Exception encountered during context initialization - cancelling refresh attempt:
 org.springframework.beans.factory.UnsatisfiedDependencyException:
 Error creating bean with name 'mockItWorkflowStepImpl' defined in file 
 [C:\\Users\\user\\core-platform-repos\\adapter\\target\\test-classes\\com\\adapter\\workflow\\WorkflowStepImpl.class]:
 Unsatisfied dependency expressed through constructor parameter 0: 
 Error creating bean with name 'defaultWorkflowStepExecutionStateRepository' 
 defined in com.adapter.persistence.repository.DefaultWorkflowStepExecutionStateRepository defined in @EnableMongoRepositories declared on MongoConfig:
 Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'"

Upvotes: 0

Views: 598

Answers (0)

Related Questions