我有一个代码需要进行单元测试
@ManagedBean
@SessionScoped
public class XC implements Serializable {
public void applyX(){
FacesContext context = FacesContext.getCurrentInstance();
if (!context.isValidationFailed()) {
try {...
}
}
我写了一个测试代码来模拟上下文 . 完整的课程如下 .
package com.xyx.abc.admin.view.test;
import static org.mockito.Mockito.when;
import javax.faces.context.FacesContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.xyx.abc.admin.view.XsltConfig;
@RunWith(PowerMockRunner.class)
@PrepareForTest({FacesContext.class})
public class XsltConfigTest {
private XsltConfig xc;
@Mock
private FacesContext context;
@Before
public void setUp() throws Exception {
xc = new XsltConfig();
MockitoAnnotations.initMocks(this);
context = PowerMockito.mock(FacesContext.class);
when(context.isValidationFailed()).thenReturn(false);
// PowerMockito.mockStatic(FacesContext.class);
when(FacesContext.getCurrentInstance()).thenReturn(context);
}
/*
* @Before public void init() { MockitoAnnotations.initMocks(this); }
*/
@Test
public void testApplyXslt() {
String theXml = "<?xml version=\"1.0\"?>\r\n" + "<?xml-stylesheet type=\"text/xsl\" href=\"example.xsl\"?>\r\n" + "\r\n"
+ "
My Article\r\n" + " This is my article text.\r\n" + "";xc.setSampleXml(theXml);
String theXslt = "<?xml version=\"1.0\"?>\r\n" + "\r\n"
+ "\r\n" + " \r\n" + "\r\n" + " \r\n"
+ " Article - \r\n" + " Authors: \r\n"
+ " \r\n" + "\r\n" + " \r\n" + " - \r\n"
+ " \r\n" + "\r\n" + "";
xc.setSampleXslt(theXslt);
String theResult = "Article - My Article";
xc.setXsltResult(theResult);
xc.applyXslt();
// assertEquals(theResult, xc.getXsltResult());
}
}
我总是得到以下错误 . 它似乎是在课堂加载时间 .
java.lang.ClassFormatError:JVMCFRE074未指定Code属性; class = javax / faces / context / FacesContext,method =''()V,pc = 0 at java.lang.ClassLoader.defineClassImpl(Native Method)at java.lang.ClassLoader.defineClass(ClassLoader.java) :346)在java.net.URLClassLoader.defineClass(URLClassLoader.java:727)的java.security.ClassLoader.defineClass(SecureClassLoader.java:154)java.net.URLClassLoader.access $ 400(URLClassLoader.java:95)at java.net.URLClassLoader $ ClassFinder.run(URLClassLoader.java:1182)java.security.AccessController.doPrivileged(AccessController.java:686)at java.net.URLClassLoader.findClass(URLClassLoader.java:602)at java.lang java.lang.misc.Launcher的java.lang.ClassLoader.loadClass(ClassLoader.java:825)中的.ClassLoader.loadClassHelper(ClassLoader.java:846)$ java.lang.ClassLoader中的$ AppClassLoader.loadClass(Launcher.java:325) . loadClass(ClassLoader.java:805)at java.lang.Class.forNameImpl(Native Method)at java.lang.Class.forName(Class.java:348)at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectio) nFactory.java:126)sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:137)at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:61)at sun.reflect.annotation .AnnotationParser.parseSig(AnnotationParser.java:451)位于sun.reflect的sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:432)sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:736) . anclipse.AnnotationParser.parseArray(AnnotationParser.java:543)at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:367)at sun.reflect.annotationParser.parseAnnotation2(AnnotationParser.java:298)at sun.reflect .annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:132)位于java.lang的java.lang.Class.getAnnotationCache(Class.java:2539)的sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:84) . org.junit.runner.Descriptio中的Class.getAnnotations(Class.java:2229)在org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java)的org.junit.internal.runners.ErrorReportingRunner.getDescription(ErrorReportingRunner.java:25)中的n.createSuiteDescription(Description.java:123): 85)org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)at org位于org.eclipse.jdt的org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)的.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444) .internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)at or.e.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)