我正在尝试运行Junit测试来测试服务层。我正在使用Spring 4,休眠5.2.3和JUnit 4.12。我不知道为什么在运行测试代码时会遇到此异常。我尝试了所有可能的方法来执行代码,但是我无法运行测试用例。
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjecti...
...
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: missing behavior definition for the preceding method call:
LocalSessionFactoryBean.isSingleton()
Usage is: expect(a.foo()).andXXX()
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:127)....
...
... 25 more
Caused by: java.lang.IllegalStateException: missing behavior definition for the preceding method call:
LocalSessionFactoryBean.isSingleton()
Usage is: expect(a.foo()).andXXX()
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:42)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:97)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean$$EnhancerByCGLIB$$5f2c3f95.getObject(<generated>)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
... 38 more
测试类如下-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class TestClass{
@Test
public void testGetAge() {
int age = HelperClass.getAge(Utiity.getCurrentDate());
assertEquals(0, age);
}
}
applicationcontext.xml
> > <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:p="http://www.springframework.org/schema/p"
> xmlns:tx="http://www.springframework.org/schema/tx"
> xmlns:context="http://www.springframework.org/schema/context"
> xsi:schemaLocation=" http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans.xsd
> http://www.springframework.org/schema/tx
> http://www.springframework.org/schema/tx/spring-tx.xsd
> http://www.springframework.org/schema/context
> http://www.springframework.org/schema/context/spring-context.xsd">
>
> <context:property-placeholder location="/spring.properties" />
> <bean id="sessionFactory" class="org.easymock.EasyMock" factory-method="createMock"> <constructor-arg index="0"
> value="org.springframework.orm.hibernate5.LocalSessionFactoryBean" />
> </bean> <bean id="dataSource" class="org.easymock.EasyMock"
> factory-method="createMock"> <constructor-arg index="0"
> value="org.springframework.jdbc.datasource.DriverManagerDataSource" />
> </bean>