模拟测试classSpring骆驼
Mocking test class Spring camel
我是骆驼的新手。我正在尝试编写一个测试用例。
public class A
{
private B b;
public void update(String s){
//calling some methods on B
.....
}
}
测试class
public class TestA extends CamelSpringTestSupport
{
private ClassPathXmlApplicationContext xmlAppContext;
@Test
public void testA()
{
String xml = "some xml";
Endpoint endpoint = context.getEndpoint("direct:incomingxml");
Exchange inExchange = endpoint.createExchange();
inExchange.getIn().setBody(xml);
inExchange.setPattern(ExchangePattern.InOnly);
template.send(endpoint, inExchange);
}
@Override
protected AbstractApplicationContext createApplicationContext()
{
xmlAppContext = new ClassPathXmlApplicationContext(
"classpath:/test-camel-context.xml");
return xmlAppContext;
}
}
spring豆子xml
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:incomingxml"/>
<to uri="bean:classA?method=update"/>
</route>
</camelContext>
<bean id="b" class="B">
</bean>
<bean id="classA" class="A">
<constructor-arg index="0" ref="b" />
</bean>
有几个使用真实对象预先编写的测试用例。有什么办法可以模拟这个 class B,注入 class A 并模拟一些方法吗?我只想在我的测试用例中做,以便预先编写的测试用例不受影响?
你可以通过在class A中添加一个setter来解决这个问题。
应用程序上下文将被加载,A 的 B 对象将由 XML 中声明的 bean 注入,但您仍然可以通过调用新定义的 setter 来使用 B 的模拟覆盖它在你的测试中。
然后通过这样做,B 的 mock 将在您的测试中使用,而不是 bean。其他测试用例不受影响
我是骆驼的新手。我正在尝试编写一个测试用例。
public class A
{
private B b;
public void update(String s){
//calling some methods on B
.....
}
}
测试class
public class TestA extends CamelSpringTestSupport
{
private ClassPathXmlApplicationContext xmlAppContext;
@Test
public void testA()
{
String xml = "some xml";
Endpoint endpoint = context.getEndpoint("direct:incomingxml");
Exchange inExchange = endpoint.createExchange();
inExchange.getIn().setBody(xml);
inExchange.setPattern(ExchangePattern.InOnly);
template.send(endpoint, inExchange);
}
@Override
protected AbstractApplicationContext createApplicationContext()
{
xmlAppContext = new ClassPathXmlApplicationContext(
"classpath:/test-camel-context.xml");
return xmlAppContext;
}
}
spring豆子xml
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:incomingxml"/>
<to uri="bean:classA?method=update"/>
</route>
</camelContext>
<bean id="b" class="B">
</bean>
<bean id="classA" class="A">
<constructor-arg index="0" ref="b" />
</bean>
有几个使用真实对象预先编写的测试用例。有什么办法可以模拟这个 class B,注入 class A 并模拟一些方法吗?我只想在我的测试用例中做,以便预先编写的测试用例不受影响?
你可以通过在class A中添加一个setter来解决这个问题。
应用程序上下文将被加载,A 的 B 对象将由 XML 中声明的 bean 注入,但您仍然可以通过调用新定义的 setter 来使用 B 的模拟覆盖它在你的测试中。
然后通过这样做,B 的 mock 将在您的测试中使用,而不是 bean。其他测试用例不受影响