E
E
EVGENY T.2021-09-29 10:54:14
Java
EVGENY T., 2021-09-29 10:54:14

Is it possible to mock a local variable?

There is a class and a "problematic" method for testing:

MyClass() {

 public void someMethod() {
  private Problem problemVar = new ProblemVar();
 ...
 }

}


I can't run the tests as usual because problemVar can't initialize without a combat network environment. Is there a way to somehow lock problemVar?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2021-09-29
@Beshere

I will assume that it is possible, using PowerMock.

ProblemVar pvMock = mock(ProblemVar.class);
whenNew(ProblemVar.class).withNoArguments().thenReturn(pvMock);

PS
But it's more correct to make problemVar work in any environment, or such an environment could be easily mocked in tests.
Or use the method that allows you to set the ProblemVar instance in the MyClass class.
Or use in DI project

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question