"java junit과 annotation 소개"의 두 판 사이의 차이

simuruk wiki
(참고링크)
 
82번째 줄: 82번째 줄:
 
         then(partnerResponseDto.getStatusCode()).isEqualTo(HttpStatus.OK);
 
         then(partnerResponseDto.getStatusCode()).isEqualTo(HttpStatus.OK);
 
         then(partnerResponseDto.getBody().getPartnerDtoList().size()).isEqualTo(2);
 
         then(partnerResponseDto.getBody().getPartnerDtoList().size()).isEqualTo(2);
 +
    }
 +
}
 +
</source>
 +
 +
==ParameterizedTest==
 +
<source lang="java">
 +
import org.junit.jupiter.params.ParameterizedTest;
 +
import org.junit.jupiter.params.provider.Arguments;
 +
import org.junit.jupiter.params.provider.MethodSource;
 +
import org.junit.jupiter.params.provider.ValueSource;
 +
 +
import java.util.stream.Stream;
 +
 +
public class DataDrivenTest {
 +
    private final ArithmeticOperation arithmeticOperation = new ArithmeticOperation();
 +
 +
    @ParameterizedTest
 +
    @ValueSource(ints = { 1, 2, 3, 4, 5 })
 +
    void canAdd(int b) {
 +
        assertTrue(arithmeticOperation.add(1, b) >= 2);
 +
    }
 +
 +
    @ParameterizedTest(name = "can add {0} to {1} and receive {2}")
 +
    @MethodSource("additionProvider")
 +
    void canAddAndAssertExactResult(int a, int b, int result) {
 +
        assertEquals(result, arithmeticOperation.add(a, b));
 +
    }
 +
 +
    static Stream<Arguments> additionProvider() {
 +
        return Stream.of(
 +
            Arguments.of(1, 3, 4),
 +
            Arguments.of(3, 4, 7),
 +
            Arguments.of(10, 20, 30)
 +
        );
 
     }
 
     }
 
}
 
}

2020년 3월 26일 (목) 02:36 기준 최신판

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

ParameterizedTest

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.stream.Stream;

public class DataDrivenTest {
    private final ArithmeticOperation arithmeticOperation = new ArithmeticOperation();

    @ParameterizedTest
    @ValueSource(ints = { 1, 2, 3, 4, 5 })
    void canAdd(int b) {
        assertTrue(arithmeticOperation.add(1, b) >= 2);
    }

    @ParameterizedTest(name = "can add {0} to {1} and receive {2}")
    @MethodSource("additionProvider")
    void canAddAndAssertExactResult(int a, int b, int result) {
        assertEquals(result, arithmeticOperation.add(a, b));
    }

    static Stream<Arguments> additionProvider() {
        return Stream.of(
            Arguments.of(1, 3, 4),
            Arguments.of(3, 4, 7),
            Arguments.of(10, 20, 30)
        );
    }
}

정리 필요

  • assertj
  • hamcrest
  • jupiter api
  • @ParameterizedTest

참고링크