【问题标题】:Spring Boot Test failing to autowire port with LocalServerPort annotationSpring Boot Test 无法使用 LocalServerPort 注释自动连接端口
【发布时间】:2018-10-18 17:53:10
【问题描述】:

我正在运行 Spring Boot 2.0.1 和 Junit 5。我正在尝试在集成测试中获取端口。但是,端口值始终为零。我不确定是什么原因造成的。我尝试将 Web 环境枚举更改为随机端口,但似乎没有任何效果。

package com.example.demo;

import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplicationTests {

@LocalServerPort
private int port;
@Test


public void printPort() throws Exception {
     assertNotEquals(port, 0);
}
}

以下是pom(注意,只显示依赖关系)

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <junit-jupiter.version>5.2.0</junit-jupiter.version>
        <junit-platform.version>1.2.0</junit-platform.version>
    </properties>

    <dependencies>
         <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-launcher</artifactId>
                <scope>test</scope>
                <version>${junit-platform.version}</version>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-engine</artifactId>
                <scope>test</scope>
                <version>${junit-platform.version}</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <scope>test</scope>
                <version>${junit-jupiter.version}</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <scope>test</scope>
                <version>${junit-jupiter.version}</version>
            </dependency>
        <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>
    </dependencies>

application.properties

server.port=8080

【问题讨论】:

标签: java spring-boot junit5 spring-boot-test


【解决方案1】:

在您的测试类顶部添加@TestPropertySource(locations = "classpath:test.properties")。你试过@Value("${server.port}")而不是@LocalServerPort吗?

【讨论】:

    【解决方案2】:

    Here 你可以找到一个使用RANDOM_PORT 的例子。您可以更改为DEFINED_PORT,只需更改webEnvironment 的值,在这种情况下,变量serverPort 的值将是默认值8080。

    【讨论】:

      【解决方案3】:

      您缺少对 spring-boot-starter-web 的依赖。

      【讨论】:

        【解决方案4】:

        我有同样的问题。这个解决方案对我有用:

        @Service
        public class TestService implements ApplicationListener<ServletWebServerInitializedEvent> {
        
           private int localPort;
        
           @Override
           public void onApplicationEvent(final ServletWebServerInitializedEvent event) {
               localPort = event.getWebServer().getPort();
           }
        }
        

        您可以自动装配此服务,也可以直接在测试类中实现 ApplicationListener。只是一个警告 - localPort 变量在 bean 完全准备好后在应用程序启动期间被初始化,因此它在 @PostConstruct 等中不可用。

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题,下面的这组注释对我有用。没有@ContextConfiguration 的端口是0。

          @ContextConfiguration
          @RunWith(SpringRunner.class)
          @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
          public class TestBase {
          
          @LocalServerPort
          protected int port;
          
          }
          

          【讨论】:

          • JUnit5 中没有@RunWith
          • 在 JUnit5 中使用 @ExtendWith
          【解决方案6】:

          我有同样的问题,我正在使用junit5,我想你的问题应该是一样的。只需执行以下操作:

          • 去掉“import org.junit.Test”;
          • 重新导入“import org.junit.jupiter.api.Test”;

          这应该可以解决问题

          【讨论】:

            【解决方案7】:

            我遇到了这个问题,调试后发现端口初始化需要时间。所以我改变了我的测试用例:

            @LocalServerPort
            lateinit var port: Number
            
            val baseUrl by lazy {  "http://localhost:$port"}
            

            【讨论】:

              猜你喜欢
              • 2015-05-27
              • 1970-01-01
              • 1970-01-01
              • 2017-09-18
              • 2023-02-06
              • 1970-01-01
              • 2019-10-02
              • 1970-01-01
              • 2019-05-21
              相关资源
              最近更新 更多