Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Easy mock无法识别模拟服务_Java_Spring_Unit Testing_Junit_Easymock - Fatal编程技术网

Java Easy mock无法识别模拟服务

Java Easy mock无法识别模拟服务,java,spring,unit-testing,junit,easymock,Java,Spring,Unit Testing,Junit,Easymock,我在测试我的一项服务时遇到了这个奇怪的问题。。。 我正在用easymock 3.0模拟两个服务,并用Spring注入它们,但我在其中一个服务上得到了“java.lang.IllegalArgumentException:不是模拟:$Proxy43”异常。我在配置文件中以相同的方式声明它们,如下所示: <bean id="recurringSchedulesJobsService" class="org.easymock.EasyMock" factory-method="createMoc

我在测试我的一项服务时遇到了这个奇怪的问题。。。 我正在用easymock 3.0模拟两个服务,并用Spring注入它们,但我在其中一个服务上得到了“java.lang.IllegalArgumentException:不是模拟:$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>
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是围绕模拟的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);
}

看起来jobPeriodService是由Spring自动代理的,而RecurringScheduleSJobService不是。这很可能是因为Spring已经将recurringSchedulesJobService标记为不符合自动代理的条件(没有潜在的切入点匹配,它在某个地方被显式关闭,等等)。如果不查看其他配置,我真的不知道具体原因是什么


如果您打开Spring的日志级别进行调试,它会告诉您为什么recurringSchedulesJobsService不符合自动代理的条件。

recurringSchedulesJobsService和jobPeriodService接口都是吗?感谢您的快速回答。我使用Spring是为了不必注入jobPeriodService的所有依赖项。我尝试过编程方法,但我必须处理jobPeriodService中通常自动连接的所有其他服务。不管怎样,为什么Spring在我的第二次模拟中只是这样?为什么第一个有效?jobPeriodService是一个模拟。在模拟中没有可注入的内容!?当我尝试使用它时,我得到了NPE,因为一个空EntityManager可能意味着该服务是一个类(而不是一个接口),并且您没有模拟被测服务调用的方法。单元测试应该只测试一个单元。它的所有依赖项都应该被模仿。是的,它是一个类。引用是一个接口。这个测试确实涵盖了很多逻辑。这就是为什么我要尽可能多地嘲弄别人。不过,我和JB Nizet在一起。你把事情弄得太复杂了。
@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);
}