Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » How correct method for test my mqtt client with Junit 5?
How correct method for test my mqtt client with Junit 5? [message #1823505] Fri, 27 March 2020 17:24
giuseppe ventura is currently offline giuseppe venturaFriend
Messages: 1
Registered: March 2020
Junior Member
I have doing a mqtt client with MqttPaho and work correctly,
I read from a topic and write in another topic.

Now I want test it with Junit5.. I don't understand well guide in internet (mockito??, moquette?).. in particular how mock producer or consumer.. I use configuration for consumer and producer class for example:
@Configuration
public class Consumer {
@Autowired MqttPahoClientFactory mqttClientFactory;
@Bean
public MessageChannel topicChannel(){
    return new DirectChannel();
}

@Bean
public MessageProducer mqttInbound() {
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
            Parameters.MQTT_CLIENT_ID, mqttClientFactory, Parameters.TOPICS[0]);
    adapter.setCompletionTimeout(5000);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(1);
    adapter.setOutputChannel(topicChannel());
    return adapter;
}
}

and
@Configuration
public class Producer {
    @Autowired MqttPahoClientFactory mqttClientFactory;
    @Bean
    public MessageChannel mqttOutboundChannel(){
        return new DirectChannel();
    }
    @MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
    public interface ProducerGateway {
        void sendToMqtt(String data, @Header(MqttHeaders.TOPIC) String topic);
    }
    @Bean
    @ServiceActivator(inputChannel = "mqttOutboundChannel")
    public MessageHandler mqttOutbound() {
        MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(
                Parameters.MQTT_CLIENT_ID,
                mqttClientFactory);
        messageHandler.setAsync(true);
        messageHandler.setLoggingEnabled(true);
        return messageHandler;
    }
}

@Configuration
public class MqttConfiguration {

    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions options = new MqttConnectOptions();
        options.setServerURIs(Parameters.BROKER_URIs);
        options.setUserName(Parameters.MQTT_USERNAME);
        options.setPassword(Parameters.MQTT_PASSWORD.toCharArray());
        factory.setConnectionOptions(options);
        return factory;
    }

}


I want test

Read on topic1,
write on topic2,
Test if broker is runner,
Test if topic1/topic2 exist..
There is a little example just for learn?


thanks in advance

Regards
Previous Topic:mqtts protocol?
Next Topic:On expire of MQTT message using Python paho library.
Goto Forum:
  


Current Time: Sat Apr 27 00:24:46 GMT 2024

Powered by FUDForum. Page generated in 0.02823 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top