如何将 util java 类重用到其他空手道项目中?

How to reuse util java class into other karate project?(如何将 util java 类重用到其他空手道项目中?)

本文介绍了如何将 util java 类重用到其他空手道项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用空手道框架来测试我的休息服务,它工作得很好,但是我有服务消耗来自 kafka 主题的消息然后坚持 mongo 最终通知 kafka.我在我的空手道项目中创建了一个 java 生产者,它由 js 调用以供功能使用.然后我有一个消费者来检查消息

I was working with karate framework to test my rest service and it work great, however I have service that consume message from kafka topic then persist on mongo to finally notify kafka. I made a java producer on my karate project, it called by js to be used by feature. Then I have a consumer to check the message

特点:

    * def kafkaProducer = read('../js/KafkaProducer.js')

JS:

function(kafkaConfiguration){
var Producer = Java.type('x.y.core.producer.Producer');
var producer = new Producer(kafkaConfiguration);
return producer;
}

Java:

public class Producer {

private static final Logger LOGGER = LoggerFactory.getLogger(Producer.class);

private static final String KEY = "C636E8E238FD7AF97E2E500F8C6F0F4C";
private KafkaConfiguration kafkaConfiguration;
private ObjectMapper mapper;
private AESEncrypter aesEncrypter;


public Producer(KafkaConfiguration kafkaConfiguration) {
    kafkaConfiguration.getProperties().put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
    kafkaConfiguration.getProperties().put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer");
    this.kafkaConfiguration = kafkaConfiguration;
    this.mapper = new ObjectMapper();
    this.aesEncrypter = new AESEncrypter(KEY);
}


public String produceMessage(String payload) {

  // Just notify kafka with payload and return id of payload
}

其他类

public class KafkaConfiguration {

private static final Logger LOGGER = LoggerFactory.getLogger(KafkaConfiguration.class);

private Properties properties;

public KafkaConfiguration(String host) {

    try {
        properties = new Properties();
        properties.put(BOOTSTRAP_SERVERS_CONFIG, host);
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "karate-integration-test");
        properties.put(ConsumerConfig.CLIENT_ID_CONFIG, "offset123");
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    } catch (Exception e) {
        LOGGER.error("Fail creating the consumer...", e);
        throw e;
    }
}

public Properties getProperties() {
    return properties;
}

public void setProperties(Properties properties) {
    this.properties = properties;
}
}

我想使用带有注释的生产者代码,就像黄瓜喜欢的那样:

I'd would like to use the producer code with anotation like cucumber does like:

@Then("^Notify kafka with payload (-?\d+)$")
    public void validateResult(String payload) throws Throwable {
        new Producer(kafkaConfiguration).produceMessage(payload);
    }

关于功能使用

Then Notify kafka with payload "{example:value}"

我想这样做是因为我想在基础项目中重用该代码,以便包含在其他项目中如果注释不起作用,也许您可​​以建议我另一种方法

I want to do that because I want to reuse that code on base project in order to be included in other project If annotation doesn't works, maybe you can suggest me another way to do it

推荐答案

答案很简单,使用普通的 Java/Maven 概念.将通用 Java 代码移动到主"包 (src/main/java).现在您需要做的就是构建一个 JAR 并将其作为依赖项添加到任何空手道项目中.

The answer is simple, use normal Java / Maven concepts. Move the common Java code to the "main" packages (src/main/java). Now all you need to do is build a JAR and add it as a dependency to any Karate project.

最后一个难题是:使用 classpath: 前缀来引用 JAR 中的任何特性或 JS 文件.空手道可以接他们.

The last piece of the puzzle is this: use the classpath: prefix to refer to any features or JS files in the JAR. Karate will be able to pick them up.

抱歉空手道不支持 Cucumber 或步骤定义.它有一个更简单的方法.详情请阅读:https://github.com/intuit/karate/issues/398

Sorry Karate does not support Cucumber or step-definitions. It has a much simpler approach. Please read this for details: https://github.com/intuit/karate/issues/398

这篇关于如何将 util java 类重用到其他空手道项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将 util java 类重用到其他空手道项目中?

基础教程推荐