Announcement Announcement Module
Collapse
No announcement yet.
How to test using SpringJUnit4ClassRunner? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to test using SpringJUnit4ClassRunner?

    Hi all,
    My application is in SI, it reads files and then puts is a channel. After reading the file file is moved to different location.
    Now I want to test this, my whole code is in context.xml. What I want is, to read the content of the file and assert it with the message in the channel. But the point here is as soon as I loads the context.xml the file is moved to the different location and data is converted in the messages and sent in the channel.

    So how can I test this requirement using SpringJUnit4ClassRunner.class?

    Thanks,
    Dev

  • #2
    Let's say your flow ends with an output channel adapter...

    Code:
    <file:outbound-channel-adapter id="outAdapter" 
    	channel="filesOut"
    	directory="file:${java.io.tmpdir}/spring-integration-samples/output"/>
    In your test case, import the main application context file and a test context file that overrides the outbound adapter...

    Code:
    <int:bridge id="outAdapter" input-channel="filesOut" output-channel="testQueueChannel"/>
    
    <int:channel id="testQueueChannel">
    	<int:queue/>
    </int:channel>
    This bridge will replace your outbound adapter and, instead, send the message to the QueueChannel.

    Autowire the test channel...

    Code:
    @ContextConfiguration(locations={"main-context.xml", "test-context.xml" })
    @RunWith(SpringJUnit4ClassRunner.class)
    public class MyTests {
    
    @Autowired
    PollableChannel testQueueChannel;
    
    ...
    Then, in your test, write a file to the input directory, and execute testQueueChannel.receive(30000) to get the message(s); you can then assert whatever you want.

    You will see this in your log...

    Code:
    ...DefaultListableBeanFactory - Overriding bean definition for bean 'outAdapter': replacing...

    Comment

    Working...
    X