Skip to content

Commit 32cbeed

Browse files
author
허원철/NSC서버팀/NE
committed
fix test
1 parent d3e11ce commit 32cbeed

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

SpringBootTest/src/test/java/com/example/MockMvcTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class MockMvcTest {
3030

3131
@Test
3232
public void test() throws Exception {
33-
TestMessage message = new TestMessage("wonchul", 0);
34-
String result = mapper.writeValueAsString(message);
33+
final TestMessage message = new TestMessage("wonchul", 0);
34+
final String result = mapper.writeValueAsString(message);
3535

3636
given(service.jsonTest())
3737
.willReturn(message);

SpringBootTest/src/test/java/com/example/MockTest.java renamed to SpringBootTest/src/test/java/com/example/MockitoBeanTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import static org.mockito.BDDMockito.given;
1313

1414
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
15-
public class MockTest {
15+
public class MockitoBeanTest {
1616

1717
@Autowired
1818
TestRestTemplate restTemplate;
@@ -25,7 +25,7 @@ public void test() {
2525
given(service.test(0))
2626
.willReturn("Spring Boot Service Test");
2727

28-
String result = restTemplate.getForObject("/test?flag=0", String.class);
28+
final String result = restTemplate.getForObject("/test?flag=0", String.class);
2929
assertThat(result)
3030
.isEqualTo("Spring Boot Service Test");
3131
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.example;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.example.domain.TestMessage;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
11+
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.test.context.bean.override.convention.TestBean;
13+
14+
import com.example.service.BasicService;
15+
16+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
17+
public class TestBeanTest {
18+
19+
@Autowired
20+
TestRestTemplate restTemplate;
21+
22+
@TestBean
23+
BasicService basicService;
24+
25+
static BasicService basicService() {
26+
return new BasicService() {
27+
@Override
28+
public String test(int flag) {
29+
return "Spring Boot Service Test";
30+
}
31+
32+
@Override
33+
public TestMessage jsonTest() {
34+
throw new UnsupportedOperationException("jsonTest not implemented");
35+
}
36+
};
37+
}
38+
39+
@Test
40+
public void mvcTest() {
41+
final String result = restTemplate.getForObject("/test?flag=0", String.class);
42+
assertThat(result)
43+
.isEqualTo("Spring Boot Service Test");
44+
}
45+
46+
@Test
47+
public void serviceTest() {
48+
assertThat(basicService.test(0))
49+
.isEqualTo("Spring Boot Service Test");
50+
}
51+
}

0 commit comments

Comments
 (0)