0

I am trying to deploy my code in gitlab.

It has a unit test class to test my Kafka producer.

It runs properly in local. But when I try to build it in gitlab it gets failed because of a test.

com.project.kafka.KafkaUtilTest > classMethod FAILED
org.I0Itec.zkclient.exception.ZkTimeoutException

But I don't have a test called classMethod anywhere in the test.

Why is this happening and how to resolve this?

@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();
        }
    }

}

This is my test.

I have removed the assertion part to reduce the size

Shamil Puthukkot
  • 442
  • 2
  • 17
  • You should post the code you are trying to deploy to help us helping you – Sterconium Sep 23 '19 at 12:33
  • I believe there have to be more logs in JUnit report. Looks like there has been no connection established to Kafka in your `init()` method – Artem Bilan Sep 23 '19 at 13:09
  • @ArtemBilan The test runs succesfully in local everytime.Also in the gitlb failure is intermittent.So i dont think Kafka connection is the issue. Also if you havent noticed my test name is testPublish- that is the single test in this class. But the failure is for a test names classMethod. I dont have such a test anywhere in my whole code – Shamil Puthukkot Sep 23 '19 at 13:30
  • The `classMethod` in JUnit usually refers to the test class initialization. Would really be great to see more logs. I saw such an error in my tests in the past and logs let me to determine what is going on. Otherwise we can’t help you here – Artem Bilan Sep 23 '19 at 13:42
  • I get this error when i do a build in Gitlab. Unfortunately am not getting logs for the failed test other than those two lines i mentioned. – Shamil Puthukkot Sep 23 '19 at 13:44
  • From ZkTimeoutException i assume connection to the embedded zookeeper was not esablished.What can be the potential reasons for this? Does this have something to do with availability of ports? – Shamil Puthukkot Sep 23 '19 at 14:16

0 Answers0