Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking the UIInput class for tests

I am trying to mock javax.faces.component.UIInput class.

My class is as below

public class MyBean{

    private UIInput someInput;  

   //setters and getters

}

Test case

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

Can some one help me to fix this

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
like image 617
Patan Avatar asked Aug 17 '15 16:08

Patan


3 Answers

In the class UIComponent (which your test uses), there's a static line for initializing the Logger:

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

This means, the class is looking for a resource bundle named LogStrings within the package javax.faces. In the javax.faces-2.2.11.jar there's the directory javax and the subdirectory faces and within it the file LogStrings.properties. So your Class is also looking for this file.

Is the faces-jar within your classpath?

like image 114
Niklas P Avatar answered Oct 22 '22 06:10

Niklas P


If you don't want to bother with another dependency just because of testing using mock objects, you can simply put empty properties file into

testResourceDirectory/javax/faces/LogStrings.properties 

and mocking of UIComponent won't be problem anymore.

like image 3
bambula Avatar answered Oct 22 '22 06:10

bambula


As stated in Niklas P.'s answer, the cause of the problem is the Logger initialization by UIComponent.

To solve the problem jsf-api-xxx.jar must be included in your project. As explained here about using jsf-api-xxx.jar or javax.faces-xxx.jar (portability reasons):

Actually you can use both. But the recommended way is to include jsf-api-xxx.jar only in the compilation classpath.

In Maven you just need to add the following dependency to your 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>
like image 2
acm Avatar answered Oct 22 '22 08:10

acm