模拟UIInput类以进行测试

时间:2015-08-17 16:47:21

标签: java unit-testing jsf mockito

我正在尝试模拟javax.faces.component.UIInput类。

我的课程如下

public class MyBean{

    private UIInput someInput;  

   //setters and getters

}

测试用例

UIInput mockedVale = Mockito.mock(UIInput.class);
MyBean myBean = new MyBean();
myBean.setSomeInput(mockedVale);

有人可以帮我修复这个

java.lang.ExceptionInInitializerError
    at sun.reflect.GeneratedSerializationConstructorAccessor3.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:45)
    at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
    at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:142)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:61)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:52)
    at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:24)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:32)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
    at org.mockito.Mockito.mock(Mockito.java:1258)
    at org.mockito.Mockito.mock(Mockito.java:1135)
    at se.telenor.ocfd.web.facade.MyBean.<init>(MyBean.java:28)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.util.MissingResourceException: Can't find javax.faces.LogStrings bundle
    at java.util.logging.Logger.setupResourceInfo(Logger.java:1945)
    at java.util.logging.Logger.<init>(Logger.java:380)
    at java.util.logging.LogManager.demandLogger(LogManager.java:554)
    at java.util.logging.Logger.demandLogger(Logger.java:455)
    at java.util.logging.Logger.getLogger(Logger.java:553)
    at javax.faces.component.UIComponent.<clinit>(UIComponent.java:116)
    ... 35 more

4 个答案:

答案 0 :(得分:4)

在课程UIComponent(您的考试使用的课程)中,有一条用于初始化Logger的静态行:

private static Logger LOGGER = Logger.getLogger("javax.faces.component",
    "javax.faces.LogStrings");

这意味着,该类正在包 javax.faces 中查找名为 LogStrings 资源包。在javax.faces-2.2.11.jar,目录 javax 和子目录面对,其中包含文件 LogStrings.properties 。所以你的班级也在寻找这个文件。

你的类路径中是faces-jar吗?

答案 1 :(得分:2)

正如Niklas P.'s answer中所述,问题的原因是Logger初始化UIComponent

要解决问题jsf-api-xxx.jar必须包含在您的项目中。正如here所述,关于使用jsf-api-xxx.jarjavax.faces-xxx.jar(可移植性原因):

  

实际上你可以同时使用两者。但推荐的方法是仅在编译类路径中包含jsf-api-xxx.jar

在Maven中,您只需要将以下依赖项添加到pom.xml

<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.2.8-18</version><!-- the version you are using -->
    <scope>provided</scope>
</dependency>

答案 2 :(得分:0)

我使用了这个额外的测试范围的Maven依赖项(pom.xml):

    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <version>2.2</version>
        <scope>test</scope>
    </dependency>

答案 3 :(得分:0)

如果你不想仅仅因为使用模拟对象进行测试而烦扰另一个依赖项,你可以简单地将空属性文件放入

testResourceDirectory/javax/faces/LogStrings.properties 

并且UIComponent的模拟将不再是问题。