@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
@ContextConfiguration
//@RequestMapping("/banners")
public class BannerControllerTest {
private static Logger LOGGER = LoggerFactory.getLogger(BannerControllerTest.class);
MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private ObjectMapper objectMapper;
@Autowired
EntityManager entityManager;
@Autowired
BannerRepository bannerRepository;
//contentType
private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
@Before
public void setUp() throws Exception {
this.mockMvc = webAppContextSetup(webApplicationContext).build();
}
/* GET MAPPING */
@Test
public void getBannerListForAdmin() throws Exception {
//getBannerListForAdmin() {
mockMvc.perform(get("/banners/")
.contentType(contentType))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.model().attribute("result_code", "0000"))
.andExpect(model().attributeExists("data"))
.andDo(print());
}
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JUnitTest {
static int itemId = //default value;
/* POST MAPPING */
@Test
@WithMockCustomUser(memberId = "7779", password = "dldndgml1", roles = { "STORE_MEMBER" })
//개밥이 있는 회원으로 테스트, 개밥 사용
public void stage1_postItem() throws Exception {
//postItem(@RequestBody Item item) {
Item item = entityManager.find(Item.class, campaignId);
String content = objectMapper.writeValueAsString(item);
//테스트 결과를 MvcResult에 담는다.
MvcResult result = mockMvc.perform(post("/items")
.content(content)
.contentType(contentType))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(model().attributeExists("data"))
.andReturn();
//테스트 결과 값 중 Model에서 다른 테스트 메소드에서 사용 할 itemId 값을 가져온다.
ModelAndView mav = result.getModelAndView();
Item tempItem = (Item) mav.getModel().get("data");
int tempItemId = tempItem.getItemId();
itemId = tempItemId;
LOGGER.info("itemId : " + itemId);
}
}
//참고링크 : <https://donghyeon.dev/articles/2019-03/Spring-%EC%BB%A8%ED%8A%B8%EB%A1%A4%EB%9F%AC-%ED%85%8C%EC%8A%A4%ED%8A%B8%EB%A5%BC-%EC%9C%84%ED%95%9C-MockMvc>
//실행순서 : TestA -> TestB 또는 stage1_test -> stage2_test 등
//실행순서 참고링크 : [<https://lottogame.tistory.com/848>](<https://lottogame.tistory.com/848>)
ex) /Users/suhee/git/pugshop-api
test {
testLogging {
showStandardStreams = true
showCauses = true
showExceptions = true
showStackTraces = true
exceptionFormat = 'full'
maxGranularity = '0'
displayGranularity = '0'
}
}
//참고링크 : <https://blog.hkwon.me/gradle-test-task-logging/>
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.5"
}
jacocoTestReport {
reports {
html.enabled true
csv.enabled true
xml.enabled false
}
}