jeudi 22 novembre 2018

how to mock class in java without creating interface

First, I am not Java developer, I am using php.

I am curious in Java, for example if I have structure like this:

interface Ainterface {
    public String method();
}

public class A implements Ainterface {
    public String method() {
       //do something
    }
}

public class B {
    public String method(Ainterface a) {
       a.method();
       //do something
    }
}

Now if I want to test B's method I can mock a

public class Amock implements Ainterface {
    public String method() {
       //do something
    }
}

And inject it into B's method.

But, if I don't want to create interface and I have situation like this:

public class A {
    public String method() {
       //do something
    }
}

public class B {
    public String method(A a) {
       a.method();
       //do something
    }
}

Is there any way to mock a or test B's method in other way?

Aucun commentaire:

Enregistrer un commentaire