简体   繁体   English

是否可以使用Mockito和Powermock对具有setter和getter的方法进行单元测试

[英]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.

相关问题 如何为没有 getter 方法的 setter 方法编写单元测试? - How do I write unit test for a setter method which does not have a getter method? 如何使用 Mockito/Powermock 使用构造函数 arguments 测试 class 的方法 - How to test a method of a class with constructor arguments using Mockito/Powermock 用Mockito模拟接口的getter / setter方法 - Mocking getter / setter method of Interface with Mockito 如何在包含本地Set的方法中进行单元测试 <String> 变量?使用powerMock或Mockito - how to do unit test in a method that contains a local Set<String> variable?use powerMock or Mockito 使用Mockito进行插入方法的单元测试 - unit test for insertion method using mockito 使用PowerMock编写单元测试,模拟方法无法调用 - Using PowerMock to write unit test, mocked method fails to invoke 使用 PowerMock 在 android 中的单元测试 singleton class 方法 - Unit test singleton class method in android using PowerMock 如何使用 Powermock,easymock,mockito 对 DriverManager.getConnection(&quot;&quot;) 或 DriverManager.getConnection(&quot;&quot;,&quot;&quot;,&quot;&quot;) 进行单元测试 - How to unit test DriverManager.getConnection("") or DriverManager.getConnection("","","") get called using Powermock,easymock,mockito 我正在尝试使用 Junit、mockito 和 PowerMock 来创建单元测试 - I am attempting to use Junit, mockito and PowerMock to create a unit test 在 main 方法中使用 getter 和 setter - Using getter and setter in main method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM