I have problem with using ObjectMapper in JUnit tests in Spring Boot app.
Jackson mapping POJO:
public Repository() {
@JsonProperty(value="fullName")
public String getFullName() {
return fullName;
}
@JsonProperty(value="full_name")
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@JsonProperty(value = "cloneUrl")
public String getCloneUrl() {
return cloneUrl;
}
@JsonProperty(value = "clone_url")
public void setCloneUrl(String cloneUrl) {
this.cloneUrl = cloneUrl;
}
@JsonProperty(value="stars")
public int getStars() {
return stars;
}
@JsonProperty(value="stargazers_count")
public void setStars(int stars) {
this.stars = stars;
}
...
}
JUnit tests:
@Autowired
private ObjectMapper objectMapper;
@Test
public void testWithMockServer() throws Exception{
Repository testRepository = new Repository();
testRepository.setCloneUrl("testUrl");
testRepository.setDescription("testDescription");
testRepository.setFullName("test");
testRepository.setStars(5);
String repoString = objectMapper.writeValueAsString(testRepository);
...
}
After creating String from testRepository
I see that not every field is set up - only description which do not require any addition JsonProperty mapping.
That is because @JsonProperty
from Repository
class is not taken into account. Do you know how to fix it?
In controller, everything works great.
restTemplate.getForObject(repoUrlBuilder(owner,repository_name),Repository.class);