我正在尝试在 gitlab 中部署我的代码。
它有一个单元测试类来测试我的 Kafka 生产者。
它在本地正常运行。但是当我尝试在 gitlab 中构建它时,它会因为测试而失败。
com.project.kafka.KafkaUtilTest > classMethod FAILED
org.I0Itec.zkclient.exception.ZkTimeoutException
但是我在测试的任何地方都没有一个名为 classMethod 的测试。
为什么会发生这种情况以及如何解决?
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestApplication.class)
@ActiveProfiles("test")
@EmbeddedKafka
public class KafkaUtilTest {
private static String TOPIC = "PE";
@Autowired
KafkaUtil kafkaUtil;
@Autowired
ConfigurationProperties configProperties;
Request request;
Response response;
Consumer<String, Object> consumer;
HashMap<String, String> expectedHeaderValueMap;
@ClassRule
public static EmbeddedKafkaRule embeddedKafkarule = new EmbeddedKafkaRule(1, true, TOPIC);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.setProperty("spring.kafka.producer.bootstrap-servers",
embeddedKafkarule.getEmbeddedKafka().getBrokersAsString());
}
@Before
public void init() {
readFile("0211");
expectedHeaderValueMap = getExpectedHeaderValueMap();
Map<String, Object> consumerConfigs = new HashMap<>(
KafkaTestUtils.consumerProps("consumer", "true", embeddedKafkarule.getEmbeddedKafka()));
consumerConfigs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerConfigs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
consumer = new DefaultKafkaConsumerFactory<String, Object>(consumerConfigs).createConsumer();
List<String> topics = new ArrayList<>();
topics.add(TOPIC);
TopicPartition topicPartition1 = new TopicPartition(TOPIC, 0);
TopicPartition topicPartition2 = new TopicPartition(TOPIC, 1);
List<TopicPartition> topicPartitions = new ArrayList<TopicPartition>();
topicPartitions.add(topicPartition1);
topicPartitions.add(topicPartition2);
consumer.assign(topicPartitions);
consumer.seekToBeginning(topicPartitions);
}
@Test
public void testPublish() throws Exception {
kafkaUtil.publishToKafka(request, response);
kafkaUtil.kafkaTemplate.flush();
ConsumerRecord<String, Object> consumedRecord = KafkaTestUtils.getSingleRecord(consumer, TOPIC);
assertRecord(consumedRecord);
}
private void readFile(String testSequenceNo) {
ObjectMapper om = new ObjectMapper();
try {
request = om.readValue(
this.getClass().getClassLoader().getResourceAsStream(
"requests/request" + testSequenceNo + ".json"),
SchedulingCallRequestDTO.class);
} catch (IOException e) {
e.printStackTrace();
}
try {
response = om.readValue(this.getClass().getClassLoader().getResourceAsStream(
"responsesv2/response" + testSequenceNo + ".json"),
ScheduleOrderResponseDTOv2.class);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我的测试。
我删除了断言部分以减小大小