Spring Boot 测试无法使用 LocalServerPort 注释自动连接端口

atk*_*wa7 5 java spring-boot junit5 spring-boot-test

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

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);
}
}
Run Code Online (Sandbox Code Playgroud)

以下是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>
Run Code Online (Sandbox Code Playgroud)

应用程序属性

server.port=8080
Run Code Online (Sandbox Code Playgroud)

小智 6

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

@ContextConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestBase {

@LocalServerPort
protected int port;

}
Run Code Online (Sandbox Code Playgroud)


Gab*_*Leg 0

添加@TestPropertySource(locations = "classpath:test.properties")到您的测试类之上。你尝试@Value("${server.port}")过代替吗@LocalServerPort