java junit과 annotation 소개

simuruk wiki
1 @BeforeClass : 테스트 클래스 내에서 수행   번만 실행, static method 여야 
2 @AfterClass : 테스트 클래스 내에서 수행   번만 실행, static method 여야 
3 @Before : 테스트 케이스 수행  반복실행
4 @After : 테스트 케이스 수행  반복실행
5 @Test : 테스트 메소드 지정
6 public void StringTest2() throws Exception{
7     assertEquals(true,false);
8 }

junit spring integration testing

@SpringBootTest(classes = SomeApplication.class,
                webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AuthControllerTest {

    private static String token = null;

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate restTemplate;

    @Order(1)
    @Test
    @DisplayName("로그인")
    public void login() {

        HttpHeaders headers = new HttpHeaders();

        AuthTokenRequestDto authTokenRequestDto = new AuthTokenRequestDto();
        authTokenRequestDto.setId("someId");
        authTokenRequestDto.setPassword("somePassword");

        ResponseEntity<AuthTokenResponseDto> authTokenResponseDto
                = restTemplate.exchange("http://localhost:" + port + "/v1/auth/token",
                                        HttpMethod.POST,
                                        new HttpEntity<>(authTokenRequestDto, headers),
                                        AuthTokenResponseDto.class);

        assertEquals(authTokenResponseDto.getStatusCode(), HttpStatus.OK);
        assertNotNull(authTokenResponseDto.getBody().getToken());
        token = authTokenResponseDto.getBody().getToken();
    }

    @Order(2)
    @Test
    @DisplayName("내정보 보기")
    public void findUser() {

        HttpHeaders headers = new HttpHeaders();
        headers.add("SOME-TOKEN", token);

        ResponseEntity<String> authUser
                = restTemplate.exchange("http://localhost:" + port + "/v1/auth/user",
                                        HttpMethod.GET,
                                        new HttpEntity<>(headers),
                                        String.class);

        assertEquals(authUser.getStatusCode(), HttpStatus.OK);
        assertNotNull(authUser.getBody());
    }

    @Order(2)
    @Test
    @DisplayName("마케터의 파트너 불러오기")
    public void getPartnerByMarketer() {

        HttpHeaders headers = new HttpHeaders();
        headers.add("SOME-TOKEN", token);

        ResponseEntity<PartnerResponseDto> partnerResponseDto
                = restTemplate.exchange("http://localhost:" + port + "/v1/partners?marketerId={marketerId}",
                                        HttpMethod.GET,
                                        new HttpEntity<>(headers),
                                        PartnerResponseDto.class,
                                        "someData");

        then(partnerResponseDto.getStatusCode()).isEqualTo(HttpStatus.OK);
        then(partnerResponseDto.getBody().getPartnerDtoList().size()).isEqualTo(2);
    }
}

정리 필요

  • assertj
  • hamcrest
  • jupiter api
  • @ParameterizedTest

참고링크