首页 文章

使用uri中的方法选项模拟bean endpoints

提问于
浏览
0

我使用Apache Camel 2.15.3构建应用程序 . 我使用spring-xml连接路由以进行依赖注入 . 我正在尝试编写一个测试,我在其中模拟一个bean的 endpoints 并在uri中有一个方法选项 .

我的路线看起来像这样:

<onException id="Exception">
    <exception>java.lang.Exception</exception>
    <handled>
        <constant>true</constant>
    </handled>
    <to uri="direct:fear"/>
</onException>

<route id="happyStory">
    <from uri="direct:inTheBeginning"/>
    <to uri="bean:enchantedKingdom?method=warn" />
    <to uri="bean:fluffykins" />
</route>

<route id="scaryStory">
    <from uri="direct:fear"/>
    <onException>
        <exception>java.lang.Exception</exception>
        <handled>
            <constant>true</constant>
        </handled>
    </onException>
    <to uri="bean:monster"/>
    <choice>
        <when>
            <simple>${header.succesfullywarned}</simple>
            <to uri="bean:enchantedKingdom?method=hide"/>
        </when>
        <otherwise>
            <to uri="bean:enchantedKingdom?method=panic" />
        </otherwise>
    </choice>
</route>

而且我不能说当调用bean方法warn然后应该在消息中设置 Headers “successcesfullywarned”然后当调用bean fluffykins时应该有一个异常导致消息被发送to“scaryStory”,在这种情况下,我不想断言bean方法'panic'被调用 .

这是测试:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration({"/META-INF/spring/route-stories.xml","/META-INF/spring/beans.xml"})
@MockEndpointsAndSkip("(bean:fluffykins|bean:monster|bean:enchantedKingdom?method=warn|bean:enchantedKingdom?method=hide|bean:enchantedKingdom?method=panic)")
public class StoryHappyRouteTest extends CamelSpringTestSupport {

    private String url = "direct:inTheBeginning";

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    protected AbstractApplicationContext createApplicationContext() {
       return (AbstractApplicationContext)applicationContext;
    }

    @Test
    public void test(){

        MockEndpoint warn = getMockEndpoint("mock:bean:enchantedKingdom?method=warn");
        MockEndpoint fluffy = getMockEndpoint("mock:bean:fluffykins");
        MockEndpoint  monster = getMockEndpoint("mock:bean:monster");
        MockEndpoint hide = getMockEndpoint("mock:bean:enchantedKingdom?method=hide");
        MockEndpoint panic = 
        getMockEndpoint("mock:bean:enchantedKingdom?method=panic");

        fluffy.whenAnyExchangeReceived(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            System.out.println("Bunny!");
            throw new NullPointerException();
        }
        });

        template.sendBody(url,"");

        warn.assertExchangeReceived(0);
        fluffy.assertExchangeReceived(0);
        monster.assertExchangeReceived(0);
        panic.assertExchangeReceived(0);

   }
}

它适用于除enchantedKingdom bean之外的所有bean,它包含路由中使用的多个方法 . 在这种情况下不使用mock,但调用了真正的bean方法,这不是我想要的 . 测试失败,因为它不是在路由中调用的模拟 .

如何通过uri'bean来测试使用模拟器进行endpoits:enchantedKingdom?method = warn','bean:enchantedKingdom?method = hide'和'bean:enchantedKingdom?method = panic'?

1 回答

  • 0

    我会改变测试方法而不是尝试模拟对bean的调用 . 相反,创建一个模拟的bean实例并在测试中使用它 .

    定义一个模拟bean(通过代码或模拟库)

    public class MockEnchantedKingdom {
    
        public boolean panicCalled = false;
    
        public void panic() {
            // do things
            panicCalled = true;
        }
    }
    

    然后在用于测试目的的spring文件中声明此bean

    @ContextConfiguration(
        {"/META-INF/spring/route-stories.xml",
        "/META-INF/spring/beans-test1.xml"})
    

    在您的测试代码中获取bean并断言您需要的内容

    // context is CamelContext you should have access to it
    MockEnchantedKingdom enchantedKingdom = (MockEnchantedKingdom) context.getRegistry().lookupByName("enchantedKingdom");
    Assert.asserttrue(enchantedKingdom.panicCalled);
    

    为不同的测试创建不同的 beans-test*.xml ,您可以使用Mockito或任何其他库创建模拟 .

    路径代码始终相同,您可以控制每个测试中bean的行为 .

相关问题