mardi 8 janvier 2019

How do I get Mockito to mock a constant from another file?

(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 should be an ImmutableList.

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.

Aucun commentaire:

Enregistrer un commentaire