問題描述
我正在使用空手道框架來測試我的休息服務,它工作得很好,但是我有服務消耗來自 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 類重用到其他空手道項目中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!