SpringCloudStream TestChannelBinderConfiguration Junits测试不支持Mockito

iszxjhcz  于 2023-06-22  发布在  Spring
关注(0)|答案(1)|浏览(84)
  1. 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>
  1. 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);
}

}
  1. SampleService.java
public interface SampleService {

public String convertToUpperCase(String str);

 }
  1. SampleServiceImpl.java
@Service
public class SampleServiceImpl  implements SampleService {

@Autowired
private SampleService2 sampleService2;

public String convertToUpperCase(String str) {
    str = sampleService2.convertStringToLowerCase(str);
    return str.toUpperCase();
}

}
  1. SampleService2.java
public interface SampleService2 {

public String convertStringToLowerCase(String str);
 }
  1. SampleService2Impl.java
@Service
public class SampleService2Impl implements SampleService2 {

public String convertStringToLowerCase(String str) {
    return str.toLowerCase();
}

}
  1. 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());
    }
}
}

这是我使用testBinder编写测试的示例springBootApplication,下面是我面临的问题。
在sampleConsumers.java
1.当@Component启用时,出现以下错误@Component(Enabled)//@EnableAutoConfiguration(Disabled)//@ComponentScan(“com.example.sampleproject.services“)(Disabled)
原因:com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException:请求中包含的安全令牌已过期(服务:AmazonDynamoDBv2;状态代码:400;错误代码:ExpiredTokenException;请求ID:*****************;代理:null)
1.当@Component被禁用且@EnableAutoConfiguration被启用时,出现以下错误//@Component(Disabled)@EnableAutoConfiguration(Enabled)
com.example.sampleproject. consumptions.SampleConsumer中的字段sampleService需要找不到类型为“com.example.sampleproject.services.SampleService”的bean。
进样点具有以下注解:- @org.springframework.beans.factory.annotation.Autowired(required=true)
由于:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“com.example.sampleproject.services.SampleService”的合格Bean可用:预期至少有1个符合autowire候选条件的bean。依赖关系注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
1.当@EnableAutoConfiguration和@ComponentScan正常工作时//@Component(Disabled)@EnableAutoConfiguration(Enabled)@ComponentScan(“com.example.sampleproject.services“)(Enabled)
a)为什么这个实现不使用@component?B)为什么我们总是需要使用@EnableAutoConfiguration和@ComponentScan注解?c)为什么mockito不使用这个?如何解决mockito问题?
1.根据mock,这个Assert应该失败,其中“hello”被替换为“mockedString”,但mock不起作用。例如:when(sampleService2.convertStringToLowerCase(ArgumentMatchers.any())).thenReturn(“mockedString”);
assertThat(target.receive().getPayload()).isEqualTo(“HELLO”.getBytes());
1.为什么这个实现不能与@Component一起工作,但它可以与@EnableAutoConfiguration和@ComponentScan注解一起工作?

6rqinv9w

6rqinv9w1#

它不应该与Mockito一起工作。TestChannelBinder设计用于基本单元/集成测试。

相关问题