bookmark_borderTDD(spring) saving user to DB

  1. Prerequisite
  2. Write Second Test Case for the project
    1. Test Case: Saving user to DB
    2. UserControllerTest.java – Second Test Case 추가
    3. User.java – Private Key Id 추가
    4. UserRepository.java 추가
    5. UserService.java – Save user data
    6. UserController.java 추가 – Save user data
    7. Test 실행

Prerequisite

TDD(Spring) Saving user to DB

Write Second Test Case for the Project

Test Case: Saving user to DB

UserControllerTest.java Test Case 추가

package com.example.backend;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.example.backend.user.User;
import com.example.backend.user.UserRepository;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@TestMethodOrder(OrderAnnotation.class)
public class UserControllerTest {

    @Autowired
    TestRestTemplate testRestTemplate;

    @Autowired
    UserRepository userRepository;

    private static final String API_1_0_USERS = "/api/1.0/users";

    private User createValidUser() {
        User user = new User();
        user.setUsername("test-user");
        user.setDisplayName("test-display");
        user.setPassword("Password");
        return user;
    }

    @Test
    @Order(1)
    public void postuser_whenUserIsValid_userSaveToDatabase() {
        User user = createValidUser();
        testRestTemplate.postForEntity(API_1_0_USERS, user, Object.class);
        assertThat(userRepository.count()).isEqualTo(1);
    }

    @Test
    @Order(2)
    public void postuser_whenUserIsValidreceiveOK() {
        User user = createValidUser();
        ResponseEntity<Object> response = testRestTemplate.postForEntity(API_1_0_USERS, user, Object.class);
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    }
}
  • postuser_whenUserIsValid_userSaveToDatabase() Method를 추가함.

User.java – Private Key Id 추가

package com.example.backend.user;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import lombok.Data;

@Data
@Entity
public class User {
    @Id
    @GeneratedValue
    private long id;

    private String username;

    private String displayName;

    private String password;
}
  • Set primary key for DB

UserRepository.java 추가

package com.example.backend.user;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
    
}
  • JpaRepository는 대부분 일반적인 Database Query Method를 포함하고 있고. 두개의 Parameter들 <User, Long> User는 Our Entity의 type이고, Long은 Id Field의 type이다.

UserService.java – Save user data 추가

package com.example.backend.user;

import org.springframework.stereotype.Service;

@Service
public class UserService {

    UserRepository userRepository;

    public UserService(UserRepository userRepository){
        super();
        this.userRepository = userRepository;
    }

    public User save(User user){
        return userRepository.save(user);
    }

}
  • User Controller에서 사용할 save method를 UserService.java에 추가

UserController.java save method

package com.example.backend.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    UserService userService;
    

    @PostMapping("/api/1.0/users")
    public void createUser(@RequestBody User user){
        userService.save(user);
    }
}
  • @Autowired annotation을 이용하여 UserService save method를 UserController에서 사용.

Test 실행

$ mvn test
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.279 s
[INFO] Finished at: 2020-09-10T10:11:00+09:00
[INFO] ------------------------------------------------------------------------
ANOTE.DEV