PowerMockito模拟静态类INSIDE枚举?

问题描述 投票:1回答:1

我有一个枚举,必须有一个内部静态类用于bean注入。

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

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,只是静态的内部类。经过大量搜索,我尝试了各种方式。设置似乎是正确的,但它可能会错过一些技巧。有帮助吗?

java junit mockito powermock powermockito
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.