通过postman调用时模拟第三方api

问题描述 投票:0回答:2

我有一个控制器类,它公开 Get API 并返回人员信息:

@RestController
public class PersonController {

@Autowired
PersonService personService;


@GetMapping
public Person getPersonInfo() {
    return personService.getPerson();
}
}

在 personService 中,我正在连接到第三方并从此 API 获取人员信息。 现在我想使用邮递员在我的控制器中调用 getPersonInfo() API。在这种情况下我如何模拟第三方响应? 我知道如果您使用测试(集成或单元)和wiremock,这是可能的。 只使用邮递员来调用我的 API 怎么样?

java spring-boot rest mocking
2个回答
0
投票

您可以使用

@Profile
注释

public interface PersonService {
...
}

@Service
@Profile("default")
public class PersonServiceImpl implements PersonService {
...
}

@Service
@Profile("mock")
public class MockPersonService implements PersonService {
...
}

如上定义后,您可以使用

appliation.properties
选择配置文件。例如:

spring.profiles.active=mock

0
投票

我有同样的情况。我需要在较低的环境中虚拟一些服务。 我可以使用wiremock来模拟Spring容器中的服务器吗?(我的是一个Spring Boot应用程序,我的服务正在连接到第三方API)

© www.soinside.com 2019 - 2024. All rights reserved.