Reputation: 11
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sample-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sample-project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- Spring Cloud Stream versions -->
<spring-cloud.version>2021.0.5</spring-cloud.version>
<spring-cloud-stream.version>3.2.7</spring-cloud-stream.version>
<spring-cloud-stream-kinesis.version>2.2.0</spring-cloud-stream-kinesis.version>
</properties>
<dependencies>
<!-- Spring Cloud Stream dependencies -->
<!-- start -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>${spring-cloud-stream.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
<version>${spring-cloud-stream.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kinesis</artifactId>
<version>${spring-cloud-stream-kinesis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<scope>test</scope>
<classifier>test-binder</classifier>
<type>test-jar</type>
<version>${spring-cloud-stream.version}</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springframework.kafka</groupId> -->
<!-- <artifactId>spring-kafka-test</artifactId> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>software.amazon.msk</groupId>
<artifactId>aws-msk-iam-auth</artifactId>
<version>1.1.6</version>
</dependency>
<!-- end -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<!-- Spring Cloud Stream dependency management -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
consumers.java
//@Component
@EnableAutoConfiguration
@ComponentScan("com.example.sampleproject.services")
public class SampleConsumer {
@Autowired
private SampleService sampleService;
@Bean
public Function<String, String> uppercase() {
return v -> sampleService.convertToUpperCase(v);
}
}
SampleService.java
public interface SampleService {
public String convertToUpperCase(String str);
}
SampleServiceImpl.java
@Service
public class SampleServiceImpl implements SampleService {
@Autowired
private SampleService2 sampleService2;
public String convertToUpperCase(String str) {
str = sampleService2.convertStringToLowerCase(str);
return str.toUpperCase();
}
}
SampleService2.java
public interface SampleService2 {
public String convertStringToLowerCase(String str);
}
SampleService2Impl.java
@Service
public class SampleService2Impl implements SampleService2 {
public String convertStringToLowerCase(String str) {
return str.toLowerCase();
}
}
SampleConsumerTests.java
@SpringBootTest
public class SampleConsumerTests {
@MockBean
private SampleService2 sampleService2;
@Test
public void sampleTest() {
when(sampleService2.convertStringToLowerCase(ArgumentMatchers.any()))
.thenReturn("mockedString");
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestChannelBinderConfiguration.getCompleteConfiguration(
SampleConsumer.class))
.run("--spring.cloud.function.definition=uppercase")) {
InputDestination source = context.getBean(InputDestination.class);
OutputDestination target = context.getBean(OutputDestination.class);
source.send(new GenericMessage<byte[]>("hello".getBytes()));
assertThat(target.receive().getPayload()).isEqualTo("HELLO".getBytes());
}
}
}
This is my sample springBootApplication where I written tests using testBinder, below are the issues i'm facing.
In sampleConsumers.java
Caused by: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The security token included in the request is expired (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ExpiredTokenException; Request ID: *****************; Proxy: null)
Field sampleService in com.example.sampleproject.consumers.SampleConsumer required a bean of type 'com.example.sampleproject.services.SampleService' that could not be found.
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.sampleproject.services.SampleService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
a) why this implementation not working with @component? b) why always we need to use @EnableAutoConfiguration and @ComponentScan annotations ? c) why mockito not working with this ? how to fix mockito issue ?
As per mock this assert should fail where "hello" is replaced with "mockedString", but mock not working. i.e: when(sampleService2.convertStringToLowerCase(ArgumentMatchers.any())) .thenReturn("mockedString");
assertThat(target.receive().getPayload()).isEqualTo("HELLO".getBytes());
why this Implementation won't work with @Component but it works with @EnableAutoConfiguration and @ComponentScan annotations ?
Upvotes: 1
Views: 863
Reputation: 6106
It is not supposed to work with Mockito. TestChannelBinder was designed for basic unit/integration testing.
Upvotes: 0