java - PowerMockito 模拟静态类 INSIDE 枚举?

标签 java junit mockito powermock powermockito

我有一个枚举,它必须有一个用于 Bean 注入(inject)的内部静态类。

我觉得我面临着模拟最困难的情况:枚举,静态类,静态字段,静态方法..

public enum Category{

    C1(Something(Constants.getFactory().createSomething(""))),
    C2(...);

    public static Constants {
        @Autowired
        private static Factory factory;

        public static Factory getFactory(){
            return factory;
        }
    }
}

我使用 PowerMockito 的测试类是:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Category.class,Category.Constants.class})
public class CategoryTests {

    @Before
    public void setUp() throws Exception {
        PowerMockito.mockStatic(Category.class);
        PowerMockito.mockStatic(Category.Constants.class);  

        //This simply testing mock didn't work             
        //PowerMockito.when(Category.Constants
        //                .getFactory()).thenReturn("123");


        //I tried to mock the inner field 'factory' and use it directly without a getter 
        //(with small changes in the original class)
        //But it didn't work either
        Factory factory = PowerMockito.mock(Factory.class);
        NewClass newClass = PowerMockito.mock(NewClass.class);
        PowerMockito.when(Factory.createSomething(anySring()))
                                         .thenReturn(newClass);

        Whitebox.setInternalState(
                 Category.Constants.class,"factory",Factory);


        //This is like the most common way to stub
        //It didn't work, so I believe the inner static class were never mocked
        PowerMockito.doReturn(factory).when(Category.Constants.class,
                                       "getFactory", anyString());
    }


    //I don't know if real test cases matter that much but I update to add it for reference.
    @Test(dataProvider = "Case1")
    public void testFromFilterType(final String testName, String input, final Category expected) {
        assertEquals(Category.doSomething(input), expected);
    }

    @DataProvider(name = "Case1")
    Object[][] fromFilterTypeCases() {
        return new Object[][] {
            { "C1", "input1", Category.C1 },
            { "C2", "input2", Category.C2 },
        };
    }
}
//Currently the tests got skipped because in class Category Constants.getFactory().createSomething(""), 
//where Constants.getFactory() returns null and mocking doesn't work.

起初我没有模拟 Enum,而只是模拟静态内部类。经过一番苦苦寻找,我尝试了各种方法。设置看起来是正确的,但可能会遗漏一些技巧。有什么帮助吗?

最佳答案

一点猜测:Category.class 是您要测试的类。该类本身不包含任何需要模拟/准备的内容。 所以:将这些部分放入您的代码中。即使它不会导致您当前的问题,我也很确定当您稍后开始测试时它可能会产生各种不良后果。

除此之外,真正的答案是首先避免使用 PowerMock(ito) 的必要性。您已经在使用 @Autowired,这意味着您正在使用 DI 框架。大多数 DI 框架也有用于单元测试的钩子(Hook)。因此,您应该尝试让 @Autowired 在您的测试设置中工作。

关于java - PowerMockito 模拟静态类 INSIDE 枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52320167/

相关文章:

java - 为什么 StringBufferInputStream 文档推荐使用 StringReader 进行 String 到 Stream 的转换?

java - 使用 JDBC 读取数据库

java - 会记住它的值的计数器

java - SonarQube - 显示考虑多个项目的代码覆盖率

java - stub 没有涵盖我在单元测试中的方法

java - Spring Security URL 模式不起作用

java - 通过测试套件中的@ExcludeCategory 忽略 junit 测试

java - 如何在 JUnit 和 Java 中使用 stub ?

spring - Spring MVC 中的模拟服务

java - 使用Mockito模拟spring的LocalContainerEntityManagerFactoryBean方法?