Java Testing with JUnit
LinkedIn Learning에서 Maaike van Putten의 강의를 듣고 작성하였습니다.
More Advanced Usage of JUnit
은행 클래스의 입금, 출금 메서드를 가지고 테스트를 진행한다.
공용 코
public class BankAccountParameterResolver implements ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
return pc.getParameter().getType() == BankAccount.class;
}
@override
public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
return new BankAccount(0, 0);
}
}@RepeatedTest
여러번 테스트 할때
연쇄로 일어나는 결과값을 테스팅할때
@ExtendWith(BankAccountParameterResolver.class)
public class BankAccountRepeatedTestTest {
@RepeatedTest
@DisplayName("Deposit 400 successfully")
public void testDeposit(BankAccount bankAccount) {
bankAccount.deposit( 500);
assertEquals( 500, bankAccount.getBalance());
}
}@ParameterizedTest
ints 안에 있는 값들이 차레로 은행 계좌에 입금하고 테스트.

CSV 형식으로 넣을 경우
외부 파일 지정할 때: @CsvSource(resources = “파일이름.csv”, delimiter = ‘;’)

간단한 다른 예제
요일이 둘다 T로 시작하니 모두 통과

@Timeout
무조건 실패하는 코드

더 간결하게 할경우 assertTimeout()
테스트 병렬 실행
@BeforeAll, @AfterAll
이 테스트 클래스에서 한번만 실행
아래 모두 통과 되는 코드
assert 되는 계좌 금액 변화를 확인하기
BeforeAll, AfterAll의 static을 지우면 통과를 하지 못한다.
클래스 레벨에서 @TestInstance(TestInstance.Lifecycle. PER_CLASS) 를 사용하면 된다.
@BeforeEach, @AfterEach
각 메서드 마다 한번씩 실행
Custom message and report
3번째 인수로 커스텀 메시지 작성.
리포트 하기 위해 Surefire 추가
surefire 명령창에 실행
target 폴더에 가면,

Condition에 따른 테스트
OS에 따라
자바 버전에 따라
JVM 시스템 프로퍼티에 기반하여 테스트를 실행하고 싶을 경우
@EnabledIfSystemProperty()
@DisabledIfSystemProperty()
@EnabledIfEnvironmentVariable()
Deprecated: @EnableIf, @DisableIf
@Disabled
테스트 비활성화
Last updated