Every attempt at creating a project with Spring Data REST and MongoDB has run into the same annoying issue. Every test that tries access a REST endpoint results in a java.lang.IllegalArgumentException: PersistentEntity must not be null!, thrown by the PersistentEntityResource builder method. This implies that when the application context is started and RepositoryRestMvcConfiguration is initialized, that the PersistentEntities bean is empty. Some example code:
@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());
}
}
Stacktrace is:
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)
I am using Spring Boot, Spring Data MongoDB, and Spring Data REST with the latest Spring Platform version (Brussels-SR1). Running the application with Spring Boot, I don't receive any errors, it is only when testing, using both the SpringJUnit4ClassRunner and SpringRunner. What am I missing?
Had same issue and had to debug spring internals a lot
The reason of error - missing MappingConverter object in MongoTemplate. When mongoTemplate bean is automatically created by Spring following constructor is used
public MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter)
To solve this issue there are two options:
1) don't redefine MongoTemplate bean. You can use application.properties to specify database
spring.data.mongodb.uri=mongodb://hostname:27017/dbName
2) Autowire mongoConverter and use in creation of mongoTemplate
@Autowired
private MongoConverter mongoConverter;
public @Bean
MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(), mongoConverter);
return mongoTemplate;
}
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With