当我开始在基于 Spring FW4 的 Java 应用程序中对一些控制器方法进行单元测试时,我遇到了一个复杂的问题。
我的 ApplicationConfig.java 用 @Configuration 和 @EnableTransactionManagement(proxyTargetClass = true) 和一个公共控制器方法进行了注释,我创建该方法是为了保存一个简单实体类的新对象,它是带有以下 ControllerTestClass 的 testet
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:/test-context.xml"})
@TransactionConfiguration(defaultRollback = true, transactionManager = "annotationDrivenTransactionManager")
public class TestController
@Autowired
public MyClassService myClassServiceMock;
protected MockMvc mockMvc;
@Autowired
protected WebApplicationContext webApplicationContext;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
};
@org.junit.Before
public void reset() {
Mockito.reset(myClassServiceMock);
}
@org.junit.After
public void after() {
verifyNoMoreInteractions(myClassServiceMock);
}
@Test
public void testSaveObject() throws Exception {
MyObject object = new MyObjectBuilder().withName("object").withDate("2014-08-15").build();
when(myClassServiceMock.createObject(objectName, objectDate)).thenReturn(object);
[.. mockMvcTest which works ... ]
verify(myclassServiceMock, times(1)).createObject(objectName, objectDate);
}
}
debug.log 的以下部分是我无法弄清楚问题的原因,但是当我删除 @EnableTransactionManager-Annotation 时,没有发生错误......
2014-08-15_17:25:59.608 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Creating new transaction with name [a.b.c.MyClassService$$EnhancerByMockitoWithCGLIB$$cf62a86c.saveObject]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2014-08-15_17:25:59.608 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@513f39c] for JPA transaction
2014-08-15_17:25:59.616 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@320cac01]
2014-08-15_17:25:59.618 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Initiating transaction commit
2014-08-15_17:25:59.618 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@513f39c]
2014-08-15_17:25:59.633 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@513f39c] after transaction
2014-08-15_17:25:59.633 [main] DEBUG o.s.o.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
2014-08-15_17:25:59.635 [main] DEBUG o.s.t.c.s.DirtiesContextTestExecutionListener - After test method: context [DefaultTestContext@8f72029 testClass = MyControllerTest, testInstance = a.b.c.MyControllerTest@453204e6, testMethod = testSaveObject@MyClassControllerTest, testException = org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at a.b.c.MyClassService$$FastClassBySpringCGLIB$$809f2bf.invoke(<generated>)
Example of correct verification:
verify(mock).doSomething()
我真的很感激一些帮助,因为我的古怪和研究已经进行了几天:(
谢谢