이번 포스팅에서는 Springboot framework를 이용하여 새로운 프로젝트를 생성하는 방법에 대해 설명한다.
하드웨어 구성
mackbook pro (Retina, 15-inch, Mid 2014, macOS Big Sur v11.4)
IDE
IntelliJ IDEA 2021.2 (Community Edition)
IntelliJ Ultimate에서는 Spring 프로젝트 생성 기능이 기본으로 제공되지만 Community Edition에서는 지원되지 않기 때문에 Graddle Java를 이용하여 생성한다.
1. 프로젝트 생성
2. Project 이름 및 GroupId를 설정
3. build.gradle 설정
plugins {
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group 'com.devlos'
version '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
4. Graddle 새로 고침
우측 상단에 있는 Gradle 새로고침기능을 이용하여 build.gradle에 설정한 dependencies들이 Build 되도록 한다.
다음과 같이 build 콘솔에 BUILD SUCCESSFUL이 출력되면 된다.
5. 패키지 생성 및 MyApplication class, MyAppController class 생성
프로젝트가 처음 생성되면 src>main>java라는 폴더 안에 아무것도 없다.
Springboot 애플리케이션을 만들기 위해서는 다음과 같이 클래스들을 직접 생성해야 한다.
MyAppController 코드
package com.devlos.project.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyAppController {
@GetMapping("/hello-world")
public String helloWorld() {
return "hello-world";
}
}
MyApplication 코드
package com.devlos.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
* @SpringBootApplication 어노테이션을 통해 Application을 만들지 않으면 Controller가 동작하지 않는다.
6. Test code 생성
MyAppController 클래스를 선택한 후, command + shift + t를 눌러 테스트 코드를 만든다.
OK 버튼을 클릭하면 다음과 같이 main의 경로와 동일하게 test 폴더에 Test class가 생성된다.
MyAppControllerTest 코드
package com.devlos.project.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
class HelloWorldControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void helloWorld() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/hello-world"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("hello-world"));
}
}
MyAppController에서 만든 API에 get 요청을 하여 결괏값이 예상 값과 같은지 확인하는 코드이다.
Test 코드를 작성하고, void HelloWorld() 함수 옆의 디버깅 버튼을 누르면,
아래와 같이 테스트가 정상 실행되는 것을 확인할 수 있다.
'프로그래밍 > Spring Boot' 카테고리의 다른 글
객체지향 프로그래밍 정리 #1 (0) | 2021.11.19 |
---|---|
IntelliJ 단축키 (0) | 2021.08.08 |
Lombok (0) | 2021.08.08 |
Annotation 이란? (0) | 2021.08.08 |