![](/img/trans.png)
[英]How to write unit test cases or mock for this type of method or is my custom method removeUsers not good enough for unit tests?
[英]How to write Unit Test cases for mule custom transformer
我们试图在Mule中为自定义转换器编写Junit测试用例。 但是我们无法在测试类中调用doTransform()方法。
后来我们意识到看到mule文档,Mule提供了单元测试用例的功能。 根据文档,我们扩展了AbstractTransformerTestCase
,它具有一些要实现的方法。
他们是 :
@Override
public Transformer getTransformer() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public Transformer getRoundTripTransformer() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getTestData() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getResultData() {
// TODO Auto-generated method stub
return null;
}
我们现在对以下事情感到困惑:
@Test(expected = RuntimeException.class)
但是在but子单元测试用例中如何做到这一点呢? AbstractTransformerTestCase
内部使用现有的“将被覆盖的方法”? 请帮助我们。 自2周以来我们什么都不知道。
要在Mule中测试变压器,请执行以下操作:
org.mule.transformer.AbstractTransformerTestCase
并实现抽象方法( 将Base64转换器的测试作为一个很好的例子 )。 这涵盖了变压器的基础知识。 org.mule.tck.junit4.FunctionalTestCase
创建功能测试用例,并创建标准的JUnit4 @Test
方法以与您将在测试配置中配置的变压器。 从测试开始,您最好采用测试驱动的开发工具之类的良好做法。 你会需要:
您将不需要:
在编写转换器测试时,您正在测试转换器的行为是否正常(好,它将一个对象转换为另一个对象)-因此,在测试用例中,您只需实例化转换器并使用一些输入命中转换方法,然后对结果执行断言。 如果您的转换器可以实例化而没有m子,并且在转换时不需要协作,则只需进行简单的单元测试即可。
如果您需要与Mule,Java EE或任何需要测试它们的子系统的协作,那么模拟就可以发挥作用。 您无需提供服务基础架构,而只是提供模拟并定义期望,您的类将如何与它们协作(您正在测试类,mulen或JDBC驱动程序)。 这是另一个怪异环境(android)的单元测试示例:
/**
* shall inject assignable views into class
* note that mocks are specifuied as parameters
*/
@Test
public void testSimpleInjection(@Mocked final WithInjectableViews injectable,
@Mocked final TextView textView,
@Mocked final Button button) {
// we expect that certain methods will be called on mocked objects
new Expectations() {
{
injectable.findViewById(239);
returns(textView);
injectable.findViewById(555);
returns(button);
}
};
// method under test
ViewInjector.startActivity(injectable);
// assertions
assertEquals(textView, Deencapsulation.getField(injectable, "asView"));
assertEquals(button, Deencapsulation.getField(injectable, "button"));
assertNull(Deencapsulation.getField(injectable, "notInjected"));
}
// class derived from android activity, base class is not instantiable
// in normal java environment, only on the phone or emulator but this is not
// practicable
class WithInjectableViews extends Activity {
// shall be injected
@InjectView(id = 239)
private android.view.View asView;
@InjectView(id = 555)
private Button button;
// shall be left alone
private View notInjected = null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.