ApiController테스트 중 에러가 발생했다.
테스트 코드는 이렇다.
@Test
public void posts_등록한다() throws Exception {
//given
String title = "title";
String content = "content";
PostsSaveRequestDto requestDto = PostsSaveRequestDto.builder()
.title(title)
.content(content)
.author("author")
.build();
String url = "http://localhost:" + port +"/api/v1/posts";
//when
ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, requestDto, Long.class);
//then
assertThat(responseEntity.getStatusCode())
.isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody())
.isGreaterThan(0L);
List<Posts> all = postsRepository.findAll();
assertThat(all.get(0).getTitle())
.isEqualTo(title);
assertThat(all.get(0).getContent())
.isEqualTo(content);
}
코드는 잘못된 게 없는 것 같은데 계속해서 아래와 같은 에러메세지가 출력되었다.
Error creating bean with name 'com.webex.web.PostApiControllerTest': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'local.server.port' in value "${local.server.port}"
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.webex.web.PostApiControllerTest': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'local.server.port' in value "${local.server.port}"
...
...
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'local.server.port' in value "${local.server.port}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at
검색해봐도 원하는 해결법이 나오지 않았다.
저번에 만들었던 프로젝트와 하나하나 비교를 해보았다.
아.
spring web 의존성을 추가해주지 않아서 생긴 문제였다.
아.
build.gradle에implementation 'org.springframework.boot:spring-boot-starter-web'
를 추가해주었다.