1

我正在使用 Spring 3.1.1.RELEASE 和 JUnit 4.8.1。在我的测试课上,我想模拟一个私有字段并发现“ReflectionTestUtils”的美妙之处......

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class OrderServiceTest extends AbstractTransactionalJUnit4SpringContextTests
    …
        @Autowired
    private OrderService m_orderSvc;

    @Test
    public void testGetPDOrders() throws QuickBaseException, Exception {
        …
            ReflectionTestUtils.setField(m_orderSvc, "m_orderDao", mockOrderDao);

下面是我试图模拟的类和领域......

@Service("orderService")
@Transactional
public class OrderServiceImpl implements OrderService {

    …
    @Autowired
    private OrderDAO m_orderDao;

令人失望的是,我收到以下错误。我读过这是因为我的类被标记为“@Transactional”,但我找不到足够的解决方法,而且仅仅为了适应 JUnit 而编写 setter 方法似乎是一种浪费。有人对我如何将模拟对象注入私有字段有其他建议吗?

java.lang.IllegalArgumentException: Could not find field [m_orderDao] on target [org.mainco.subco.myclient.service.OrderServiceImpl@282f0e07]
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:107)
    at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:84)
    at org.mainco.subco.myclient.service.OrderServiceTest.testGetPDOrders(OrderServiceTest.java:130)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    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.InvokeMethod.evaluate(InvokeMethod.java:20)
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

2 回答 2

2

我不确定您使用的是什么模拟库,但是 Spring 和 Mockito 之间有一个有用的集成,恰当而笨拙地命名为“ Springockito ”,它可以在更大的 Spring 上下文中非常容易地进行模拟的战术插入。

这个想法是您更改测试应用程序上下文以将该 bean 映射到模拟,而不是尝试在运行时将模拟连接到您的父 bean。

所以实际上你最终会得到:

文本上下文.xml

<beans xmlns="http://www.springframework.org/schema/beans"
      ...
      xmlns:mockito="http://www.mockito.org/spring/mockito"
      xsi:schemaLocation="... http://www.mockito.org/spring/mockito https://bitbucket.org/kubek2k/springockito/raw/tip/springockito/src/main/resources/spring/mockito.xsd">

    <mockito:mock id="m_orderDao" class="my.package.OrderDao"/>

    <!--... other config ...-->
 </beans>

然后,如果您需要与其交互,您可以将模拟本身自动连接到您的测试中,就像您现在为您的服务所做的那样。

如果您使用 Mockito,您仍然可以使用上述方法,而是使用您的模拟库的方法来创建模拟作为 bean 的工厂。

于 2012-12-13T23:50:13.220 回答
1

截至 2014 年,最简单的解决方案是使用 @InjectMocks 注释,它是 Mockito 的一部分。这适用于任何类,包括标有@Transactional 的类。

这是一个例子:

public class TestTestController {

    @Mock
    private TestService testService;

    @InjectMocks
    private TestController testController;

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testMocks() throws Exception {
        String mockedReturnValue = "this is a mocked reply";
        when(testService.getMessage()).thenReturn(mockedReturnValue);

        assertEquals(mockedReturnValue, testController.callTestService());

    }
}

和相关的类

public class TestController {

    @Autowired
    private TestService testService;

    public String callTestService() {
        return testService.getMessage();
    }

}

public class TestService {

    public static final String THIS_IS_A_TEST = "this is a getMessage";

    public String getMessage() {
        return THIS_IS_A_TEST;
    }

}
于 2014-09-16T08:23:52.160 回答