简体   繁体   中英

Dirty test context since migrating from spring 4.0.x to 4.1.x

I'm trying to migrate an old applicaiton step by step, starting with migrating from spring 4.0 to 4.1. While the documentation says that there are no breaking changes, it seems otherwise.

My integration test looks somewhat like this:

@ContextConfiguration(locations = {"/com/a/KernelTestContext.xml"})
public class KernelTest extends AbstractJUnit4SpringContextTests {
    @Configuration
    static class ContextConfiguration  {
        @Bean
            ...
        }
    }

    @Resource(name = "CsvResourceProcessingChannel")
    private MessageChannel csvResourceProcessingChannel;

    @Test()
    public void testIt() {
        ...
        csvResourceProcessingChannel.send(MessageBuilder.withPayload(new ByteArrayResource(input.getBytes()))
            .setHeader(Headers.IMPORT_SOURCE, importSource1).setReplyChannel(testChannel)
            .setErrorChannel(testErrorChannel).build());
    }
}

The csvResourceProcessingChannel Bean accesses posts into the splitter bean CsvResourceSplitter, which is defined like this:

public class CsvResourceSplitter extends AbstractMessageSplitter {

    private final static Logger LOGGER = LoggerFactory.getLogger(CsvResourceSplitter.class);

    @Autowired
    private ApplicationContext applicationContext;

    private Map<Partner, CsvMappingStrategy> csvMappingMap;

    ...

    private void updateCsvMappings() {
        final Map<String, CsvMappingStrategy> mappingBeanMap = applicationContext.getBeansOfType(CsvMappingStrategy.class,
                false, true);
        csvMappingMap = Maps.newEnumMap(Partner.class);

        for (final CsvMappingStrategy csvMappingStrategy : mappingBeanMap.values()) {
            final CsvMappingStrategy previous = csvMappingMap.put(csvMappingStrategy.getPartner(),
                    csvMappingStrategy);
            Preconditions.checkArgument(previous == null, "More than one CsvMapping bean found for  partner: '%s",
                    csvMappingStrategy.getPartner());
        }
    }
}

The problem becomes noticeable with the final Preconditions check in updateCsvMappings: While the test only defines one CsvMappingStrategy class, several more including duplicates are found since the upgrade from spring 4.0 to 4.1. Due to the contents of mappingBeanMap during runtime I'm pretty sure that the context used here contains context elements used in CsvResourceSplitterTest, which looks somewhat like this:

@ContextConfiguration
public class CsvResourceSplitterTest extends AbstractJUnit4SpringContextTests {

    @Configuration
    static class Config {

        @Bean
        public CsvResourceSplitter createCsvResourceSplitter() {
            return new CsvResourceSplitter();
        }

        @Bean
        public CsvMappingStrategy createTestMappingStrategy() {
            return new AbstractCsvMappingStrategy() {

                ...

            };
        }
        ...
    }

    @Autowired
    private CsvResourceSplitter splitter;

    ...

}

Any ideas on what is going wrong are appreciated.

A useful workaround for this was moving the CsvResourceSplitterTest context to a different profile:

@ContextConfiguration
@ActiveProfiles("anonymous_profile")
public class CsvResourceSplitterTest extends AbstractJUnit4SpringContextTests {
    @Profile("anonymous_profile")
    @Configuration
    static class Config {
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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