[英]Is it possible to unit test a method which have setter and getter using mockito and powermock
I am writing unit test for a legacy code which is basically setting and getting value in the method using chain stub, the class I am testing does not have dependency on other classes so I do not have mock anything but have a method like this 我正在为遗留代码编写单元测试,该代码基本上是使用链存根在该方法中设置并获得价值的,我正在测试的类不依赖于其他类,因此我没有模拟任何东西,但是有一个像这样的方法
public class SampleClass {
public void process(Context context){
context.getState().setValue(MathUtil.roundToThree(context.getState().getValue());
}
}
The test class I have written is as follows: 我编写的测试类如下:
@RunWith(MockitoJUnitRunner.class)
public class TestSampleClass{
SampleClass sample = new SampleClass();
@Test
public void testProcess(){
Context context = Mockito.mock(Context.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(context.getState().getValue()).thenReturn(4.12345);
sample.process(context);
assertEquals(4.123,context.getState().getValue(),0.0);
}
}
Test case is failing because it is returing 4.12345 due to Mockito.when() but I am not sure how do I test the value in context
object after calling the process()
method. 测试用例失败,因为由于Mockito.when()而重新播放4.12345,但我不确定在调用process()
方法后如何测试context
对象中的值。
I can not change the source code. 我无法更改源代码。 Intializing Context is very difficult because it depends on so many other Classes. 初始化上下文非常困难,因为它依赖于许多其他类。 Please help. 请帮忙。
The problem is that your stub State
object doesn't know that the value returned by getValue()
should be the last value that was passed to setValue()
. 问题是您的存根State
对象不知道getValue()
返回的值应该是传递给setValue()
的最后一个值。
The stub created by Mockito doesn't automatically pick up that you have a getter and setter and the getter should return what the setter was last called with. 由Mockito创建的存根不会自动识别您具有getter和setter,并且getter应该返回上次调用setter的内容。 It's just a dumb stub that only does what you've told it to do, and you haven't told it to do anything when setValue()
is called. 这只是一个愚蠢的存根,只会执行您已告诉它的操作,并且在调用setValue()
时您并未告诉它执行任何操作。
It may be simplest to verify that setValue
was called with the rounded value, rather than attempt to fetch it from getValue()
: 验证用舍入后的值调用setValue
可能是最简单的方法,而不是尝试从getValue()
获取它:
Mockito.verify(context.getState()).setValue(AdditionalMatchers.eq(4.123, 0.0));
Another approach is to create a test State
class that has real getters and setters, and use that in your tests. 另一种方法是创建一个具有真实的getter和setter的测试State
类,并在测试中使用它。 Of course, whether or not you can do this depends on how many other methods this class has. 当然,是否可以执行此操作取决于该类具有多少其他方法。 If you were to write such a class (I named it TestState
in the code below), the test code would look like the following: 如果要编写这样的类(我在下面的代码TestState
其命名为TestState
),则测试代码将如下所示:
@Test
public void testProcess2(){
Context context = Mockito.mock(Context.class);
Mockito.when(context.getState()).thenReturn(new TestState());
context.getState().setValue(4.12345);
sample.process(context);
assertEquals(4.123,context.getState().getValue(),0.0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.