我昨天开始使用spring-data
项目,并试图将测试添加到我创建的存储库中。
项目结构类似于
persistence/pom.xml
src/main/java/
ApplicationConfig
BaseRepository
Network
src/main/test/BaseRepositoryTest
ApplicationConfig
看起来像
@Configuration
@ComponentScan
@EnableJpaRepositories
public class ApplicationConfig {
@Bean
public DataSource dataSource() {
final EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
embeddedDatabaseBuilder.setType(EmbeddedDatabaseType.H2);
return embeddedDatabaseBuilder.build();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.H2);
jpaVendorAdapter.setGenerateDdl(true);
final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
localContainerEntityManagerFactoryBean.setPackagesToScan(getClass().getPackage().getName());
localContainerEntityManagerFactoryBean.setDataSource(dataSource());
return localContainerEntityManagerFactoryBean;
}
}
BaseRepository
看起来像
import org.springframework.data.repository.Repository;
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
List<T> findAll();
}
Network
是一个类似于
@Entity
public class Network extends com.org.comma.persistence.Entity {
private final long networkId;
private final String name;
private final boolean active;
private final DateTime createdAt;
private String createdBy;
private DateTime updatedAt;
private String updatedBy;
...
}
和BaseRepositoryTest
看起来像
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationConfig.class, BaseRepository.class})
public class BaseRepositoryTest {
@Autowired
BaseRepository<Network, Long> baseRepository;
@Test
public void testAllNetworks() {
final List<Network> networks = baseRepository.findAll();
assertTrue(networks.isEmpty());
}
}
我的pom.xml
看起来像
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
当我运行mvn clean install
时,我将错误视为
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:319)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
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:175)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
请帮助我确定我在这里做错了什么,我是这个项目的新手
答案 0 :(得分:36)
存储库管理的类型必须在引导时知道,这意味着您需要实际创建一个这样的具体存储库:
interface NetworkRepository extends BaseRepository<Network, Long> { … }
您展示的代码中还有一些其他故障:
Network
类没有使用@Id
BaseRepository
应该带有@NoRepositoryBean
注释,表明它根本不应该被实例化。有关详细信息,请参阅reference documentation。答案 1 :(得分:8)
如果您有一个扩展另一个的存储库接口并且您忘记了超级接口上的@NoRepositoryBean,也会发生这种情况。
答案 2 :(得分:7)
我遇到了同样的问题,并通过添加@NoRepositoryBean
解决了这个问题@NoRepositoryBean
public interface BaseRespository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {}
答案 3 :(得分:4)
由于 my.package 名称错误,我收到了同样的错误。一旦我提供了正确的包名,它就可以正常工作。
@EnableJpaRepositories( '的 my.package 强>')
答案 4 :(得分:0)
尝试以下更改
@ContextConfiguration(classes = {ApplicationConfig.class, BaseRepository.class}, loader=AnnotationConfigContextLoader.class)
答案 5 :(得分:0)
我在做下面的错误:
在创建工厂时,我已经通过了repo包而不是实体包。
答案 6 :(得分:0)
我认为您想念模型类上的@Entity注释
@Entity
class <classname>
{
//model variables & getters Setters
}
答案 7 :(得分:0)
另一个问题可能是由于忘记了用@Entity
注释类,这就是为什么Spring Boot将其视为Not a managed type
注释完课程后,它应该可以工作了。但是,如果出现以下错误:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]
然后确保您的班级具有一个带有以下注释的ID:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
另一种情况可能是Spring Boot没有检测到您的JPA或CrusRepository软件包。确保使用以下注释对主类进行注释:
@EnableJpaRepositories("Your-Package-Name-That-Includes-Your-Repository")
答案 8 :(得分:0)
尝试将ID替换为您要映射的类或模型的数据类型,且该数据类型将为Integer,因此请在
中替换ID
JpaRepository<T,ID>
带有整数的ID,即JpaRepository<T,Integer>