Spring cloud Stream Solace Unit Test
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
-
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.
0 -
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;
}
0