简体   繁体   English

使用 PowerMock 在静态块中模拟 getResource

[英]Mocking getResource in static block with PowerMock

How to mock getResourceAsStream in the static block ?如何在静态块中模拟 getResourceAsStream ? I think it is untestable.我认为这是不可测试的。

I reviewed SO and cannot find the answer.我查看了 SO 并找不到答案。 The closes-SO-post-here does not address the issue as the call to getResourceAsAStream in the post is not from a static block . closes-SO-post-here没有解决这个问题,因为在 post 中对 getResourceAsAStream 的调用不是来自静态块

I tried PowerMock, and run into number of limitations.我尝试了 PowerMock,但遇到了许多限制。 First if I want to mock SomeProperties.class.getResourceAsStream - the static block will execute, as I will need to refer to the class itself.首先,如果我想模拟SomeProperties.class.getResourceAsStream - 静态块将执行,因为我需要引用类本身。 I can suppress static block to prevent doing so, but this will prevent me from getting the static block to execute at all.我可以抑制静态块以防止这样做,但这将阻止我完全执行静态块。 The solution would be to postpone the execution of the static block until after someProperties.class.getResourceAsStream is mocked.解决方案是将静态块的执行推迟到 someProperties.class.getResourceAsStream 被模拟之后。 I do not think it is possible though.我不认为这是可能的。 It seems that this code is purely untestable;看来这段代码纯粹是不可测试的; Any other ideas?还有其他想法吗?

Here is the code [and a link to GITHUB] :这是代码[和到 GITHUB 的链接]

package com.sopowermock1;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class SomeProperties {

    private static Properties props = new Properties();

    static {
        InputStream is = SomeProperties.class.getResourceAsStream("/some.properties");

        try {
            props.load(is);
            System.out.println("Properties.props.keySet() = " + props.keySet());            
        } catch (IOException e) {
            // How test this branch???
            System.out.println("Yes. We got here.");
            throw new RuntimeException(e);
        }
    }

    private SomeProperties() {}; // to makes life even harder...

    public static String getVersion() {
        return props.getProperty("version");
    }
}

And here is the test GITHUB Link这是测试GITHUB 链接

package com.sopowermock1;
import java.io.InputStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.sopowermock1.SomeProperties;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SomeProperties.class)

// This will prevent running static block completely:
// @SuppressStaticInitializationFor("com.sopowermock1.SomeProperties")
public class SomePropertiesTest {

    @Mock
    private static InputStream streamMock;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(SomeProperties.class);
        System.out.println("test setUp");
    }

    @Test(expected = RuntimeException.class)
    public void testStaticBlock() {

        PowerMockito.mockStatic(SomeProperties.class); // this will mock all static methods (unwanted as we want to call getVersion)

        // This will cause static block to be called.
        PowerMockito.when(SomeProperties.class.getResourceAsStream("/some.properties")).thenReturn(streamMock);
        SomeProperties.getVersion();
    }
}

Any ideas?.有任何想法吗?。 Full GITHUB source is here .完整的 GITHUB 源代码在这里

as mention in How to mock getResourceAsStream method using PowerMockito and JUnit?如何使用 PowerMockito 和 JUnit 模拟 getResourceAsStream 方法中所述? , use Extract Delegate , then mock the delegate class such as XXStreamFetcher , and then you can test it. ,使用Extract Delegate ,然后模拟XXStreamFetcher等委托类,然后就可以测试了。

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

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