无法使用 MockedStatic 或 PowerMockito 验证单元测试中的静态方法
我真的厌倦了连续几天使用 MockedStatic
或 PowerMockito
尝试数十种不同的方法来正确测试静态方法。
我有以下服务方法和测试方法作为该服务方法的单元测试。当我只从测试方法调用服务方法时,我无法验证静态方法调用。另一方面,如果我在测试方法的try块中调用服务方法或静态方法,它似乎可以工作,但实际上却不能。因为它只是验证 try 块中的静态方法调用。如果我调用同一个静态方法两次,它会验证两次调用。但实际上,它只是一次服务中的静态方法。
那么,如何使用 MockedStatic
或 PowerMockito
验证静态方法调用?任何帮助将不胜感激。
服务:
// servis method
public CommandDTO create(final EmployeeRequest request) {
// code omitted
calculate(); // --> I call this method
return CommandDTO.builder().uuid(empoyee.getUuid()).build();
}
// private method that calls static method
private void calculate() {
// code omitted
LoggingUtils.error("Error...");
}
单元测试:
@Test
public void test() {
// code omitted
// call service method
employeeService.create(request);
try(MockedStatic<LoggingUtils> mocked = Mockito.mockStatic(LoggingUtils.class)) {
mocked.when(() -> LoggingUtils.error(any()))
.thenAnswer((Answer<Void>) invocation -> null);
/* ??? I am not sure if I need to call the service method for the 2nd time.
* But in any case, I should not call it from here again */
employeeService.create(request);
/* ??? in most examples, the sttaic method is called here. however, it is
* meaningless, because if I call 2 times and use times(2) in the following
* line, it works. but not called 2 times actually in the service method */
LoggingUtils.error(any());
// verify
mocked.verify(times(1), () -> LoggingUtils.error(any()));
}
}
I am really fed up with by trying tens of different approaches to test a static method properly using MockedStatic
or PowerMockito
for several days.
I have the following service methods and test method as a unit test for this service method. I cannot verify the static method call when I just call the service method from test method. on the other hand, if I call the service method or the static method in the try block of the test method, it seems to work, but actually it does not. because it just verify the static method call in the try block. if I call the same static method twice, it verify 2 times call. But in reality, it just ht one time to the static method in the service.
So, how can I verify the static method call using MockedStatic
or PowerMockito
? Any help would be appreciated.
service:
// servis method
public CommandDTO create(final EmployeeRequest request) {
// code omitted
calculate(); // --> I call this method
return CommandDTO.builder().uuid(empoyee.getUuid()).build();
}
// private method that calls static method
private void calculate() {
// code omitted
LoggingUtils.error("Error...");
}
unit test:
@Test
public void test() {
// code omitted
// call service method
employeeService.create(request);
try(MockedStatic<LoggingUtils> mocked = Mockito.mockStatic(LoggingUtils.class)) {
mocked.when(() -> LoggingUtils.error(any()))
.thenAnswer((Answer<Void>) invocation -> null);
/* ??? I am not sure if I need to call the service method for the 2nd time.
* But in any case, I should not call it from here again */
employeeService.create(request);
/* ??? in most examples, the sttaic method is called here. however, it is
* meaningless, because if I call 2 times and use times(2) in the following
* line, it works. but not called 2 times actually in the service method */
LoggingUtils.error(any());
// verify
mocked.verify(times(1), () -> LoggingUtils.error(any()));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的代码没问题,也许你将模拟与静态方法混淆了。由于测试之上的
employeeService.create(request);
,静态方法被命中一次。代码中验证两次的不是静态方法而是模拟方法,这两次调用发生在try..catch
块中。在调试时仔细检查堆栈跟踪应该可以确认。通过调试此代码:
您应该看到静态方法没有停止,并且测试应该正常结束,因为在
try..catch
中仅调用了一次模拟。I think your code is ok, maybe your are confusing mock with static method. Static method is hit once because of
employeeService.create(request);
on top of your test. What is verified twice in you code is not the static method but the mock, the two calls occur in thetry.. catch
block. Doublechecking the stacktrace while debugging should confirm.By debbugging this code:
you should see no stop on the static method and test should end fine because mock is called only once in the
try.. catch
.