如何模拟静态最终 class

How to mock static final class

我有以下 classes,其中第二个 class 是第一个的静态最终字段:

public class BaseItemScan  {


    protected String initItem(){
       ...
    }

}

public class ItemScan extends BaseItemScan {

    private static final ItemFactory if = ItemFactory.getInstance();
    public void handleItem(){
       super.initItem();
       ...
    }

}

工厂 class 包含一个构造函数和一个 getInstance 方法:

public class ItemFactory {
    private static ItemFactory INSTANCE = null;

    public static ItemFactory getInstance() {
        if (INSTANCE == null) {
            throw new IllegalStateException("ItemFactory not initialized!");
        } else {
            return INSTANCE;
        }
    }

    public ItemFactory () {
        if (INSTANCE != null) {
            throw new IllegalStateException("ItemFactory already initialized!");
        } else {
            INSTANCE = this;
        }
    }
}

如何测试handleItem?对于以下代码:

public class ItemScanTest {

    @InjectMocks
    ItemScan itemScan= new ItemScan();

    @Rule
    public MockitoRule rule = MockitoJUnit.rule();

    @Test
    void testHandleItem() {
        itemScan.handleItem();
    }

}

错误信息是:

Caused by: java.lang.IllegalStateException: ItemFactory not initialized!
    at ItemFactory.getInstance(ItemFactory.java:)
    at ItemScan.<clinit>(ItemScan.java:)

这是执行此操作的示例。我只是将 sayHello 方法添加到 ItemFactory class 以验证调用次数。添加默认构造函数只是因为 PowerMock 抱怨没有零参数构造函数。

@RunWith(PowerMockRunner.class)
@PrepareForTest(ItemFactory.class)
class ItemScanTest {

    public ItemScanTest() {}

    private ItemScan itemScan;

    @Mock
    private ItemFactory itemFactory;

    @Before
    public void init(){
        MockitoAnnotations.initMocks(this);
        PowerMockito.mockStatic(ItemFactory.class);
        Mockito.when(ItemFactory.getInstance()).thenReturn(itemFactory);
        itemScan = new ItemScan();
    }

    @Test
    public void handleItemTest() {
        Mockito.when(itemFactory.sayHello()).thenReturn("HelloHello");
        itemScan.handleItem();
        Mockito.verify(itemFactory, times(1)).sayHello();
    }
}