Simple mock

Report a typo

Take a look at the following classes:

public class SomeClass {

    public int function() {
        return 42;
    }
}

and

public class MyClass {
    SomeClass instance;

    public MyClass(SomeClass instance) {
        this.instance = instance;
    }

    public void someMethod() {
        System.out.println(instance.function());
    }
}

What will someMethod print to the console if we run the following test?

import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class MyClassTest {
    private SomeClass someMock = mock(SomeClass.class);
    private MyClass object = new MyClass(someMock);

    @Test
    void test() {
        when(someMock.function()).thenReturn(10);

        object.someMethod();
    }
}
Enter a number
___

Create a free account to access the full topic