2

我试图遵循的例子:

@PrepareForTest(X.class)
public class XTest extends PowerMockTestCase {
        @Test
        public void test() {
                whenNew(MyClass.class).withNoArguments().thenThrow(new IOException("error message"));

                X x = new X();
                x.y(); // y is the method doing "new MyClass()"

                ..
        }
}

我试图单元测试的工厂类:

public final class LoadableBeanFactory implements ILoadableBeanFactory {
    private static final class Loader {
        private static final LoadableBeanFactory INSTANCE = new LoadableBeanFactory();
    }

    private LoadableBeanFactory() { }

    public static @Nonnull LoadableBeanFactory getInstance() {
        return Loader.INSTANCE;
    }

    public final @Nonnull <BeanT extends ILoadableBean> BeanT create(final Class<BeanT> beanClass) {

       final BeanT optionBean;

        try {

            final Constructor<BeanT> ctor = beanClass.getConstructor();
            optionBean = ctor.newInstance();
            return beanClass.cast(optionBean);
        } catch(Exception e) {
           throw new IllegalArgumentException("Could not instantiate an instance of " + beanClass);
        }
    }
}

我的测试如下。工厂不退回模型。我认为这是因为工厂是一个单例,它被实例化并加载了一个私有静态加载器类。那么,有没有办法模拟这个对象创建场景,还是我应该放弃将它变成真正的单元测试?

@PrepareForTest(LoadableBeanFactory.class)
@Test(groups = {"FactoryTestGroup", "LoadableBeanFactoryTestGroup"})
public class LoadableBeanFactoryTest extends PowerMockTestCase {

    @Mock LoadableBean mockBean;

    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void shouldCreateBean() {
        try {

                PowerMockito.whenNew(LoadableBean.class).withNoArguments().thenReturn(mockBean);
            LoadableBeanFactory.getInstance().create(LoadableBean.class);
        assertEquals(LoadableBeanFactory.getInstance().create(LoadableBean.class), mockBean,
                    "LoadableBeanFactory should have return mocked bean, but did not: " + mockBean);
        } catch(Exception e) {
            fail("Failed to mock bean creation");
        }
    }
}
4

1 回答 1

1

为什么你甚至想这样做?

如果您将工厂包装在一个抽象(一个单独的类)中,那么您可以通过构造函数注入它并模拟它的创建方法。

public class BeanFactory {

public <BeanT extends ILoadableBean> BeanT create(final Class<BeanT> beanClass) {
return LoadableBeanFactory.getInstance().create(beanClass);
    }

}

现在你想和你一起工作的班级

public class SomeClass {
private final BeanFactory beanFactory;

public SomeClass(BeanFactory beanFactory)  {
this.beanFactory= beanFactory;
}

public void doSth() {
beanFactory.create(...);
}

}

然后你根本不需要 PowerMock,你的设计真的很棒。

于 2015-09-12T09:35:41.623 回答