Spring Unit Test
This commit is contained in:
46
src/test/java/com/rest/api/repo/UserJpaRepoTest.java
Normal file
46
src/test/java/com/rest/api/repo/UserJpaRepoTest.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.rest.api.repo;
|
||||
|
||||
import com.rest.api.entity.User;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest
|
||||
public class UserJpaRepoTest {
|
||||
|
||||
@Autowired
|
||||
private UserJpaRepo userJpaRepo;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Test
|
||||
public void whenFindByUid_thenReturnUser() {
|
||||
String uid = "angrydaddy@gmail.com";
|
||||
String name = "angrydaddy";
|
||||
// given
|
||||
userJpaRepo.save(User.builder()
|
||||
.uid(uid)
|
||||
.password(passwordEncoder.encode("1234"))
|
||||
.name(name)
|
||||
.roles(Collections.singletonList("ROLE_USER"))
|
||||
.build());
|
||||
// when
|
||||
Optional<User> user = userJpaRepo.findByUid(uid);
|
||||
// then
|
||||
assertTrue(user.isPresent());
|
||||
assertThat(user.get().getName(), is(name));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user