Jor*_*.S. 5 java testing spring jmockit mocking
有以下课程:
class ToTest{
@Autowired
private Service service;
public String make(){
//do some calcs
obj.setParam(param);
String inverted = service.execute(obj);
return "<" + inverted.toString() + ">";
}
}
Run Code Online (Sandbox Code Playgroud)
我想添加一个测试,断言我用一个带有param X的对象调用了service.execute.
我会通过验证来做到这一点.我想模仿这个调用,让它返回一些可测试的东西.我这样做是出于期望.
@Tested
ToTest toTest;
@Injected
Service service;
new NonStrictExpectations(){
{
service.exceute((CertainObject)any)
result = "b";
}
};
toTest.make();
new Verifications(){
{
CertainObject obj;
service.exceute(obj = withCapture())
assertEquals("a",obj.getParam());
}
};
Run Code Online (Sandbox Code Playgroud)
我在obj.getParam()上得到一个空指针.显然验证不起作用.如果我删除了它的预期,但我在invert.toString()中得到一个空指针.
你们怎么做这个工作?
以下测试类对我来说工作正常,使用 JMockit 1.4:
public class TempTest
{
static class CertainObject
{
private String param;
String getParam() { return param; }
void setParam(String p) { param = p; }
}
public interface Service { String execute(CertainObject o); }
public static class ToTest
{
private Service service;
public String make()
{
CertainObject obj = new CertainObject();
obj.setParam("a");
String inverted = service.execute(obj);
return "<" + inverted + ">";
}
}
@Tested ToTest toTest;
@Injectable Service service;
@Test
public void temp()
{
new NonStrictExpectations() {{
service.execute((CertainObject) any);
result = "b";
}};
toTest.make();
new Verifications() {{
CertainObject obj;
service.execute(obj = withCapture());
assertEquals("a", obj.getParam());
}};
}
}
Run Code Online (Sandbox Code Playgroud)
你能展示一个失败的完整示例测试吗?
| 归档时间: |
|
| 查看次数: |
4276 次 |
| 最近记录: |