본문 바로가기

내가 당면한 문제와 해결방안

can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)

스프링부트 테스트 코드 작성중 이런 에러가.

ㅈㄴ 친절하게 말해주는데 ㅎ ㅏ... 나는 먼지 모르겠네 ...

 

 

 

 

 

 

package com.회사.lab.tour.geolocation;

import com.회사.lab.tour.geolocation.controller.AirportController;
import com.회사.lab.tour.geolocation.model.Place;
import com.회사.lab.tour.geolocation.result.ResultApi;
import com.회사.lab.tour.geolocation.service.PlaceService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

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


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class AirportControllerTest {

@Autowired
TestRestTemplate testRestTemplate;

@Test
public void test() throws Exception {
	ResponseEntity<ResultApi> response = testRestTemplate.getForEntity("/api/v1/airports?q=Al&page=1&pageSize=20", ResultApi.class);
	assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
	assertThat(response.getBody()).isNotNull();
	}
}

테스트 코드는 이러했다.

 

 

https://www.concretepage.com/questions/334

 

JsonMappingException: No suitable constructor found for type

JsonMappingException: No suitable constructor found for type

www.concretepage.com

여기 은혜로운 답변이.

 

While reading JSON, the type of class must have default constructor.  Jackson uses default constructor to initialize the class.  So create a default constructor in your person class as

 

public Person() {}

 

 

default constructor 를 생성하지 않아도 되는줄알았는데... 그런 경우도 있나보군.  그래서 나도

 

public ResultApi() {
}

 

이거 하나 생성하니까 테스트 성공했다.