barbara
barbara

Reputation: 3201

How can I verify invocation of particular parent constructor?

I have a code of child class -

public class A extends B {
    public A(User user, UserFilter filter) {
        super(user, filter);
    }

    /* the second possible option
    public A(User user, UserFilter filter) {
        super(user, filter, null);
    }
    */
}

And the parent class -

public class B {
    private User user;
    private UserFilter filter;
    private QRCode code;

    public B(User user, UserFilter filter) {
        this.user = user;
        this.filter = filter;
    }

    public B(User user, UserFilter filter, QRCode code) {
        this.user = user;
        this.filter = filter;
        this.code = code;
    }
}

I want to verify that only specific parent constructor being invoked. Not super(user, filter, null) but super(user, filter). It's important that I can add field, so check for null for code is inappropriate.

I want to do that verification with PowerMockito.

Upvotes: 1

Views: 53

Answers (1)

talex
talex

Reputation: 20455

Make public B(User user, UserFilter filter, QRCode code) private and add static method which call it.

By doing so you will be able to instantiate your B class with any constructor. But descendants of B will be forced to use only visible constructor

Upvotes: 1

Related Questions