Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
허원철/NSC서버팀/NE committed Jan 1, 2025
1 parent d3e11ce commit 32cbeed
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
4 changes: 2 additions & 2 deletions SpringBootTest/src/test/java/com/example/MockMvcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class MockMvcTest {

@Test
public void test() throws Exception {
TestMessage message = new TestMessage("wonchul", 0);
String result = mapper.writeValueAsString(message);
final TestMessage message = new TestMessage("wonchul", 0);
final String result = mapper.writeValueAsString(message);

given(service.jsonTest())
.willReturn(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import static org.mockito.BDDMockito.given;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MockTest {
public class MockitoBeanTest {

@Autowired
TestRestTemplate restTemplate;
Expand All @@ -25,7 +25,7 @@ public void test() {
given(service.test(0))
.willReturn("Spring Boot Service Test");

String result = restTemplate.getForObject("/test?flag=0", String.class);
final String result = restTemplate.getForObject("/test?flag=0", String.class);
assertThat(result)
.isEqualTo("Spring Boot Service Test");
}
Expand Down
51 changes: 51 additions & 0 deletions SpringBootTest/src/test/java/com/example/TestBeanTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example;

import static org.assertj.core.api.Assertions.assertThat;

import com.example.domain.TestMessage;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.bean.override.convention.TestBean;

import com.example.service.BasicService;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class TestBeanTest {

@Autowired
TestRestTemplate restTemplate;

@TestBean
BasicService basicService;

static BasicService basicService() {
return new BasicService() {
@Override
public String test(int flag) {
return "Spring Boot Service Test";
}

@Override
public TestMessage jsonTest() {
throw new UnsupportedOperationException("jsonTest not implemented");
}
};
}

@Test
public void mvcTest() {
final String result = restTemplate.getForObject("/test?flag=0", String.class);
assertThat(result)
.isEqualTo("Spring Boot Service Test");
}

@Test
public void serviceTest() {
assertThat(basicService.test(0))
.isEqualTo("Spring Boot Service Test");
}
}

0 comments on commit 32cbeed

Please sign in to comment.