2015-08-17 272 views
5

我想模擬javax.faces.component.UIInput類。嘲笑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 

回答

3

在類UIComponent(哪些測試使用),有一條用於初始化的靜態行在Logger

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

這意味着,該類尋找一個名爲包javax.facesLogStrings一個資源包。在javax.faces-2.2.11.jar有目錄javax和子目錄面臨並在其中的文件LogStrings.properties。所以你的班級也在尋找這個文件。

是你的classpath中的faces-jar嗎?

2

Niklas P.'s answer中所述,問題的原因是Logger初始化由UIComponent

要解決問題jsf-api-xxx.jar必須包含在您的項目中。至於有關使用jsf-api-xxx.jarjavax.faces-xxx.jar(移植原因)解釋here

其實你可以同時使用。但推薦的方法是僅在編譯類路徑中包含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> 
+1

無論出於何種原因,甲骨文在它的'javax:javaee-api:7.0' jar包中不包含_LogStrings_資源包(它被包含在源代碼jar中!dooh!)。這個工作的唯一方法就是包含這裏提到的'jsf-api-xxx.jar'。 –

0

我用這個額外的測試範圍的Maven依賴(pom.xml):

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