简体   繁体   English

将属性设置为模拟对象jmock

[英]Set property to mock object jmock

Is it possible to assing value to mock object. 是否可以将值赋给模拟对象。 Ex: 例如:

myMockObject = context.mock(MyObject.class);

myMockObject.setId("someId");

My method which I'm testing reaches the end, but at the end there is method for validation of that object so object without id is considered to be invalid. 我正在测试的方法到最后,但是最后有一种用于验证该对象的方法,因此没有id的对象被认为是无效的。 Is there anything else I can do about this? 我还有其他办法吗?

Can I somehow specify ok I'm expecting this exception but pass the test anyway? 我能以某种方式指定好吗?我期待这个异常,但无论如何通过测试?

I found this link but I'm unable to found solution : 我找到了此链接,但找不到解决方案:

http://www.jmock.org/yoga.html http://www.jmock.org/yoga.html

I'm expecting logger to throw an validation exception with message string, did anyone have experience with this before? 我期望记录器抛出带有消息字符串的验证异常,以前有人对此有经验吗?

I tried this : 我尝试了这个:

context.checking(new Expectations() {
            {

allowing(logger).error(with(exceptionMessage));

    }
        });

Note exceptionMessage message is thrown by the validation method which validates the object at the end of the method which I'm testing. 注意 exceptionMessage消息是由验证方法引发的,该方法在我正在测试的方法的末尾验证对象。

This was the answer I was looking for : 这就是我一直在寻找的答案:

http://www.jmock.org/throwing.html http://www.jmock.org/throwing.html

You need to add an Expectation that causes the mock method to return the value you expect: 您需要添加一个Expectation,以使模拟方法返回您期望的值:

allowing (myMockObject).getId(); will(returnValue("someId"));

This will cause getId to return the value you expect, and since it is using the allowing invocation count it won't cause the test to fail if it is not called. 这将导致getId返回您期望的值,并且由于它使用的是允许调用计数,因此如果不调用它,也不会导致测试失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM