[java] JUnitParams를 사용하여 암호 알고리즘을 테스트하는 방법은 어떻게 되나요?
먼저, 프로젝트의 의존성에 JUnitParams를 추가해야 합니다. 이를 위해 Maven을 사용하는 경우 pom.xml
파일에 다음 의존성을 추가합니다:
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
Gradle을 사용하는 경우, build.gradle
파일에 다음 의존성을 추가합니다:
testImplementation 'pl.pragmatists:JUnitParams:1.1.1'
이제 암호 알고리즘을 테스트하기 위해 매개 변수화된 테스트 메서드를 작성합니다. 예를 들어, encrypt
메서드를 테스트하는 경우 다음과 같이 작성할 수 있습니다:
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
@RunWith(JUnitParamsRunner.class)
public class EncryptionTest {
@Test
@Parameters({
"hello, uryyb",
"test, grfg",
"world, jbeyq"
})
public void encrypt_shouldReturnEncryptedText(String input, String expected) {
Encryption encryption = new Encryption();
String encryptedText = encryption.encrypt(input);
assertEquals(expected, encryptedText);
}
}
위의 예제에서 @RunWith(JUnitParamsRunner.class)
는 JUnitParams 러너를 사용하여 매개 변수화된 테스트를 실행하도록 지정합니다. @Parameters
주석으로 매개 변수를 지정하고, @Test
주석은 해당 메서드를 테스트 메서드로 표시합니다. 각 매개 변수는 테스트 메서드에 전달되고 assertEquals
를 통해 예상 결과와 비교됩니다.
위의 예제에서는 Encryption
클래스의 encrypt
메서드를 테스트하는 간단한 예시를 보여주었습니다. 매개 변수화된 테스트는 다양한 입력 값을 사용하여 반복적인 테스트를 쉽게 작성할 수 있습니다.
참고 자료:
- JUnitParams GitHub 레포지토리: https://github.com/Pragmatists/JUnitParams