public class MainClass {
public void userValidation() {
Utils.isNameExist();
};
}
public class Utils {
public static String isNameExist() {
Boolean isExist = false;
//get name value from UserEnum enum object
if(UserEnum.NAME.value().equal("Me")) {
isExist = true;
}
}
}
public enum UserEnum {
NAME("NM");
private String val;
private UserEnum (final String Value){
this.val = value;
}
public String value(){
return val;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({Utils.class})
public class MainClassTest {
MainClass mainClass;
@Before
public void setUp() throws Exception() {
mainClass = new MainClass();
}
@Test
public void testUserValidation() {
PowerMockito.mockStatic(Utils.class);
when(Utils.isNameExist()).thenReturn(true);
mainClass.userValidation();
}
}
以上代码在Eclipse中运行良好。我只是在尝试使用Ant构建时遇到错误。我观察到如果我删除了与Enum相关的代码,那么Ant构建工作正常。以某种方式Enum创造问题。任何人都可以帮助我。
Jar使用:
javassist-3.4.GA.jar
powermock-easymock-1.4.5-full.jar
powermock-mockito-1.4.6-full.jar
junit-4.4.jar
mockito-all-1.9.0.jar
Error:
[junit] Caused an ERROR
[junit] Failed to transform class with name com.Utils$UserEnum. Reason: null
[junit] java.lang.IllegalStateException: Failed to transform class with name com.Utils$UserEnum. Reason: null
[junit] at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:208)
[junit] at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:146)
[junit] at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:597)
[junit] at java.lang.Class.getDeclaredMethodsImpl(Native Method)
[junit] at java.lang.Class.getDeclaredMethods(Class.java:664)
[junit] at org.mockito.cglib.core.ReflectUtils.addAllMethods(ReflectUtils.java:349)
[junit] at org.mockito.cglib.proxy.Enhancer.getMethods(Enhancer.java:422)
[junit] at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:457)
[junit] at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
[junit] at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
[junit] at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
[junit] at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
[junit] at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:93)
[junit] at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:50)
[junit] at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:100)
[junit] at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:54)
[junit] at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70) org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:322)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:309)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:297)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:222)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:161)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:133)
[junit] at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:112)
[junit] at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
[junit] Caused by: java.lang.NullPointerException
[junit] at javassist.compiler.MemberResolver.lookupMethod(MemberResolver.java:110)
[junit] at javassist.compiler.MemberResolver.lookupMethod(MemberResolver.java:96)
[junit] at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:704)
[junit] at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:681)
[junit] at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:156)
[junit] at javassist.compiler.ast.CallExpr.accept(CallExpr.java:45)
[junit] at javassist.compiler.Javac$1.setReturnType(Javac.java:449)
[junit] at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:146)
[junit] at javassist.compiler.ast.CallExpr.accept(CallExpr.java:45)
[junit] at javassist.compiler.TypeChecker.atVariableAssign(TypeChecker.java:248)
[junit] at javassist.compiler.TypeChecker.atAssignExpr(TypeChecker.java:217)
[junit] at javassist.compiler.ast.AssignExpr.accept(AssignExpr.java:38)
[junit] at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:235)
[junit] at javassist.compiler.CodeGen.atStmnt(CodeGen.java:323)
[junit] at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
[junit] at javassist.compiler.CodeGen.atStmnt(CodeGen.java:344)
[junit] at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
[junit] at javassist.compiler.CodeGen.atIfStmnt(CodeGen.java:384)
[junit] at javassist.compiler.CodeGen.atStmnt(CodeGen.java:348)
[junit] at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
[junit] at javassist.compiler.CodeGen.atStmnt(CodeGen.java:344)
[junit] at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
[junit] at javassist.compiler.Javac.compileStmnt(Javac.java:558)
[junit] at javassist.expr.MethodCall.replace(MethodCall.java:233)
[junit] at org.powermock.core.transformers.impl.MainMockTransformer$PowerMockExpressionEditor.edit(MainMockTransformer.java:257)
[junit] at javassist.expr.ExprEditor.loopBody(ExprEditor.java:178)
[junit] at javassist.expr.ExprEditor.doit(ExprEditor.java:90)
[junit] at javassist.CtClassType.instrument(CtClassType.java:1224)
[junit] at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:70)
[junit] at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:204)
[junit]
BUILD FAILED