'How to mock an object with Guice @Inject?

I have a class A and a class B.

public class A{
    @Inject // Guice Inject
    private B b;
}

The test class looks like -

public class ATest{
    private B b;
    
    private A a;

    @Before
    public void setup() {
        b = Mockito.mock(b.class);
        a = new A();
    }
}

The thing is that class B isn't getting mocked. In the test class, there's a NullPointerException whenever a method of B is getting invoked. I can't make changes to class A. Please help me out on how to mock class B successfully?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source