在Cucumber

时间:2017-11-09 17:15:16

标签: java maven dependency-injection cucumber picocontainer

我有4个步骤定义类和一组域对象类。 我的第一步定义类如下所示:

public class ClaimProcessSteps {
    Claim claim;

    public ClaimProcessSteps(Claim w){
        this.claim = w;
    }

    @Given("^a claim submitted with different enrolled phone's model$")
    public void aClaimSubmittedFromCLIENTSChannelWithDifferentEnrolledPhoneSModel() throws Throwable {
        claim = ObjMotherClaim.aClaimWithAssetIVH();
    }


}

我的索赔类看起来像这样:

public class Claim {
    private String claimType;
    private String clientName;                  
    private Customer caller;
    private List<Hold> holds;

    public Claim() {}

    public Claim(String claimType, String clientName, Customer caller) {
        this.claimType              =       claimType;
        this.clientName             =       clientName;
        this.caller                 =       caller;
    }

    public String getClaimType() {
        return claimType;
    }

我的第二步定义类如下:

public class CaseLookupSteps {
    Claim claim;

    public CaseLookupSteps(Claim w){
        this.claim = w;
    }

    @When("^I access case via (right|left) search$")
    public void iAccessCaseInCompassViaRightSearch(String searchVia) throws Throwable {
       System.out.println(claim.getClaimType());
    }

我已经在我的POM.XML中导入了picocontainter依赖项,我收到以下错误。

对于&#39;类java.lang.String&#39;,3个可满足的构造函数太多了。构造函数列表:[(缓冲区),(构建器),()]

我的步骤定义类构造函数都没有接收基元作为参数。有没有人知道我为什么还会收到这个错误?可能是我的业务对象构造函数在其构造函数中期望String吗?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

Picocontainer不仅会查看您的步骤定义类来解决依赖关系。它还会查看您的步骤定义所依赖的所有类。

在这种情况下,它正在尝试解析非默认Claim构造函数的依赖关系。

 public Claim(String claimType, String clientName, Customer caller) {
    ...
 }

根据这个issue除了在所有依赖项中只保留默认构造函数之外,没有办法解决这个问题。

答案 1 :(得分:0)

假设您的方案如下:

Given some sort of claim
When I lookup this claim
Then I see  this claim

目前,您的测试缺少声明的设置步骤。

因此,除了直接在步骤之间共享声明对象之外,您应该仅使用默认构造函数创建ClaimService类。您可以将此服务注入步骤定义中。

注册服务后,您可以在Given some sort of claim的步骤定义中使用该服务来致电claimService.createSomeSortOfClaim()以创建声明。可以在内存,模拟数据库,实际数据库或其他持久性介质中创建此声明。

When I lookup this claim中,您可以使用claimService.getClaim()返回该声明,以便您可以使用其类型进行搜索。

这样做可以避免尝试让DI容器弄清楚应该如何创建测试声明。