🎄 Happy Holidays! 🥳

Most of Solace is closed December 24–January 1 so our employees can spend time with their families. We will re-open Thursday, January 2, 2024. Please expect slower response times during this period and open a support ticket for anything needing immediate assistance.

Happy Holidays!

Please note: most of Solace is closed December 25–January 2, and will re-open Tuesday, January 3, 2023.

Spring cloud Stream Solace Unit Test

kirthi
kirthi Member Posts: 22 ✭✭

Solace Spring Cloud BOM  2.4.0

Solace Spring Cloud Stream Starter 3.4.0

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-stream</artifactId>

<type>test-jar</type>

<scope>test</scope>

<classifier>test-binder</classifier>

</dependency>

<!-- https://mvnrepository.com/artifact/com.solace.spring.cloud/spring-cloud-starter-stream-solace -->

<dependency>

<groupId>com.solace.spring.cloud</groupId>

<artifactId>spring-cloud-starter-stream-solace</artifactId>

</dependency>

Class:

@Component

@EnableAutoConfiguration

public class MessageReceiver {

 @Bean

 @SuppressWarnings("unchecked")

 Function<Message<List<?>>, Collection<Message<String>>> receiveMessage() {

  return batchMsg -> {

   List<?> batchedPayloads = batchMsg.getPayload();

   ArrayList<Message<String>> msgList = new ArrayList<Message<String>>();

   // System.out.println("size " + batchedPayloads.get(0));

   

List<Map<String, Object>> batchedHeaders =

     (List<Map<String, Object>>)

       batchMsg.getHeaders().get(SolaceBinderHeaders.BATCHED_HEADERS);

   msgList.add(

       MessageBuilder.withPayload("dgjk").build());

   return msgList;

  };

 }

}

TestClass:


 @RunWith(SpringRunner.class)

 @SpringBootTest

 public class MessageReceiverTest {

 @Test

public void receiveMessageTest() throws InterruptedException {


try (ConfigurableApplicationContext context = new SpringApplicationBuilder(

TestChannelBinderConfiguration.getCompleteConfiguration(

MessageReceiver.class))

.run("--spring.cloud.function.definition=receiveMessage")) {

InputDestination source = context.getBean(InputDestination.class);

OutputDestination target = context.getBean(OutputDestination.class);

Message<?> msgInput = MessageBuilder.withPayload(inputObj).build();


assertNotNull(msgInput.toString());

source.send(msgInput);



target.receive().getHeaders();


assertNotNull(target.receive().getPayload());

}}


 }

 Error Log:

 

 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

at org.springframework.util.Assert.state(Assert.java:76)

at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:236)

at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:152)

at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:393)

Answers

  • giri
    giri Member, Administrator, Employee Posts: 116 admin

    Sorry, the unit tests are missing on this sample. From the exception, it seems like a spring/junit configuration issue, please refer to other samples and update the code and try.

    https://github.com/SolaceSamples/solace-samples-spring/blob/master/cloud-stream-processor/src/test/java/com/solace/samples/spring/scs/ConvertFtoCProcessorTest.java has a valid configuration that can be used.

  • kirthi
    kirthi Member Posts: 22 ✭✭
    edited March 2023 #3

    Hi, I am using TestChannelBinderConfiguration. I have resolved the dependency issues by adding other classes in the argument,

    new SpringApplicationBuilder(

    TestChannelBinderConfiguration.getCompleteConfiguration(

    MessageReceiver.class, xxx.class, yyy.class))

    in my yyy.class, i have mongotemplate and mongoclient used. How to mock them, Please suggest if any inputs.

    "Catching","thrown":{"commonElementCount":0,"localizedMessage":"Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]","message":"Timed out after 30000 ms while waiting to connect.


    How to mock this ?

    @Autowired

     public SafeStoreDaoImpl(MongoClient mongoClient) {

      this.mongoClient = mongoClient;

     }

  • giri
    giri Member, Administrator, Employee Posts: 116 admin

    This seems to be a mongo connection error - connection timeout while connecting, please check your mongo installation.

  • kirthi
    kirthi Member Posts: 22 ✭✭
    1. How to mock the mongo client in the above sample code ?

    @Autowired

     public SafeStoreDaoImpl(MongoClient mongoClient) {

      this.mongoClient = mongoClient;

     }

    2.Should we give connection details to use the real mongo db for integration testing?