Вот пример, поясняющий это более подробно:
Класс для тестирования:
Код: Выделить всё
public class BuildResponse extends DoFn {
@ProcessElement
public void processElement(ProcessContext c, OutputReceiver out)
{
String element = c.element();
String output = response(element);
out.output(output);
}
private String response(String s) {
var time = TableUtil.getCurrentTS(); // I cannot see my mock object when I debug leading to incorrect output
return time + s;
}
}
Код: Выделить всё
public class TableUtil implements Serializable {
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH).withZone(ZoneId.of("UTC"));
public static String getCurrentTS(){
return dateTimeFormatter.format(Instant.now());
}
}
Код: Выделить всё
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class BuildResponseTest {
private static MockedStatic tableUtilMockedStatic;
private static String currentTime;
@BeforeAll
static void setUp() {
currentTime = Instant.now().toString().concat("test");
tableUtilMockedStatic = Mockito.mockStatic(TableUtil.class);
tableUtilMockedStatic.when(TableUtil::getCurrentTS).thenReturn(currentTime);
}
@AfterAll
static void tearDown() {
tableUtilMockedStatic.close();
}
@Test
public void BuildResponseProcessTest() throws Exception{
tableUtilMockedStatic.when(TableUtil::getCurrentTS).thenReturn(currentTime);
System.out.println("currentTime -> "+ currentTime);
System.out.println("table time -> "+ TableUtil.getCurrentTS()); // this gives the correct mocked output
TestPipeline p = TestPipeline.create().enableAbandonedNodeEnforcement(false);
String s = "Element";
PCollection input = p.apply(Create.of(s));
PCollection output = input.apply(ParDo.of(new BuildResponse()));
String expectedOutput = currentTime + s;
PAssert.that(output).containsInAnyOrder(expectedOutput);
p.run().waitUntilFinish(); // this when runs gives the incorrect output
}
}
Код: Выделить всё
java.lang.AssertionError: ParDo(BuildResponseNew)/ParMultiDo(BuildResponseNew).output:
Expected: iterable with items ["2024-10-22T05:13:02.035ZtestElement"] in any order
but: not matched: "2024-10-22T05:13:04.755ZElement"
Я также пытался использовать встроенный статический макет, но результат был тот же .
Я ожидал статического макета вызова TableUtil.getCurrentTS() в классе BuildResponse, как реализовать такую макетную стратегию.
Может кто-нибудь помочь мне с правильным подходом к тестированию таких конвейеров?
Изменить: вопрос, который пометил этот пост как дубликат, я также пробовал этот обходной путь, но был не умеет издеваться (также требует изменений в основном коде). Мой точный вопрос вращается вокруг того, нужно ли мне внести некоторые изменения в тестовый код, какие существуют возможности, чтобы мне не пришлось менять основной код.
Подробнее здесь: https://stackoverflow.com/questions/791 ... -beam-pipl