相关疑难解决方法(0)

Spring语境中的有状态bean和无状态bean

我正在通过其官方文档阅读spring,并且在一个地方,我来到了一个使用原型范围的所有statefull bean而不是无状态bean的单例.

我知道在EJB中有一些像statefull一样的无状态bean,但这不是他们在文档中提到的.

任何人都可以解释一下这在春天的状态和无状态bean的确切含义

提前致谢

spring

33
推荐指数
1
解决办法
5万
查看次数

如何使用Spring在单元测试中模拟远程REST API?

假设我在我的应用程序中创建了一个使用远程Web服务的简单客户端,该服务在某个URI上公开RESTful API /foo/bar/{baz}.现在我想对我的客户端进行单元测试,该客户端调用此Web服务.

理想情况下,在我的测试中,我想模拟我从Web服务获得的响应,给出一个特定的请求,如/foo/bar/123/foo/bar/42.我的客户端假设API实际上正在某个地方运行,所以我需要一个本地"Web服务"来开始运行http://localhost:9090/foo/bar我的测试.

我希望我的单元测试是自包含的,类似于使用Spring MVC Test框架测试Spring控制器.

一些简单客户端的伪代码,从远程API获取数字:

// Initialization logic involving setting up mocking of remote API at 
// http://localhost:9090/foo/bar

@Autowired
NumberClient numberClient // calls the API at http://localhost:9090/foo/bar

@Test
public void getNumber42() {
    onRequest(mockAPI.get("/foo/bar/42")).thenRespond("{ \"number\" : 42 }");
    assertEquals(42, numberClient.getNumber(42));
}

// ..
Run Code Online (Sandbox Code Playgroud)

使用Spring有哪些替代方案?

java rest unit-testing spring-test

33
推荐指数
4
解决办法
6万
查看次数

如何在运行时获取SPRING Boot HOST和PORT地址?

如何在运行时获取部署应用程序的主机和端口,以便我可以在我的java方法中使用它?

port host spring-boot

16
推荐指数
2
解决办法
3万
查看次数

如何使用属性server.port = 0运行spock测试时查找Spring Boot容器的端口

鉴于此条目application.properties:

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

这导致Spring Boot选择一个随机可用端口,并使用spock测试一个spring boot web应用程序,spock代码如何知道要打哪个端口?

正常注射如下:

@Value("${local.server.port}")
int port;
Run Code Online (Sandbox Code Playgroud)

不适用于spock.

testing port spock spring-boot

7
推荐指数
2
解决办法
1万
查看次数

SpringBoot测试中如何获取正在运行的服务器端口?

我正在使用 Spock 作为测试框架,使用 Apache Camel 路由为 Spring Boot 应用程序创建一些单元测试,并且我需要模拟来自另一个应用程序的响应。我为此制作了一个模拟控制器,但我需要将测试运行的端口注入到属性中。有没有办法获取正在运行测试的端口?

\n\n

我尝试过

\n\n
@LocalServerPort\nprivate int port \n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
@Autowired Environment environment;\nString port = environment.getProperty("local.server.port");\n
Run Code Online (Sandbox Code Playgroud)\n\n

但两者都返回 -1,我不知道\xc2\xb4t 是否有其他方法来获取端口

\n\n

我的测试配置了以下注释:

\n\n
@RunWith(SpringRunner)\n@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)\n@ActiveProfiles(\'test\')\n
Run Code Online (Sandbox Code Playgroud)\n\n

另外,有没有办法在文件中注入随机端口application-test.yml?理想情况下,我需要在我的application-test.yml文件中执行类似的操作:

\n\n
app:\n  service: localhost:${server.port}\n
Run Code Online (Sandbox Code Playgroud)\n\n

其中端口是运行测试的随机端口。

\n

java spring spring-boot

6
推荐指数
1
解决办法
4952
查看次数

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

我正在运行 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> …
Run Code Online (Sandbox Code Playgroud)

java spring-boot junit5 spring-boot-test

5
推荐指数
2
解决办法
7420
查看次数

Spring boot - 如何获取运行端口和IP地址

我在启动 spring 启动应用程序时通过 shell 脚本传递端口。想知道如何在应用程序中获取运行端口和系统IP地址以打印在日志文件中。

脚本:-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9890

java spring spring-boot

4
推荐指数
2
解决办法
2万
查看次数