Skip to content

Instantly share code, notes, and snippets.

@okihouse

okihouse/Test Secret

Last active February 4, 2016 09:23
Show Gist options
  • Save okihouse/f5e2fe8fa4c17d6a6be9 to your computer and use it in GitHub Desktop.
Save okihouse/f5e2fe8fa4c17d6a6be9 to your computer and use it in GitHub Desktop.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class UserTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private UserRepository userRepository;
@Autowired
private UserInfoRepository userInfoRepository;
private MockMvc mockMvc;
@Before
public void before(){
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void test() throws Exception {
// create temporary user for test.
User user = new User();
user.setType(Type.User);
UserInfo userInfo = new UserInfo();
userInfo.setEmail("temporary_user@test.com");
userInfo.setUser(user);
user.setUserInfo(userInfo);
// persist
userRepository.save(user);
// request post
mockMvc.perform(
post("/user")
.param("email", "temporary_user@test.com")
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andDo(print());
}
}
@Entity
@Table(name = "USER")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "UNO")
private Long userNo;
@Column(name = "USER_TYPE")
@Enumerated(EnumType.STRING)
private USER_TYPE type;
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private UserInfo userInfo;
}
@Entity
@Table(name = "USER_INFO")
@Data
public class UserInfo {
@Id
private Long userNo;
@Column(name = "EMAIL")
private String email;
@MapsId
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "UNO")
private User user;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment