Spring Data RESTとMongoDBでプロジェクトを作成しようとすると、同じ問題が発生します。 RESTエンドポイントにアクセスしようとするすべてのテストはjava.lang.IllegalArgumentException: PersistentEntity must not be null!
となり、PersistentEntityResource
ビルダー・メソッドによってスローされます。これは、アプリケーションコンテキストが起動され、RepositoryRestMvcConfiguration
が初期化されると、PersistentEntities
というBeanが空であることを意味します。いくつかのサンプルコード:"PersistentEntityをnullにすることはできません" MongoDBとSpringデータの例外REST
@Document
public class Person {
@Id private String id;
private String name;
private Integer age;
// Getters and setters
}
@RepositoryRestResource(path = "persons", collectionResourceRel = "persons")
public interface PersonRepository extends MongoRepository<Person, String> {
}
@Configuration
@EnableMongoRepositories(basePackages = { "me.woemler.test" })
public class DataSourceConfig {
@Bean(destroyMethod = "close")
public Mongo mongo() throws IOException {
return new EmbeddedMongoBuilder().build();
}
@Bean
public MongoTemplate mongoTemplate(Mongo mongo){
return new MongoTemplate(mongo, "test-db");
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {DataSourceConfig.class, RepositoryRestMvcConfiguration.class})
public class PersonTests {
@Autowired private PersonRepository personRepository;
@Autowired private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setup(){
personRepository.deleteAll();
Person person = new Person();
person.setName("Joe");
person.setAge(33);
personRepository.save(person);
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void test() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get("/persons"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
スタックトレースは次のとおりです。私は、最新の春のプラットフォームのバージョン(ブリュッセル-SR1)で春ブーツ、春データのMongoDB、そして春データRESTを使用しています
Caused by: java.lang.IllegalArgumentException: PersistentEntity must not be null!
at org.springframework.util.Assert.notNull(Assert.java:134)
at org.springframework.data.rest.webmvc.PersistentEntityResource$Builder.<init>(PersistentEntityResource.java:140)
at org.springframework.data.rest.webmvc.PersistentEntityResource$Builder.<init>(PersistentEntityResource.java:123)
at org.springframework.data.rest.webmvc.PersistentEntityResource.build(PersistentEntityResource.java:115)
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:74)
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:55)
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:38)
at org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:200)
at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:132)
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:92)
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toResources(AbstractRepositoryRestController.java:76)
at org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(RepositoryEntityController.java:209)
。 Springブートでアプリケーションを実行すると、エラーは発生しません。テストするのはSpringJUnit4ClassRunner
とSpringRunner
の両方を使用する場合に限られます。私は何が欠けていますか?