PowerMocking静态不返回预期目标(PowerMocking static does not

2019-07-18 13:19发布

我有一个问题嘲讽Calendar.getInstance() 正如你现在这个方法返回一个日历 - 我嘲讽的对象。

现在我的代码如下所示:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Calendar.class)
public class SurveillanceDatabaseTest {
    @Test
    public void testFailingDatabase() throws Exception {
        mockStatic(Calendar.class);
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.HOUR, 1);
        when(Calendar.getInstance()).thenReturn(calendar);
        final Surveillance surveillance = new Surveillance();
        surveillance.checkDatabase();
   }
}

Calendar.getInstance()获取称为不同的时间surveillance.checkDatabase()每一次它是一个新的对象,而不是日历的预期模拟。

任何人都可以看到我在做什么错?

Answer 1:

看来你需要添加目标测试类的PrepareForTest标签:
@PrepareForTest({Calendar.class,Surveillance.class})

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Calendar.class, Surveillance.class })
public class SurveillanceDatabaseTest {
    @Test
    public void testFailingDatabase() throws Exception {
        mockStatic(Calendar.class);
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.HOUR, 1);
        when(Calendar.getInstance()).thenReturn(calendar);
        final Surveillance surveillance = new Surveillance();
        surveillance.checkDatabase();
   }
}

即使上述汤姆Tresansky的例子都需要它,如果我们移动监控类MockCalendarTest类之外的某处。



Answer 2:

我不熟悉的when(object.call()).andReturn(response); 但我假设它的工作方式是相同的expect.(object.call()).andReturn(response); 。 如果是这样的话,那么它看起来像你缺少类的重播PowerMock.replay(Calendar.class)和你正在尝试做的,而不是一个局部静态模拟一个完整的静态模拟。 这应该解决您的问题。

@RunWith(PowerMockRunner.class)
@PrepareForTest(Calendar.class)
public class SurveillanceDatabaseTest {
    @Test
    public void testFailingDatabase() throws Exception {

        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.HOUR, 1);

        PowerMock.mockStaticPartial(Calendar.class, "getInstance");  //Mock Static Partial
        expect(Calendar.getInstance()).andReturn(calendar);
        PowerMock.replay(Calendar.class);  // note the replay of the Class!

        final Surveillance surveillance = new Surveillance();
        surveillance.checkDatabase();

        //Whatever tests you need to do here
   }
}


Answer 3:

好像你正在做的一切权利。 举例来说,本次测试下方通过,证明了Calendar由归国Calendar#getInstance()其实是在你设置了静态嘲讽的一个。

import static org.junit.Assert.*;
import static org.powermock.api.mockito.PowerMockito.*;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Calendar.class)
public class MockCalendarTest {
    @Test
    public void testFailingDatabase() {
        mockStatic(Calendar.class);

        final Calendar testCalendar = new GregorianCalendar();
        testCalendar.add(Calendar.HOUR, 1);
        when(Calendar.getInstance()).thenReturn(testCalendar);

        final Surveillance surveillance = new Surveillance();
        final Calendar resultCalendar = surveillance.checkDatabase();

        assertTrue(testCalendar == resultCalendar);
   }

    public static class Surveillance {
      public Calendar checkDatabase() {
        return Calendar.getInstance();
      }
    }
}

也许发布的相关部分Surveillance类,所以我们可以看到它是如何试图获得一个新的Calendar ,并分析一下,为什么它的失败。



文章来源: PowerMocking static does not return expected object