Skip to content

Commit

Permalink
[ADD] HelloController 및 TDD 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
devAon committed Jul 25, 2020
1 parent 107e52c commit f32626e
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 48 deletions.
Binary file removed .gradle/5.2.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file removed .gradle/5.2.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file removed .gradle/5.2.1/fileChanges/last-build.bin
Binary file not shown.
Binary file removed .gradle/5.2.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file removed .gradle/5.2.1/fileHashes/fileHashes.lock
Binary file not shown.
Empty file removed .gradle/5.2.1/gc.properties
Empty file.
Binary file removed .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
2 changes: 0 additions & 2 deletions .gradle/buildOutputCleanup/cache.properties

This file was deleted.

Binary file removed .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Empty file removed .gradle/vcs-1/gc.properties
Empty file.
4 changes: 0 additions & 4 deletions .idea/gradle.xml

This file was deleted.

25 changes: 0 additions & 25 deletions .idea/jarRepositories.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

74 changes: 71 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ Git Tools : Git Bash
OS : Window
SpringBoot 2.1.9
Java8
Gradle
Gradle 4.10.2

👉 springBootVersion 2.1.7 / 2.1.8 / 2.1.9 모두 괜찮으나, 2.2 이상 버전에서는 정상작동 하지 않는다.

👉 Gradle 5.x에서는 정상작동 하지 않는다.
`Gradle 4.10.2 ` 로 변경하는 방법 ?

​ 인텔리제이에서 `alt+F12` 눌러 👉 터미널에서 `gradlew wrapper --gradle-version 4.10.2` 명령어 실행

<br>


Expand Down Expand Up @@ -152,7 +157,7 @@ public class Application {
🐥 **내장 WAS 란 ?**

* 외부에 WAS를 두지 않고 애플리케이션을 실행할 때 내부에서 WAS를 실행하는 것을 의미
* ?
* 내장 WAS 사용 이유 ?
항상 서버에 톰캣을 설치할 필요가 없다.
스프링 부트로 만들어진 Jar 파일 (실행 가능한 Java 패키징 파일)로 실행하면 된다.
* `언제 어디서나 같은 환경에서 스프링 부트를 배포` 할 수 있기 때문에 내장 WAS 사용 권장
Expand All @@ -161,11 +166,27 @@ public class Application {



<br>



### 📝 API 작성

* **@RestController**

: JSON을 반환하는 컨트롤러로 만들어준다

* **@GetMapping**

: HTTP Method인 Get의 요청을 받을 수 있는 API를 만들어준다

* **@PostMapping, @PutMapping, @DeleteMapping**





<br>



Expand Down Expand Up @@ -226,5 +247,52 @@ public class Application {



###
### 📝 TDD 작성

* **@RunWith(SpringRunner.class)**

: 스프링 부트 테스트와 JUnit 사이에 연결자 역할
테스트를 진행할 때 JUnit에 내장된 실행자 외에 다른 실행자를 실행시킨다.
실행자 : SpringRunner

* **@WebMvcTest**

: 여러 스프링 테스트 어노테이션 중, Web (Spring MVC)에 집중할 수 있는 어노테이션

사용 가능 - @Controller, @ControllerAdvice

사용 불가능 - @Service, @Component, @Repository

* **@Autowired**

: 스프링이 관리하는 빈(Bean)을 주입 받는다.

* **private MockMvc mvc**

: 웹 API 를 테스트할 때 사용

스프링 MVC 테스트의 시작점

이 클래스를 통해 HTTP GET, POST 등에 대한 API 테스트를 할 수 있다.

* **mvc.perform(get("/hello"))**

: MockMvc를 통해 /hello 주소로 HTTP GET 요청을 한다

체이닝이 지원되어 여러 검증 기능을 이어서 선언할 수 있다.

* **.andExpect(status().isOk())**

: mvc.perform 의 결과를 검증한다.

HTTP Header의 `Status를 검증`한다.

Status인 200,404, 500 등 상태 중 200인지 검증한다

* **.andExpect(content().string(hello))**

: mvc.perform 의 결과를 검증한다.

`응답 본문의 내용을 검증`한다.

Controller에서 리턴하는 "hello"와 내용이 일치하는지 검증한다.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
18 changes: 18 additions & 0 deletions src/main/java/com/devAon/aoneemall/web/HelloController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.devAon.aoneemall.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Created by qwone4@gmail.com on 2020-07-26
* Blog : https://velog.io/@aonee
* Github : http://github.com/devAon
*/

@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
35 changes: 35 additions & 0 deletions src/test/java/com/devAon/aoneemall/web/HelloControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.devAon.aoneemall.web;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* Created by qwone4@gmail.com on 2020-07-26
* Blog : https://velog.io/@aonee
* Github : http://github.com/devAon
*/


@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));
}
}

0 comments on commit f32626e

Please sign in to comment.