本文介绍了每当使用PowerMock(EasyMock)调用程序包保护的静态方法时,都会引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论何时调用TimeZone.getDefaultRef()方法,我都试图抛出一个断言异常,以基本上表明该方法在测试期间从未被调用。问题是它的软件包受保护且是静态的,所以我认为我不得不使用PowerMock。这是我的尝试:

I'm trying to throw an assertion exception whenever the TimeZone.getDefaultRef() method is called, to basically show that this method is never called during the test. The problem is that it's package protected and static, so I think I'm forced to use PowerMock. Here's my attempt:

@RunWith(PowerMockRunner.class)
@PrepareForTest(TimeZone.class)
public class RandomTestingClass {
    @Before
    public void setup() {
        PowerMock.mockStaticPartialNice(TimeZone.class, "getDefaultRef")
        PowerMock.expectPrivate(TimeZone.class,
            TimeZone.class.getDeclaredMethod("getDefaultRef")).andStubThrow(new AssertionError());
        PowerMock.replay(TimeZone.class)
    }

    @Test
    public void randomTestThatShouldFailBecauseMethodCallsGetDefaultRefMethod() {
        Calendar.getInstance();
    }

    @Test
    public void randomTestThatShouldPassBecauseMethodDoesNotCallGetDefaultRefMethod() {
        Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    }

    @After
    public void after() {
        PowerMock.verify(TimeZone.class);
    {

我收到错误java.lang.IllegalStateException:没有最后一次调用一个可用的模拟,我之前肯定见过,但不确定如何在这种情况下修复。我也愿意以其他更优雅的方式来实现这一目标。总结一下:

I'm getting the error java.lang.IllegalStateException: no last call on a mock available, which I've definitely seen before but am not sure how to fix in this context. I'm also open to any other more elegant way to accomplish this. To sum up:


  1. 如果曾经调用过 Timezone.getDefaultRef(),测试应该会失败
  2. >
  3. 测试不应仅仅因为从未调用过该方法而失败(EasyMock期望使用该方法,但永远不会出现)

  4. 一个测试失败不应影响其他测试

  5. 有关TimeZone类的所有其他信息都应正常运行

  1. Test should fail if Timezone.getDefaultRef() is ever called
  2. Test shouldn't fail just because the method is never called (EasyMock is expecting the method but it never comes)
  3. One test failing shouldn't affect the other tests
  4. Everything else about the TimeZone class should operate normally


推荐答案

我正在尝试找到一种解决方法,但是快速的答案是该方法未被模拟。

I am trying to find a workaround but the quick answer is that the method is not mocked. The real method is called instead during the expectation.

PowerMock无法模拟由引导类加载器加载的类。 TimeZone 就是其中之一。

PowerMock can't mock classes loaded by the bootstrap class loader. TimeZone is one of those.

解决方案是通过调用它来模拟。在进行了说明。它说您需要准备调用系统类而不是系统类的类。

The solution is to mock with is calling it. It's explained here. It says you need to prepare the class calling the system class instead of the system class.

但是对于您而言,我不确定您可以。因为您想知道某个地方是否正在呼叫您的班级。因此,如果您要寻找的是 TimeZone ,则无法准备。除非您的呼叫者范围有限。

But in your case, I'm not sure you can. Because you want to know if something somewhere is calling your class. So you can't prepare whatever is using TimeZone if that is was you are looking for. Unless you have a limited scope of possible callers.

这篇关于每当使用PowerMock(EasyMock)调用程序包保护的静态方法时,都会引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 12:44
查看更多