단위 테스트로 디버깅
단위 테스트로 디버깅 (Debug with Unit Test)
일반 함수를 테스트하듯이 Pulsar Functions도 입력·출력이 있는 하나의 함수로 보고 단위 테스트로 검증할 수 있어요. 이 문서에서는 Java 함수를 TestNG로 단위 테스트하는 간단한 예시를 보여 줘요.
출처: 문서
본문
입력과 출력이 있는 다른 함수처럼, Pulsar Functions도 다른 함수를 테스트하는 것과 비슷한 방식으로 테스트할 수 있어요.
참고 Pulsar는 테스트에 TestNG를 사용해요.
예를 들어 Java의 언어 네이티브 인터페이스로 작성된 다음 함수가 있다고 해볼게요:
import java.util.function.Function;
public class JavaNativeExclamationFunction implements Function<String, String> {
@Override
public String apply(String input) {
return String.format("%s!", input);
}
}
함수를 테스트하는 간단한 단위 테스트를 작성할 수 있어요.
@Test
public void testJavaNativeExclamationFunction() {
JavaNativeExclamationFunction exclamation = new JavaNativeExclamationFunction();
String output = exclamation.apply("foo");
Assert.assertEquals(output, "foo!");
}
다음 예시는 Java SDK로 작성된 함수예요.
import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Function;
public class ExclamationFunction implements Function<String, String> {
@Override
public String process(String input, Context context) {
return String.format("%s!", input);
}
}
이 함수를 테스트하려면 단위 테스트를 작성하고 다음과 같이 Context 파라미터를 mock할 수 있어요.
@Test
public void testExclamationFunction() {
ExclamationFunction exclamation = new ExclamationFunction();
String output = exclamation.process("foo", mock(Context.class));
Assert.assertEquals(output, "foo!");
}
더 알아보기 (Learn more)
- Pulsar Functions 디버깅 개요 — 다른 디버깅 방법을 확인해요.
- localrun 디버깅 — 실제 클러스터에 연결해 디버깅해요.
- Functions 컨텍스트 — 함수 컨텍스트 객체를 익혀요.