I have project in Spring Boot. I have User model, what have Profile model in relation OneToOne
:
User: (Simplified)
@Entity
@Table(name = "users")
public class User extends AbstractEntity {
@Id
@GeneratedValue
private Integer id;
@NotEmpty
@Basic(optional = false)
@Column(nullable = false, unique = true)
private String username;
@Valid
@JsonIgnore
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
private Profile profile;
@JsonIgnore
public Profile getProfile() {
return profile;
}
@JsonProperty
public void setProfile(Profile profile) {
this.profile = profile;
}
}
Profile: (Simplified)
@Entity
@Table(name = "profiles")
public class Profile extends AbstractEntity {
@Id
@GeneratedValue
private Integer id;
@NotEmpty
@Basic(optional = false)
@Column(nullable = false)
private String name;
@NotEmpty
@Basic(optional = false)
@Column(nullable = false)
private String surname;
// Getters, setters, etc
}
My test:
@Test
public void createUserAndProfileReturnsCreatedStatus() throws Exception {
final User user = Generator.generateUser();
user.setProfile(Generator.generateProfile());
MvcResult mvcResult = this.mockMvc.perform(
post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content(toJson(user)))
.andExpect(status().isCreated())
.andReturn();
}
Problem is, when i do user.setProfile()
, Profile is set into User but when i call toJson(user)
its automatically ignored because of my annotations in model.
How to disable those annotations just for purpose of testing? Is it possible?
I dont want to remove @JsonIgnore annotations from model, because they are there to not expose Profile when I READ
user by GET /users/<id>
.