Java – spring data JPA cannot be configured: the specified class is an interface
I have started using spring data JPA Unfortunately, I can't configure it I have entity class and repository interface, but when I try to test it, something goes wrong
Source code (I have a setter / getter method, but I skip it for better code):
@Entity public class Employee { @Id @GeneratedValue private Long id; private String firstName; private String lastName; public Employee() { } }
Repository class:
public interface EmployeeRepository extends JpaRepository<Employee,Long> { }
Configuration:
@Configuration @ComponentScan(basePackages = "com.szymon.repository") @EnableJpaRepositories(basePackages = "com.szymon.repository") public class AppConfig { @Bean public LocalEntityManagerfactorybean entityManagerfactorybean() { LocalEntityManagerfactorybean localEntityManagerfactorybean = new LocalEntityManagerfactorybean(); localEntityManagerfactorybean.setPersistenceUnitName("postgres"); return localEntityManagerfactorybean; } }
When I try to test it:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = EmployeeRepository.class) public class EmployeeRepositoryTest { @Autowired private EmployeeRepository employeeRepository; @Test public void test() { Employee employee = new Employee("Szymon","Nowak"); employeeRepository.save(employee); Assert.assertTrue(true); } }
I received an error:
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.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': Instantiation of bean Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.szymon.repository.EmployeeRepository]: Specified class is an interface at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.instantiateBean(AbstractAutowireCapablebeanfactory.java:1105) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBeanInstance(AbstractAutowireCapablebeanfactory.java:1050) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.doCreateBean(AbstractAutowireCapablebeanfactory.java:510) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBean(AbstractAutowireCapablebeanfactory.java:482) at org.springframework.beans.factory.support.Abstractbeanfactory$1.getObject(Abstractbeanfactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.Abstractbeanfactory.doGetBean(Abstractbeanfactory.java:302) at org.springframework.beans.factory.support.Abstractbeanfactory.getBean(Abstractbeanfactory.java:197) at org.springframework.beans.factory.support.DefaultListablebeanfactory.preInstantiateSingletons(DefaultListablebeanfactory.java:772) at org.springframework.context.support.AbstractApplicationContext.finishbeanfactoryInitialization(AbstractApplicationContext.java:839) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ... 28 more Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.szymon.repository.EmployeeRepository]: Specified class is an interface at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:68) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.instantiateBean(AbstractAutowireCapablebeanfactory.java:1098) ... 44 more
I used to create and test it with a simple entitymanager and create hibernate tables in the database, so I think the database layer is OK
Solution
Your test is annotated with @ contextconfiguration (classes = employeerepository. Class)
However, the classes property of this annotation should refer to configuration (classes annotated with @ configuration, such as AppConfig) Therefore, you start the test without configuring spring data and the repository
If you want to test the repository, you should create @ configuration. XML using @ enablejparepositories and an in memory database