[英]Using @autowire annotation in spring boot is throwing 'BeanCreationException'
[英]Spring Boot Test failing to autowire port with LocalServerPort annotation
我正在运行 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);
}
}
以下是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>
应用程序属性
server.port=8080
我遇到了同样的问题,下面的这组注释对我有用。 没有 @ContextConfiguration 端口是 0。
@ContextConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestBase {
@LocalServerPort
protected int port;
}
我有同样的问题。 这个解决方案对我有用:
@Service
public class TestService implements ApplicationListener<ServletWebServerInitializedEvent> {
private int localPort;
@Override
public void onApplicationEvent(final ServletWebServerInitializedEvent event) {
localPort = event.getWebServer().getPort();
}
}
您可以自动装配此服务,也可以直接在您的测试类中实现 ApplicationListener。 只是一个警告 - 在 bean 完全准备好后,在应用程序启动期间初始化 localPort 变量,因此它在 @PostConstruct 等中不可用。
您缺少对spring-boot-starter-web
的依赖。
在测试类的顶部添加@TestPropertySource(locations = "classpath:test.properties")
。 您是否尝试过@Value("${server.port}")
而不是@LocalServerPort
?
在这里您可以找到使用RANDOM_PORT
的示例。 你可以改变DEFINED_PORT
根本改变的价值webEnvironment
在这种情况下,变量的值serverPort
将是8080,默认的。
我有同样的问题,我正在使用junit5,我想你的问题应该是一样的。 只需执行以下操作:
这应该可以解决问题
我遇到了这个问题,调试后我发现端口初始化需要时间。 所以我像这样改变了我的测试用例:
@LocalServerPort
lateinit var port: Number
val baseUrl by lazy { "http://localhost:$port"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.