java – Easy mock无法识别模拟服务

我在测试我的一项服务时遇到了这个奇怪的问题……
我正在用easymock 3.0模拟两个服务并用Spring注入它们,但我得到一个“java.lang.IllegalArgumentException:不是mock:$Proxy43”异常.我在配置文件中以相同的方式声明它们,如下所示:

<bean id="recurringSchedulesJobsService" class="org.easymock.EasyMock" factory-method="createMock">
    <constructor-arg value="com.spmsoftware.recurringschedules.service.RecurringSchedulesJobsService"/>
</bean>

<bean id="jobPeriodService" class="org.easymock.EasyMock" factory-method="createMock">
    <constructor-arg value="com.spmsoftware.jobdefinition.service.JobPeriodService"/>
</bean>

在我的junit测试中,我按照以下方式使用它们:

@Autowired
private RecurringSchedulesJobsService recurringSchedulesJobsService;
@Autowired
private JobPeriodService jobPeriodService;

@Before
public void setUp() throws Exception {
    reset(recurringSchedulesJobsService);
    expect(recurringSchedulesJobsService.getBasedOnRecurringScheduleId(RECURRING_SCHEDULE_ID)).andReturn(buildRecurringScheduleJob());
    replay(recurringSchedulesJobsService);

    reset(jobPeriodService);
    expect(jobPeriodService.findPeriodByJobId(RECURRING_SCHEDULE_JOB_ID)).andReturn(buildJobDefinitionPeriod());
    replay(jobPeriodService);
}

recurringSchedulesJobsService被模拟,当我评论第二个服务时,它的行为与预期一致.另一方面,jobPeriodService不被识别为模拟.相反,我得到这个例外:

java.lang.IllegalArgumentException: Not a mock: $Proxy43
at org.easymock.internal.ClassExtensionHelper.getControl(ClassExtensionHelper.java:66)
at org.easymock.EasyMock.getControl(EasyMock.java:2068)
at org.easymock.EasyMock.reset(EasyMock.java:1983)
at com.spmsoftware.recurringschedules.occurrences.generator.OccurrenceGeneratorTest.setUp(OccurrenceGeneratorTest.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)

抛出异常只是reset()方法,但是……

我发现一个有趣的事情是两个对象不是同一个实例.这是我在调试时得到的:

对此有任何想法都是非常有价值的.
谢谢

解决方法:

Spring将bean包装到代理中,可能是为了在方法周围应用AOP方面(事务性,安全性).因此,它在测试中注入的bean是围绕mock的Spring代理,而不是mock本身.

你为什么要使用spring上下文和依赖注入?您可以在单元测试中简单地实例化服务对象,在对象中注入模拟依赖项并对其进行测试.不需要Spring容器.这可能是IoC框架的主要有趣特性:它使单元测试变得简单:

@Before
public void setUp() {
    this.recurringSchedulesJobsService = mock(RecurringSchedulesJobsService.class);
    this.jobPeriodService = mock(JobPeriodService.class);
}

@Test
public void testSomeMethod() {
    expect(recurringSchedulesJobsService.doThis()).andReturn(that);
    expect(jobPeriodService.doThat()).andReturn(1);

    replay(recurringSchedulesJobsService, jobPeriodService);

    MyServiceImplementation serviceToTest = 
        new MyServiceImplementation(recurringSchedulesJobsService, jobPeriodService);
    serviceToTest.someMethod();
    verify(recurringSchedulesJobsService, jobPeriodService);
}
上一篇:java – 是否可以在现有对象中模拟单个方法?


下一篇:java – 使用EasyMock测试param值