Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为单元测试Spring云功能配置maven项目_Java_Spring_Maven_Spring Cloud_Spring Cloud Function - Fatal编程技术网

Java 如何为单元测试Spring云功能配置maven项目

Java 如何为单元测试Spring云功能配置maven项目,java,spring,maven,spring-cloud,spring-cloud-function,Java,Spring,Maven,Spring Cloud,Spring Cloud Function,使用功能和单元测试设置SpringCloud项目的正确方法是什么 我想用SpringCloud功能创建一个独立的web应用程序,并有可能通过单元测试来覆盖它。我已根据以下步骤进行设置: 我用springbootstarter父级创建了maven项目 在dependencyManagement中添加了spring云依赖项 添加了springcloudstarter功能webstarter 添加了测试依赖项: 将第5版的junit bom添加到dependencyManagement 添加了spr

使用功能和单元测试设置SpringCloud项目的正确方法是什么

我想用SpringCloud功能创建一个独立的web应用程序,并有可能通过单元测试来覆盖它。我已根据以下步骤进行设置:

  • 我用
    springbootstarter父级创建了maven项目
  • 在dependencyManagement中添加了
    spring云依赖项
  • 添加了
    springcloudstarter功能web
    starter
  • 添加了测试依赖项:
    • 将第5版的junit bom添加到dependencyManagement
    • 添加了
      springbootstarter测试
      starter,并从中排除了
      junit
    • 添加了
      junit-jupiter
      mockito-junit-jupiter
      依赖项
  • My
    FunctionaApplication
    包含bean
    大写字母
    作为函数,即来自文档的exmaple。我想做个测试

    @ExtendWith(SpringExtension.class)
    @函数SpringBootTest
    @自动配置WebTestClient
    类函数应用程序测试{
    @自动连线
    WebTestClient;
    @之前
    无效设置(){
    //设立
    }
    @试验
    void应调用函数viaweb(){
    client.post().uri(“/”).body(“foo”,String.class).exchange()
    .expectStatus().isOk().expectBody(String.class).isEqualTo(“FOO”);
    }
    }
    
    但它无法启动上下文并找到
    WebTestClient

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.file.FunctionsApplicationTest': Unsatisfied dependency expressed through field 'client'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.web.reactive.server.WebTestClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:393)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
        at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
        at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:98)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$5(ClassBasedTestDescriptor.java:337)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:342)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassBasedTestDescriptor.java:337)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
        at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
        at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:313)
        at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743)
        at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742)
        at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:647)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:336)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:259)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$2(ClassBasedTestDescriptor.java:252)
        at java.util.Optional.orElseGet(Optional.java:267)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$3(ClassBasedTestDescriptor.java:251)
        at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:29)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:106)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:105)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:69)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:107)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:107)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:75)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
        at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
        at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
        at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
        at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
        at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
        at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.web.reactive.server.WebTestClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
        ... 67 more
    
    
    以及条件评估报告中的一小部分:

    Unconditional classes:
    ----------------------
    
        org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
    
        org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration
    
        org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration
    
        org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
    
        org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration
    
        org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration
    
        org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration
    
        org.springframework.cloud.client.CommonsClientAutoConfiguration
    
        org.springframework.cloud.commons.httpclient.HttpClientConfiguration
    
        org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
    
    我做错了什么

    更新

    我猜
    @AutoConfigureWebTestClient
    WebTestClient

    Oleg的问题在于,您的问题在标题中有点不清楚,因为您似乎明确希望将您的函数部署为web端点并进行测试。这与“如何测试弹簧功能”不同。 不管怎样,我们可能需要更新我们的文档,以使其更清晰,但它就在这里

    假设这是您的应用程序:

    @SpringBootApplication
    public class SampleFunctionApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SampleFunctionApplication.class, args);
        }
    
        @Bean
        public Function<String, String> uppercase() {
            return v -> v.toUpperCase();
        }
    }
    
    POM中的依赖项段:

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        . . . .
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-web</artifactId>
            <version>3.0.1.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
    
    org.springframework.boot
    
    有关更多信息/示例

    Oleg,您的问题在标题中有点不清楚,因为您似乎明确希望将函数部署为web端点并进行测试。这与“如何测试弹簧功能”不同。 不管怎样,我们可能需要更新我们的文档,以使其更清晰,但它就在这里

    假设这是您的应用程序:

    @SpringBootApplication
    public class SampleFunctionApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SampleFunctionApplication.class, args);
        }
    
        @Bean
        public Function<String, String> uppercase() {
            return v -> v.toUpperCase();
        }
    }
    
    POM中的依赖项段:

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        . . . .
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-web</artifactId>
            <version>3.0.1.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
    
    org.springframework.boot
    
    有关更多信息/示例

    对不起,我们的示例不清楚。我创建了一些函数,将它们标记为bean,并希望通过单元测试来覆盖它们。此外,我还想检查它如何处理HTTP请求。我在以前版本的文档中看到,我可能会使用
    WebTestClient
    进行测试,但在我的示例中它不起作用,并且无法注入
    WebTestClient
    bean。感谢您的回复。这有点帮助。我可以做单元测试。不幸的是,
    testrestemplate
    没有提供像
    MockMvc
    WebTestClient
    这样的断言方法。在您的示例中,Spring测试是否在本地部署服务器?很抱歉,还不清楚。我创建了一些函数,将它们标记为bean,并希望通过单元测试来覆盖它们。此外,我还想检查它如何处理HTTP请求。我在以前版本的文档中看到,我可能会使用
    WebTestClient
    进行测试,但在我的示例中它不起作用,并且无法注入
    WebTestClient
    bean。感谢您的回复。这有点帮助。我可以做单元测试。不幸的是,
    testrestemplate
    没有提供像
    MockMvc
    WebTestClient
    这样的断言方法。在您的示例中,Spring测试是否在本地部署服务器?