- structure
- build.gradle
group 'org.blog.test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.4.0.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.4.0.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.4.0.RELEASE'
compile group: 'com.h2database', name: 'h2', version: '1.4.192'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.10'
compile group: 'junit', name: 'junit', version: '4.12'
}
- TestController.java
package org.blog.test.controller;
import org.blog.test.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/test/{name}")
public String getByName(@PathVariable String name) {
return String.valueOf(testService.getTestTableByName(name));
}
}
- JunitApplication.java
package org.blog.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JunitApplication {
public static void main(String[] args) {
SpringApplication.run(JunitApplication.class, args);
}
}
- TestControllerTest.java
package org.blog.test.controller;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
import org.blog.test.entity.TestTable;
import org.blog.test.service.TestService;
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.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class)
@WebMvcTest(TestController.class)
public class TestControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private TestService testService;
@Test
public void getByName() throws Exception {
//Given
TestTable testTable = new TestTable("test");
List resultList = new ArrayList<>();
resultList.add(testTable);
given(this.testService.getTestTableByName("test")).willReturn(resultList);
//When
this.mockMvc.perform(get("/test/test").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
- result
MockHttpServletRequest:
HTTP Method = GET
Request URI = /test/test
Parameters = {}
Headers = {Accept=[application/json]}
Handler:
Type = org.blog.test.controller.TestController
Method = public java.lang.String org.blog.test.controller.TestController.getByName(java.lang.String)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8], Content-Length=[31]}
Content type = application/json;charset=UTF-8
Body = [TestTable(id=null, name=test)]
Forwarded URL = null
Redirected URL = null
Cookies = []

댓글 없음 :
댓글 쓰기