2

I am trying to mock a class with a method that has 3 arguments, and these 3 arguments could be any of its own types. I used the fflib_ApexMocks to return 'hello world' whenever the method is called; however, fflib_ApexMocks is returning null instead. I tried the following:

fflib_ApexMocks mocks = new fflib_ApexMocks();
ClassB classBMock = (ClassB) mocks.mock(ClassB .class);

mocks.startStubbing();
mocks.when(classBMock.doBStuff((Class3) fflib_Match.anyObject(), (DummyEnum) fflib_Match.anyObject(), fflib_Match.anyString())).thenReturn('Hello world');
ClassA = classA = new ClassA(classBMock);
mocks.stopStubbing();

As follows code example:

class classA {
    private ClassB classB;

    public classA(ClassB classB) {
        this.classB = classB;
    }

    public void doAStuff() {
        // ....
        String result = classB.doBStuff(new ClassC(), DummyEnum.PIGGY, 'Hi man');
        // ....

    }
}

class ClassB {
 public String doBStuff(ClassC classC, DummyEnum dummyEnum, String message) {
  // ...
  return 'bla';
 }
}

class ClassC {

}

public enum DummyEnum {
 DOG,
 KOALA,
 PIGGY
}

1 Answer 1

1

I solved my issue. Only after mocks.stopStubbing(), the classBMock.doBStuff will return 'Hi man'. Corrected code as follows:

fflib_ApexMocks mocks = new fflib_ApexMocks();
ClassB classBMock = (ClassB) mocks.mock(ClassB .class);

mocks.startStubbing();
mocks.when(classBMock.doBStuff((Class3) fflib_Match.anyObject(), (DummyEnum) fflib_Match.anyObject(), fflib_Match.anyString())).thenReturn('Hello world');
mocks.stopStubbing();

ClassA = classA = new ClassA(classBMock);
1
  • 1
    take a look at this answer for additional tips
    – cropredy
    Commented Dec 6, 2019 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .