我需要为我的测试模拟java.lang.reflect.Method(我有动态的东西)。 我们使用Junit spring& amp;编写了所有测试用例。的Mockito。但是我在为我的方面写Junit时遇到了问题。 我们还编写了自定义注释 请帮帮我
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/services-test.xml")
@ImportAutoConfiguration(RefreshAutoConfiguration.class)
public abstract class AbsServicesTest extends AbstractJUnit4SpringContextTests {
static{
System.setProperty("LOCAL");
}
}
public class TaxValidatorAspectTest extends AbsServicesTest {
@Autowired
private ResourcesUtils resourcesUtil;
@Mock
private JoinPoint joinPoint;
@Mock
private MethodSignature methodSignature;
@Mock
private Method method;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(new TaxValidatorAspectTest());
Mockito.when(joinPoint.getSignature()).thenReturn(methodSignature);
Mockito.when(methodSignature.getMethod()).thenReturn(method);
}
@Test
public void testBeforeMethod() throws IOException{
TaxValidatorAspect taxonomyValidationAspect = new TaxValidatorAspect();
final Object[] arguments = new Object[4];
arguments[0] = requiredParam1;
arguments[1] = requiredParam2;
arguments[2] = requiredParam3;
Mockito.when(joinPoint.getArgs()).thenReturn(arguments);
taxonomyValidationAspect.before(joinPoint);
}
}
我收到如下错误
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.lang.reflect.Method
Mockito cannot mock/spy because :
- final or anonymous class
at com.taxvalidation.TaxValidatorAspectTest.setUp(TaxValidatorAspectTest.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
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)
答案 0 :(得分:4)
您可以使用以下行来模拟java.lang.reflect.Method
Method mockMethod = this.getClass()。getMethod(methodName)
不允许直接使用模拟或间谍为java.lang.reflect.Method创建模拟对象!