简体   繁体   English

不兼容的返回值类型

[英]incompatible return value type

I am new to unit testing, Recently I used profiler to populate different metrics depending on different conditions using if-else statements. 我是单元测试的新手,最近我使用事件探查器根据不同的条件使用if-else语句填充不同的指标。 I have written a unit test to check if the right metrics is getting populated. 我编写了一个单元测试,以检查是否填充了正确的指标。 However I m getting a Incompatible return value type error. 但是,我收到了Incompatible return value type错误。 The code snippet is as follows: 代码片段如下:

ProfilerObject1 mockObject = ctrl.createMock(ProfilerObject1.class);
EasyMock.expect(ProfilerObject2.func1()).andReturn(mockObject);
PopulateMetric(m1);
EasyMock.expectLastCall().once();
control.replay();

I m getting the incompatible return value error on line 2(EasyMock.expect call), whereas the function returns the object of type ProfilerObject1. 我在第2行(EasyMock.expect调用)上收到了不兼容的返回值错误,而该函数返回了ProfilerObject1类型的对象。 I have no clue on why I m getting this error. 我不知道为什么会收到此错误。

Any help would really be appreciated. 任何帮助将不胜感激。 TIA TIA

PS: I m using this snippet at 2 separate places and at the 2nd place it gives no last call on mock available error. PS:我在2个不同的地方使用此代码段,在2个地方没有给出no last call on mock available错误的no last call on mock available Which confuses me even more. 这让我更加困惑。

You are setting the expectation on a static method call. 您正在对静态方法调用设置期望。 It is not supported by EasyMock. EasyMock不支持它。 To mock static methods you should use PowerMock along with EasyMock. 要模拟静态方法,应该将PowerMock与EasyMock一起使用。 Check below sample on how to do. 请检查以下示例以了解如何操作。

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ProfilerObject2.class })
public class TestClass {

   @Test
   public void test() {

       PowerMock.mockStatic(ProfilerObject2.class);
       ProfilerObject1 mockObject = ctrl.createMock(ProfilerObject1.class);
       EasyMock.expect(ProfilerObject2.func1()).andReturn(mockObject);
       // your rest of the code goes here
       PowerMock.replayAll();

   }

}

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

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