1
votes

Have a springboot application which reads files from source directory using file-inbound-adapter.Written junit testcases for it. Junit testcase execute successfully in my local eclipse. but facing issue while running it from bamboo/jenkins which is deployed in another machine.

Unable to load the applicationcontext while executing spring boot junit testcases. 'filesInChannel.adapter.source' Caused by: java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.

Whats the route cause for this. Any suggestion/advice on this? is it due to some permission issue in bamboo deployed machine?

Have written a seperate configuration for test case. But not sure why its pointing to original xml to load application context while executing test cases.

Error:

   java.lang.IllegalStateException: Failed to load ApplicationContext
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter': Cannot resolve reference to bean 'filesInChannel.adapter.source' while setting bean property 'source'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.
    Caused by: java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.

source Code of file-process.xml :

<file:inbound-channel-adapter id="filesInChannel"
        directory="/filepath/" auto-startup="false" scanner="recursiveScanner" auto-create-directory="true">
                <integration:poller id="poller" max-messages-per-poll="${max.messages.per.poll}" fixed-rate="${message.read.frequency}" task-executor="pollingExecutor">
            <integration:transactional transaction-manager="transactionManager" />
        </integration:poller>       
    </file:inbound-channel-adapter>

test case:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class FileApplicationTests {

    @Test
    public void contextLoads() {
    }

}

TestConfig:

@Configuration
@ImportResource({ "classpath:test-file-process.xml" })
public class TestConfig {

}

source Code of test-file-process.xml :

<file:inbound-channel-adapter id="filesInChannel"
        directory="file:${java.io.tmpdir}/filepath/" auto-startup="false" scanner="recursiveScanner" auto-create-directory="true">
                <integration:poller id="poller" max-messages-per-poll="${max.messages.per.poll}" fixed-rate="${message.read.frequency}" task-executor="pollingExecutor">
            <integration:transactional transaction-manager="transactionManager" />
        </integration:poller>
    </file:inbound-channel-adapter>

Log :

018-09-11 14:54:39.893  INFO 9224 --- [           main] c.o.i.f.h.ErrorHandlerTest               : Starting ErrorHandlerTest on pc226116 with PID 9224 (started by test in D:\Repo\file-handler)
2018-09-11 14:54:39.896  INFO 9224 --- [           main] c.o.i.f.h.ErrorHandlerTest               : No active profile set, falling back to default profiles: default
2018-09-11 14:54:39.901  INFO 9224 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@22dc59b2: startup date [Tue Sep 11 14:54:39 IST 2018]; root of context hierarchy
2018-09-11 14:54:39.910  INFO 9224 --- [           main] o.s.b.f.x.XmlBeanDefinitionReader        : Loading XML bean definitions from class path resource [test-file-process.xml]
2018-09-11 14:54:40.158  INFO 9224 --- [           main] o.s.i.c.IntegrationRegistrar             : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2018-09-11 14:54:40.161  INFO 9224 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'configurationValidator' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.ConfigurationValidator]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\ConfigurationValidator.class]] with [Generic bean: class [com.interfaces.file.handler.ConfigurationValidator]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=init; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.164  INFO 9224 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'cacheManager' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.CacheManager]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\CacheManager.class]] with [Generic bean: class [org.springframework.cache.support.SimpleCacheManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.167  INFO 9224 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'headerEnricher' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.HeaderEnricher]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\HeaderEnricher.class]] with [Generic bean: class [com.interfaces.file.handler.HeaderEnricher]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.182  INFO 9224 --- [           main] .f.x.DefaultBeanDefinitionDocumentReader : Skipped XML bean definition file due to specified profiles [aws] not matching: class path resource [test-file-process.xml]
2018-09-11 14:54:40.548  INFO 9224 --- [           main] o.s.b.f.x.XmlBeanDefinitionReader        : Loading XML bean definitions from class path resource [file-process.xml]
2018-09-11 14:54:40.795  INFO 9224 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'transactionManager' with a different definition: replacing [Generic bean: class [org.springframework.integration.transaction.PseudoTransactionManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]] with [Generic bean: class [org.springframework.integration.transaction.PseudoTransactionManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [file-process.xml]]
1
Can you share the log output when your test runs? I would guess that your test's inbound channel adapter is being defined first and then overridden by the adapter defined in file-process.xml.Andy Wilkinson
yes correct. can you provide the solution for this. Have attached the log where its overridingJessie
See my answer below.Artem Bilan

1 Answers

1
votes

I think the problem here that <file:inbound-channel-adapter> creates actually several beans, where one of them is FileReadingMessageSource with the condition like:

Assert.isTrue(this.directory.exists(),
            "Source directory [" + this.directory + "] does not exist.");

just in its afterPropertiesSet() independently of the auto-startup="false".

I'm afraid exactly this bean is not overridden, just because its id is auto-generated.

That's how another config doesn't help.

I suggest you to take a look into the profiles support: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-definition-profiles

And wrap that <file:inbound-channel-adapter> into something like:

<beans:beans profile="!test">
</beans:beans>