PowerMockito无法模拟javax.faces.context.FacesContext

时间:2015-09-29 16:22:00

标签: java jsf mockito powermockito

如果我进行mvn clean安装,我会遇到构建错误,否则测试用例正常工作并返回预期结果。是否有任何mockito和powermockito版本问题?

java.lang.IllegalStateException: Failed to transform class with name javax.faces.context.FacesContext. Reason: javax.el.ELContext
    at javassist.ClassPool.get(ClassPool.java:450)
    at javassist.bytecode.Descriptor.toCtClass(Descriptor.java:592)
    at javassist.bytecode.Descriptor.getReturnType(Descriptor.java:489)
    at javassist.CtBehavior.getReturnType0(CtBehavior.java:306)
    at javassist.CtMethod.getReturnType(CtMethod.java:217)
    at org.powermock.core.transformers.impl.MainMockTransformer.modifyMethod(MainMockTransformer.java:172)
    at org.powermock.core.transformers.impl.MainMockTransformer.allowMockingOfStaticAndFinalAndNativeMethods(MainMockTransformer.java:142)
    at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:65)
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:243)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:177)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:95)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:107)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:31)
    at sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:370)
    at sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:351)
    at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:653)
    at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:460)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:286)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:222)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3070)
    at java.lang.Class.getAnnotations(Class.java:3050)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.classAnnotations(PowerMockJUnit44RunnerDelegateImpl.java:163)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.getDescription(PowerMockJUnit44RunnerDelegateImpl.java:155)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getDescription(JUnit4TestSuiteChunkerImpl.java:177)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.getDescription(AbstractCommonPowerMockRunner.java:47)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:51)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)

的pom.xml

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>1.10.8</version>
                <scope>test</scope>
            </dependency>
            <dependency>
               <groupId>org.powermock</groupId>
               <artifactId>powermock-core</artifactId>
               <version>1.5.5</version>
               <scope>test</scope>
           </dependency>
            <dependency>
               <groupId>org.powermock</groupId>
               <artifactId>powermock-module-junit4</artifactId>
               <version>1.5.5</version>
               <scope>test</scope>
           </dependency>
           <dependency>
               <groupId>org.powermock</groupId>
               <artifactId>powermock-api-mockito</artifactId>
               <version>1.5.5</version>
               <scope>test</scope>
           </dependency>

测试

@Before
public void setup() throws Exception {
    PowerMockito.mockStatic(Util.class);
    PowerMockito.mockStatic(Service.class);

    mockPageCodeBase = Mockito.mock(PageCodeBase.class);
    testWorkbenchDetails = new WorkbenchDetails();

    PowerMockito.mockStatic(FacesContext.class);
    PowerMockito.mockStatic(ExternalContext.class);

    facesContext = mock(FacesContext.class);
    extContext = mock(ExternalContext.class);

    when(FacesContext.getCurrentInstance()).thenReturn(facesContext);
    when(facesContext.getExternalContext()).thenReturn(extContext);

    facesMessage = Mockito.spy(new FacesMessage());
    PowerMockito.whenNew(FacesMessage.class).withAnyArguments().thenReturn(facesMessage);

    Iterator<FacesMessage> mockIterator = mock(Iterator.class);
    when(facesContext.getMessages()).thenReturn(mockIterator);

    sessionScope = mock(HashMap.class);
    sessionScope.put("awrId", "12345");
    sessionScope.put("userId", "wpsadmin");

    requestParameterMap = mock(HashMap.class);
    requestParameterMap.put("selectedApplicantId", "selectedApplicantId");
    testWorkbenchDetails.setApplicants(MockDataPotalClient.getApplicantWorkbenchDetailsList());

    BusinessInformation businessInformation = new BusinessInformation();
    businessInformation.setFax("fax");
    testWorkbenchDetails.setBusinessInformation(businessInformation);

    when(extContext.getSessionMap()).thenReturn(sessionScope);
    when(extContext.getRequestParameterMap()).thenReturn(requestParameterMap);
    when(requestParameterMap.get("selectedApplicantId")).thenReturn("selectedApplicantId");

    Mockito.when(Util.extractApplicant(any(String.class), any(List.class))).thenReturn(MockDataPotalClient.getApplicantWorkbenchDetails());

    suppress(field(WorkbenchDetails.class, "applicants"));

    PowerMockito.doNothing().when(Service.class, "initiateCCHCheck" ,any(WorkbenchDetails.class),any(ApplicantWorkbenchDetails.class));

    workbenchDetails = new WorkbenchDetails() {
        private static final long serialVersionUID = 1L;
    };

    applicant = new ApplicantWorkbenchDetails();
}

@Test
public void initiateCheckTest() throws Exception{
    String check = testWorkbenchDetails.initiateCheck();
    Assert.assertEquals(check, "SAME_PAGE");
}

2 个答案:

答案 0 :(得分:1)

您似乎没有在依赖项中定义的EL API,请尝试添加一个:

<dependency>
    <groupId>javax.el</groupId>
    <artifactId>el-api</artifactId>
    <version>2.2</version>
</dependency>

答案 1 :(得分:0)

您不必在pom.xml中定义mockito和powermockito依赖项。 PowerMockito内部提供兼容版本的mockito罐子 只保留在pom中的两个依赖项下面。

       <dependency>
           <groupId>org.powermock</groupId>
           <artifactId>powermock-module-junit4</artifactId>
           <version>1.5.5</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.powermock</groupId>
           <artifactId>powermock-api-mockito</artifactId>
           <version>1.5.5</version>
           <scope>test</scope>
       </dependency>