EDIT: The method I'm testing calls this constant that's defined in another class, so I want to test that th emethod works independently of how the other class defines the constant. Mocking it was the first thing I could think of, but I'm open to other ideas for how to test it in a clean, secure way
(Class, method, and variable names are genericized)
I'm trying to figure out how to write a test. One of my methods gets a constant from another class, like so:
OtherClass.CONSTANT
and this constant is defined as:
public static final List<Long> CONSTANT =
ImmutableList.of(1, 2);
In the test for this method, I want to mock this call. I've tried
when(OtherClass.CONSTANT).thenReturn(ImmutableList.of(1, 2));
but that gives me this error:
RegularImmutableList cannot be returned by otherFunction()
otherFunction() should return String
otherFunction() being some other function in the code base that doesn't seem to be related to anything I've been working on.
I've also tried
doReturn(ImmutableList.of(1, 2)).when(OtherClass.CONSTANT);
but, as you might be guessing, it gives me this error:
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
I'm pretty lost as to how exactly I should be mocking this constant.