Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JMockit中预期和验证相同的方法_Java_Spring_Testing_Mocking_Jmockit - Fatal编程技术网

Java 如何在JMockit中预期和验证相同的方法

Java 如何在JMockit中预期和验证相同的方法,java,spring,testing,mocking,jmockit,Java,Spring,Testing,Mocking,Jmockit,具有以下类别: class ToTest{ @Autowired private Service service; public String make(){ //do some calcs obj.setParam(param); String inverted = service.execute(obj); return "<" + inverted.toString() + ">"

具有以下类别:

class ToTest{
    @Autowired
    private Service service;

    public String make(){
         //do some calcs
         obj.setParam(param);
         String inverted = service.execute(obj);
         return "<" + inverted.toString() + ">";
    }
}
我在obj.getParam()上得到一个空指针。显然,核查不起作用。如果我删除了期望值,它会工作,但在inversed.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());
         }};
    }
}
公共类诱惑
{
静态类CertainObject
{
私有字符串参数;
字符串getParam(){return param;}
void setParam(字符串p){param=p;}
}
公共接口服务{String execute(CertainObject o);}
公共静态类测试
{
私人服务;
公共字符串make()
{
CertainObject obj=新的CertainObject();
对象setParam(“a”);
字符串反转=service.execute(obj);
返回“”;
}
}
@测试测试测试;
@可注入服务;
@试验
公开作废临时文件()
{
新的非严格测量(){{
执行((CertainObject)任何);
结果=“b”;
}};
toTest.make();
新的核查(){{
确定对象对象;
执行(obj=withCapture());
assertEquals(“a”,obj.getParam());
}};
}
}

您能展示一个失败的完整示例测试吗?

我刚才使用的是一个旧版本的JMockit。此代码不再适用于JMockit 1.23版。我将得到以下异常:java.lang.IllegalStateException:已经记录了相同的期望;请删除此验证或调整录制。。。原因:多余的期望。。。定义期望和进行自定义验证的推荐方法是什么?在移除验证块的情况下,上述测试中可以使用四种不同的机制,都在期望记录块中:1)使用调用中传递的Hamcrest参数匹配器,并使用argthat(Hamcrest matcher);2) 在调用
和(委托)
时使用自定义匹配器;3) 使用带有捕获(列表)的
;4) 使用验证接收到的参数的
Delegate
对象分配
result
。其中哪一个最好取决于几个因素,包括个人偏好。当然,示例测试实际上并不需要这样做,因为它只是检查参数是
“a”
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());
         }};
    }
}