220118 ๊ฐœ๋ฐœ๊ธฐ๋ก : SpringBoot MVC test code ๊ฐœ๋ฐœ ์‹œ ์ดˆ๊ธฐ ์ด์Šˆ

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());
    }

}