Я пытаюсь запустить тест Junit для проверки уровня сервиса.Я использую Spring 4, Hibernate 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>