Table of Contents
ν μ€νΈ μ½λλ
TDD
ν μ€νΈ μ½λλ₯Ό λ¨Όμ μμ±νλ ν μ€νΈ μ£Όλ κ°λ°
1. νμ μ€ν¨νλ ν μ€νΈ μ½λ μμ±
2. ν μ€νΈκ° ν΅κ³Όνλ νλ‘λμ μ½λ μμ±
3. ν μ€νΈκ° ν΅κ³Όνλ©΄ νλ‘λμ μ½λ 리ν©ν λ§
μ΄λ κ² μΈ λ¨κ³λ₯Ό λμκ°λ©΄μ κ°λ°νλ κ³Όμ μ κ±°μΉλ€.
λ¨μν μ€νΈ
- κΈ°λ₯ λ¨μμ ν μ€νΈ μ½λλ₯Ό μμ±νλ κ²
- νμμ±
- κ°λ° λ¨κ³ μ΄κΈ°μ λ¬Έμ λ°κ²¬νλλ‘ λμμ€
- μΆν μ½λ 리ν©ν λ§, λΌμ΄λΈλ¬λ¦¬ μ κ·Έλ μ΄λ λ±μμ κΈ°μ‘΄ κΈ°λ₯μ λμμ νμΈ κ°λ₯
- κΈ°λ₯μ λν λΆνμ€μ± κ°μμν΄
- μμ€ν μ λν μ€μ λ¬Έμλ‘ μ 곡 κ°λ₯
- μλ°μμλ JUnitμ΄λΌλ ν μ€νΈ νλ μμν¬κ° μ¬μ©λ¨
Controller μμ±
μΌλ°μ μΌλ‘ ν¨ν€μ§λͺ μ μΉ μ¬μ΄νΈ μ£Όμμ μμμΌλ‘ μ€μ
λ©μΈ ν΄λμ€
package com.oliviarla.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication
- μ€νλ§ λΆνΈμ μλ μ€μ , μ€νλ§ Bean μ½κΈ° λ° μμ±μ μλμΌλ‘ μ€μ ν¨
- λ©μΈ ν΄λμ€κ° μλ μμΉλΆν° μ€μ μ μ½μ΄λκ°λ―λ‘ νλ‘μ νΈ μ΅μλ¨μ μμΉν΄μΌν¨
λ΄μ₯ WAS
- λ³λλ‘ μΈλΆμ WASλ₯Ό λμ§ μκ³ μ ν리μΌμ΄μ μ€ν μ λ΄λΆμμ μ€νλ¨
- μΈμ μ΄λμλ κ°μ νκ²½μμ μ€νλ§ λΆνΈ λ°°ν¬ κ°λ₯
- μΈμ₯ WAS μ¬μ©ν κ²½μ° λͺ¨λ μλ²μ WAS μ’ λ₯, λ²μ , μ€μ μ μΌμΉμμΌμΌ νλ―λ‘ λ³΅μ‘ν μμ μμλ¨ -> λ΄μ₯ WASλ‘ ν΄κ²° κ°λ₯
컨νΈλ‘€λ¬ ν΄λμ€
package com.oliviarla.springboot.web;
import com.oliviarla.springboot.web.dto.HelloResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
@RestController
- JSONμ λ°ννλ 컨νΈλ‘€λ¬λ‘ μ€μ
@GetMapping
- HTTP Get λ©μλμ μμ²μ λ°μ μ μλ API λ©μλλ₯Ό λ§λ€μ΄ μ£Όλ μ΄λ Έν μ΄μ
- localhost:8080/hello λΌλ urlλ‘ μμ²μ΄ μ¨λ€λ©΄ "hello"λ₯Ό λ°ννλ κΈ°λ₯μ μ 곡ν¨
ν μ€νΈ ν΄λμ€
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {
@Autowired //μ€νλ§μ΄ κ΄λ¦¬νλ Bean μ£Όμ
λ°μ
private MockMvc mvc;
@Test
public void return_hello() throws Exception{
String hello = "hello";
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
}
MockMvc ν΄λμ€λ₯Ό μ¬μ©ν΄ μΉ APIλ₯Ό ν μ€νΈνλ μ½λ
@WebMvcTest: Webμ μ§μ€νμ¬ ν μ€νΈ ν μ μλ μ΄λ Έν μ΄μ
mvc.perform λ©μλμμλ...
- HTTP Headerμ statusκ° 200μΈμ§ νμΈ
- Hello 컨νΈλ‘€λ¬μμ λ°νλ contentκ° "hello"μ μΌμΉνλμ§ νμΈ
λ¨Όμ ν μ€νΈμ½λλ‘ κ²μ¦ν ν μ λ’°κ° κ°μ§ μλλ€λ©΄ νλ‘μ νΈλ₯Ό μ€νν΄ νμΈνλ λ°©ν₯μΌλ‘ κ°λ°ν΄μΌ ν¨!
Lombok
μλ° κ°λ° μ μμ£Ό μ¬μ©νλ Getter, Setter, κΈ°λ³Έ μμ±μ, toString λ±μ μ΄λ Έν μ΄μ μ ν΅ν΄ μλ μμ±ν΄μ€
build.gradleμ μμ‘΄μ± μΆκ°
implementation 'org.projectlombok:lombok'
+) 둬볡 νλ¬κ·ΈμΈ μ€μΉλ₯Ό ν μ μμΌλ νλ‘μ νΈ μ§νν΄λ΄€μ λ μ€μΉνμ§ μμλ λΆνΈν¨ μμ΄ μ§νλμμΌλ―λ‘ μλ΅ν©λλ€ π
Lombok νμ©νκΈ°
둬볡μ μ μ©ν΄ ν΄λμ€λ₯Ό μμ±ν΄λ³Έλ€.
DTO ν΄λμ€
package com.oliviarla.springboot.web.dto;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public class HelloResponseDto {
private final String name;
private final int amount;
}
@Getter: μ μΈλ λͺ¨λ νλμ get λ©μλ μμ±
@RequiredArgsConstructor: μ μΈλ λͺ¨λ final νλκ° ν¬ν¨λ μμ±μ μμ± (final μλ νλλ ν¬ν¨λμ§ μμ)
컨νΈλ‘€λ¬ ν΄λμ€ μμ
package com.oliviarla.springboot.web;
import com.oliviarla.springboot.web.dto.HelloResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
@GetMapping("/hello/dto")
public HelloResponseDto helloResponseDto(@RequestParam("name") String name, @RequestParam("amount") int amount){
return new HelloResponseDto(name, amount);
}
}
νλΌλ―Έν° κ°μΌλ‘ μ λ ₯λ name, amount κ°μ μ¬μ©ν HelloResponseDto κ°μ²΄λ₯Ό μμ±ν΄ λ°ν
ex) "localhost:8080/hello/dto?name=kim&amount=1" λΌλ URL μμ² μ HelloResponseDto(kim,1) μ΄λΌλ κ°μ²΄κ° λ°νλ¨
ν μ€νΈ ν΄λμ€μ λ©μλ μΆκ°
@Test
public void return_helloDto() throws Exception{
String name = "hello";
int amount = 1000;
mvc.perform(get("/hello/dto")
.param("name", name)
.param("amount", String.valueOf(amount)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(name)))
.andExpect(jsonPath("$.amount", is(amount)));
}
νλΌλ―Έν° κ°μΌλ‘ nameκ³Ό amount λ³μμ κ°μ μ λ¬νκ³ , GETμΌλ‘ μ»μ JSON μλ΅ κ°κ³Ό μΌμΉνλ©΄ ν μ€νΈκ° ν΅κ³Όνκ² λλ€.
MVCμ param κ°μ Stringλ§ νμ©λλ―λ‘ int, date νμμ μ¬μ©νλ €λ©΄ StringμΌλ‘ νλ³ν ν μ¬μ©ν΄μΌ ν¨.
λ§μΉλ©°
λ³΄ν΅ μλ°μμ ν μ€νΈ μ½λλ₯Ό μμ±ν λ Assertjμ AssertThat λ©μλλ₯Ό λ§μ΄ μ¬μ©νμ§λ§, μ€νλ§ λΆνΈμμ ν μ€νΈ μ½λλ₯Ό μμ±ν λλ API ν μ€νΈμ μ ν©ν MockMvcμ λ©μλλ₯Ό μ¬μ©νμ¬ νΈλ¦¬νκ² λ¨μν μ€νΈλ₯Ό μ§νν μ μλ€!
'Spring Boot > μ€νλ§ λΆνΈμ AWSλ‘ νΌμ ꡬννλ μΉ μλΉμ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
6μ₯: AWS μλ² νκ²½ λ§λ€κΈ° (0) | 2022.06.27 |
---|---|
3μ₯: JPAλ‘ λ°μ΄ν°λ² μ΄μ€ λ€λ£¨κΈ° (2) (0) | 2022.06.15 |
3μ₯: JPAλ‘ λ°μ΄ν°λ² μ΄μ€ λ€λ£¨κΈ° (1) (0) | 2022.06.15 |
1μ₯: μΈν 리μ μ΄ μ¬μ©νκΈ° (μμ μ€) (0) | 2022.06.13 |
λκΈ