ใ์คํ๋ง ๋ถํธ์ AW๋ก ํผ์ ๊ตฌํํ๋ ์น ์๋น์คใ๋ฅผ ๋ณด๊ณ ๊ณต๋ถํ ๊ธฐ๋ก์ ๋๋ค. (์ฐ๋นํํ)
๐๐ป๋ชฉํ
ํ ์คํธ ์ฝ๋์ ๋ํ ๊ฐ๋
๋ง๋๋ ๋ชจ๋ ์๋ฌ ์ฒ๋ฆฌํ๊ธฐ
TDD๋ ํ ์คํธ๊ฐ ์ฃผ๋ํ๋ ๊ฐ๋ฐ์ ๋ปํ๋ค. ์ฆ ํ ์คํธ์ฝ๋๋ฅผ ๋จผ์ ์์ฑํ๋ ๊ฒ๋ถํฐ ๊ฐ๋ฐ์ ์์ํ๋ค.
ํ
์คํธ
๋ฅผ ๋จผ์ ์์ฑํ๋ก๋์
์ฝ๋๋ฅผ ์์ฑ๋ฆฌํฉํ ๋ง
ํจ๋จ์ ํ ์คํธ๋ TDD์ ์ฒซ ๋ฒ์งธ ๋จ๊ณ์ธ ๊ธฐ๋ฅ ๋จ์์ ํ ์คํธ ์ฝ๋ ์์ฑ์ ์๋ฏธํ๋ค. TDD์ ๋ฌ๋ฆฌ ํ ์คํธ ์ฝ๋๋ฅผ ๋จผ์ ์์ฑํ ํ์๋, ๋ฆฌํฉํ ๋ง์ ํ ํ์๋ ์๋ค.
ํ ์คํธ ์ฝ๋ ์์ฑ์ ๋์์ฃผ๋ ํ๋ ์์ํฌ์๋ xUnit๊ฐ ์๋ค.
๐ groupId ์ฐพ๊ธฐ
groupId๋
build.gradle
ํ์ผ ์์ group๋ก ๋ช ์๋์ด์ใธ ใ ^^ na noon babo..
java
- package ์์ฑ@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
๋ด์ฅ WAS
์ธ ํฐ์บฃ์ ์คํ@RestController
public class HelloController {
@GetMapping("/hello")
public String Hello() {
return "hello!!";
}
}
@RestController
@ResponseBody
๋ฅผ ๊ฐ ๋ฉ์๋๋ง๋ค ์ค์ ํ๋๋ฐ ์ด์ ๋ ํ๋ฒ์ ๊ฐ๋ฅ!@GetMapping
HTTP GET
์์ฒญ์ ๋ฐ์ ์ ์๋ API๋ฅผ ๋ง๋ค์ด์คsrc/test/java
๋๋ ํ ๋ฆฌ์ main์์ ์์ฑํ๋ ํจํค์ง๋ฅผ ์์ฑ@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void hello๊ฐ_๋ฆฌํด๋๋ค() throws Exception {
String hello = "hello";
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
@Autowired
private MockMvc mvc
HTTP GET, POST
์ ๋ํ API ํ
์คํธ ๊ฐ๋ฅmvc.perform(get("/hello"))
์ฒด์ด๋
์ด ์ง์๋์ด ๊ฒ์ฆ ๊ธฐ๋ฅ์ ์ด์ด์ ์ํ ๊ฐ๋ฅ.andExpect(status().isOk())
.andExpect(content().string(hello));
๐ERROR!
๋ค๋ค ์์๋ ๊ทธ ์๋ฌ์ ๋๋ค. (8080 ํฌํธ ๋ฒํธ๊ฐ ์ด๋ฏธ ์ฌ์ฉ์ค)
IntelliJ Spring Boot(Gradle) ํฌํธ ๋ฒํธ ๋ณ๊ฒฝํ๋ ๋ฒ
Run
-Edit Configurations
- Environment variables ์ถ๊ฐ
Name
: server.portvalue
: 5858(์ํ๋ ํฌํธ๋ฒํธ)
๋กฌ๋ณต ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์๋ฐ ๊ฐ๋ฐํ ๋ ์์ฃผ ์ฌ์ฉํ๋ ์ฝ๋์ธ Getter, Setter, ๊ธฐ๋ณธ ์์ฑ์, toString
๋ฑ์ ์ด๋
ธํ
์ด์
์ผ๋ก ์๋ ์์ฑํด์ค๋ค.
๋กฌ๋ณต ํ๋ฌ๊ทธ์ธ ์ค์น๋ ํ ๋ฒ๋ง ํด๋ ๋์ง๋ง
๋กฌ๋ณต ์ค์ ์ ํ๋ก์ ํธ๋ง๋ค ํด์ค์ผํ๋ค. (build.gradle ์ถ๊ฐ)
compile('org.projectlombok:lombok')
ctrl+shift+A
- pluginsEnable annotation processing
์ฒดํฌpackage com.studying.book.springboot.web.dto;
@Getter
@RequiredArgsConstructor
public class HelloResponseDto {
private final String name;
private final int amount;
}
package com.studying.book.springboot.web.dto;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class HelloResponseDtoTest {
@Test
public void ๋กฌ๋ณต_๊ธฐ๋ฅ_ํ
์คํธ() {
//given
String name="test";
int amount = 1000;
//when
HelloResponseDto dto = new HelloResponseDto(name, amount);
//then
assertThat(dto.getName()).isEqualTo(name);
assertThat(dto.getAmount()).isEqualTo(amount);
}
}
๐Cannot resolve symbol 'assertThat' ERROR!
์ถ์ฒ : https://aonee.tistory.com/2
์์ธ : junit4์ ๊ฒฝ์ฐ org.junit.Assert ํด๋์ค์์ ํ์ฉํ๋ Hamcrest ํ์ฉ ๋ฉ์๋๋ฅผ ์ ๊ณตํ์ง ์์๊ธฐ ๋๋ฌธ
ํด๊ฒฐ : gradle ์์กด์ฑ ์ถ๊ฐ
build.gradle
dependencies { testCompile 'junit:junit:4.12' }
์๋ก ์ถ๊ฐ๋ Gradle์ ์ ์ฉํ๊ธฐ ์ํด Gradle-Reimport All Gradle Project (์๋ก๊ณ ์นจ ์ด๋ชจํฐ์ฝ)์ ํด๋ฆญ!
- ์๋ก ์ถ๊ฐ๋ gradle import ๊ฐ๋ฅ
More actions ...
-import static method
- 3๋ฒ์งธ์Assertions.assertThat
ํด๋ฆญ!
- import static org.assertj.core.api.Assertions.assertThat;
์๋ฃ! ์จ์จ์จ
๐ ํ ์คํธ ์คํจ ์๋ฌ! ๋กฌ๋ณต ์๋ฌ!
error: variable name not initialized in the default constructor private final String name;
์์ํ ์๋ฌ๋ค๊ณผ ํจ๊ป ์์ ๊ฐ์ ์๋ฌ๋ค์ด ์๊พธ ๋ ์ ๊ตฌ๊ธ๋งํ ๊ฒฐ๊ณผ ์ ์๋์ ์ด์์์ ํด๊ฒฐ๋ฐฉ๋ฒ์ ์ฐพ์๋ค.
์ฑ ์ ์์ ๋ค์ Gradle 4 ๊ธฐ์ค์ผ๋ก ์ฐ์ฌ์ ธ์๋ ์ํ์ด๊ณ ! ๋กฌ๋ณต์ด Gradle 5๋ก ๋๋ฉด์ ๋ง์ด ๋ณ๊ฒฝ๋์๋ค๊ณ ํ๋ค!
alt+F12
์ผ๋ก ํฐ๋ฏธ๋ ์ด๊ธฐ./gradlew wrapper --gradle-version 4.10.2
์
๋ ฅjava-resources ํด๋ ์์ new - file - application.properties
์์ฑํด์
server.port=5858
ํฌํธ๋ฒํธ๋ฅผ ์ง์ ํด์คฌ๋ค. ใ ก ๋ ใ ก๐คข
@RestController
public class HelloController {
@GetMapping("/hello")
public String Hello() {
return "hello";
}
@GetMapping("/hello/dto")
public HelloResponseDto helloDto(@RequestParam("name") String name,
@RequestParam("amount") int amount) {
return new HelloResponseDto(name, amount);
}
}
name
์ผ๋ก ๋๊ธด ํ๋ผ๋ฏธํฐ๋ฅผ String name
์ ์ ์ฅ@RunWith(SpringRunner.class)
@WebMvcTest(HelloController.class)
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void hello๊ฐ_๋ฆฌํด๋๋ค() throws Exception {
String hello = "hello";
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
@Test
public void 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)));
}
}
String.valueOf(amount)
$
๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋๋ช
์ ๋ช
์$.name
$.amount
๐ ํ ์คํธ status() ์์ 404 ์๋ฌ๊ฐ ๋ฌ๋ค?
- ์.. 404 ์๋ฌ๊ฐ ๋ฌ๋ค๋ฉด ๋ฌด.์กฐ.๊ฑด URI๋ฅผ ํ๋ฒ ๋ ํ์ธํฉ์๋ค.
- ์ ๊ฐ์ ๊ฒฝ์ฐ์๋ get ("/hello/dto")๊ฐ ์๋ ("hello/dto")๋ก ๋งคํ์์ผ์ 404 ์๋ฌ๊ฐ ๋์์ต๋๋ค....๐ฉ
๐ non-ascii characters in an identifier intellij
- ์ปค๋ฐํ ๋ "Non-ascii " ์ด์ฉ๊ตฌ ์๋ฌ์
ctrl+shift+a
+settings
-inspections
-non-ascii character
- ์ฒดํฌ๋ฐ์ค ํด์