2022. 1. 19. 00:21ใ๐ TIL
SpringBoot Mvc ์ ํ ์คํธ ์ฝ๋๋ฅผ ์ฒ์ ์์ฑํ ๋ ๋ฐ์ํ ์๋ฌ ๊ธฐ๋ก
Controller
CSAService2020Service ๋ผ๋ Service Layer ๋ฅผ ์์กดํ๊ณ ์๋ค.
@RestController(path = "CSA2020")
@RequiredArgsConstructor
public class CSA2020Controller {
private final CSA2020Service csa2020Service;
@PostMapping("/CSA2020SelectMst")
public Object CSA2010SelectMst(@RequestBody @Valid CSA2020MstParams param){
return csa2020Service.CSA2020SelectMst(param);
}
}
TestCode
@RunWith(SpringRunner.class)
@WebMvcTest
@ExtendWith(MockitoExtension.class)
public class CSA2020ControllerTest extends TestCase {
@Autowired
private MockMvc mvc;
@MockBean
private CSA2020Service csa2020Service;
@Test
public void test1() throws Exception {
CSA2020MstParams params = new CSA2020MstParams();
params.setPrsc_cndt_dept_cd("1131");
params.setPrsc_date("20220118");
ObjectMapper objMapper = new ObjectMapper();
String json = objMapper.writeValueAsString(params);
mvc.perform(post("/CSA2020/CSA2020SelectMst")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
).andExpect(status().isOk());
}
}
Error log
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'CSACommonCodeController': Unsatisfied dependency expressed through field
'icsaCommonCodeService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'ํจํค์ง๋ช
' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
์ค๋ฅ ์์ธ
์๋ฌ ๋ก๊ทธ๋ฅผ ํ์ธํ๋ฉด CSACommonCodeController ๋ผ๋ ๋น์ ์์ฑํ๋๋ฐ ๋ฌธ์ ๊ฐ ์๋ค๊ณ ํ๋๋ฐ, ํ ์คํธ ๋์์ธ CSA2020Controller ์์๋ ํด๋น ( CSACommonCodeController ) ๋น์ ์ฌ์ฉํ๋ ๋ถ๋ถ์ด ์๋ค. ์ฆ ํ ์คํธ ๋์ ๋ฒ์๋ฅผ ๋์ด์ ์์ญ๊น์ง ๋์ฌ๋ค์ด์ ๋น์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ผ ์๊ฐํ๋ค.
ํด๊ฒฐ
@WebMvcTest ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ํ ์คํธํ ํน์ ์ปจํธ๋กค๋ฌ ํด๋์ค๋ฅผ ๋ช ์ํด์ผ ํ๋ค. ์ฆ ์์ ์๋ฌ๊ฐ ๋ฐ์ํ ์ด์ ๋ @WebMvcTest ์ด๋ ธํ ์ด์ ์ ์ธ์๋ก ์ปจํธ๋กค๋ฌ ํด๋์ค๋ฅผ ๋ช ์ํ์ง ์์๊ธฐ ๋๋ฌธ์ด๋ค.
@RunWith(SpringRunner.class)
@WebMvcTest(CSA2020Controller.class) // ๋ณ๊ฒฝ๋ ๋ถ๋ถ
@ExtendWith(MockitoExtension.class)
public class CSA2020ControllerTest extends TestCase {
@Autowired
private MockMvc mvc;
@MockBean
private CSA2020Service csa2020Service;
@Test
public void test1() throws Exception {
CSA2020MstParams params = new CSA2020MstParams();
params.setPrsc_cndt_dept_cd("1131");
params.setPrsc_date("20220118");
ObjectMapper objMapper = new ObjectMapper();
String json = objMapper.writeValueAsString(params);
mvc.perform(post("/CSA2020/CSA2020SelectMst")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
).andExpect(status().isOk());
}
}
'๐ TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค ์ฐ์ ์คํธ๋ฆญ 106์ผ ๋ฌ์ฑ ๋ฐ ์ ์ ์ค๋จ (0) | 2022.03.02 |
---|---|
git ๋ช ๋ น์ด ๋ณ๊ฒฝ / ์ปค์คํฐ๋ง์ด์ง (feat. alias) (0) | 2022.02.15 |
์๋๋ฆฌ์ค๋ณ git ๋ช ๋ น์ด (0) | 2021.12.28 |
211216 ๊ฐ๋ฐ๊ธฐ๋ก: exception ์ด catch ๋๋ ๊ณผ์ (feat. instanceOf) (0) | 2021.12.16 |
211214 ๊ฐ๋ฐ๊ธฐ๋ก: URL Pattern /* ์ /** ์ฐจ์ด (0) | 2021.12.14 |