首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Micronaut @替换为声明性客户端。

Micronaut @替换为声明性客户端。
EN

Stack Overflow用户
提问于 2021-08-11 10:47:03
回答 1查看 209关注 0票数 0

我将使用Micronaut文档中的代码(声明式Http客户机)--我使用的是Spock

PetOperations.java

代码语言:javascript
运行
复制
@Validated
public interface PetOperations {
    @Post
    Single<Pet> save(@NotBlank String name, @Min(1L) int age);
}

我有一个声明式客户端:

代码语言:javascript
运行
复制
@Client("/pets") 
public interface PetClient extends PetOperations { 

    @Override
    Single<Pet> save(String name, int age); 
}

当我运行一个测试类时,我想调用(@)另一个类( PetDummy )而不是PetClient,PetDummy类位于我的测试文件夹中。

代码语言:javascript
运行
复制
@Primary
@Replaces(PetClient.class)
@Singleton
public class PetDummy implements PetOperations {

    @Override
    public Single<Pet> save(String name, int age) {
        Pet pet = new Pet();
        pet.setName(name);
        pet.setAge(age);
        // save to database or something
        return Single.just(pet);
    }
}

考试班:

代码语言:javascript
运行
复制
class PetTest extends Specification {

    @Shared
    @AutoCleanup
    ApplicationContext applicationContext = ApplicationContext.run();
    //EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start();

    PetClient client = applicationContext.getBean(PetOperations.class);


    def 'test' (){
        given: 'name and age'

        when:
        client.save("Hoppie", 1);

        then:
        noExceptionThrown()
    }
}

但是,最后的称为,我也尝试过使用@Factory注释,但没有成功

PetClient扩展了PetOperations和PetDummy实现了PetOperations,如果它们都实现了,那么使用@代替.

还有什么我可以试试的吗?

谢谢!

另一期:

现在它已经工作了,PetClient是我的PetService中的一个依赖项。当我测试我的PetService时,它仍然调用PetClient而不是PetDummy。

我想这与applicationContext有关,你会看到

PetService:

代码语言:javascript
运行
复制
PetService {
    @Inject
    PetClient client;

    buyFood(){
        //...
        Single<Pet> pet = client.save("Hoppie", 1));
    }
}

PerService测试:

代码语言:javascript
运行
复制
class PetServiceTest extends ApplicationContextSpecification {

    @Subject
    @Shared
    PetService petService = applicationContext.getBean(PetService)

    PetOperations client = applicationContext.getBean(PetOperations.class) //client is not used here

    def 'test' (){
        given:

        when:
        petService.buyFood()

        then:
        noExceptionThrown()
    }
}

我认为我需要从applicationContext中“进入”PetService,告诉“使用PetDummy”实现(在测试类中),因为ApplicationContextSpecification属于另一个模块。

ApplicationContextSpecification是:

代码语言:javascript
运行
复制
abstract class ApplicationContextSpecification extends Specification implements ConfigurationFixture {

    @AutoCleanup
    @Shared
    ApplicationContext applicationContext = ApplicationContext.run(configuration)

/*    def cleanup() {
        assert !hasLeakage()
    }*/

}

ConfigurationFixture包含数据库的属性(Hibernate)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-11 12:17:53

您已经在检索PetClient bean实现:

代码语言:javascript
运行
复制
PetClient client = applicationContext.getBean(PetOperations.class);

如果用适当的类型调用,那么应该提供替换虚拟bean实现:

代码语言:javascript
运行
复制
PetOperations client = applicationContext.getBean(PetOperations.class);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68740533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档