Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking a class instance affects the static instance initialization #2647

Open
k-rus opened this issue May 20, 2022 · 1 comment
Open

Mocking a class instance affects the static instance initialization #2647

k-rus opened this issue May 20, 2022 · 1 comment

Comments

@k-rus
Copy link

k-rus commented May 20, 2022

I have an unusual case with using Mockito and run into an interesting issue, which might be a feature of Java and Mockito or bug, but I wasn't able to figure it out. If it is not a bug, it would be great if you can answer and explain in my SO question.

I have a class with static instance of it and then I create a mock instance in my test, which results that the static instance is not initialized properly. Here relevant parts of the class definition code:

...
public class Schema implements SchemaProvider
{
    public static final Schema instance = new Schema();

    private volatile Keyspaces distributedKeyspaces = Keyspaces.none();

    private final Keyspaces localKeyspaces;

    private final boolean online;

    ...

    private Schema()
    {

        this.online = isDaemonInitialized();
        this.localKeyspaces = (FORCE_LOAD_LOCAL_KEYSPACES || isDaemonInitialized() || isToolInitialized())
                              ? Keyspaces.of(SchemaKeyspace.metadata(), SystemKeyspace.metadata())
                              : Keyspaces.none();
    }

    @VisibleForTesting
    public Schema(boolean online, Keyspaces localKeyspaces)
    {
        this.online = online;
        this.localKeyspaces = localKeyspaces;
    }
    ...

Then the project jar is used in another project where Schema is mocked in a test (Schema is not used before this):

Schema schema = Mockito.mock(Schema.class);

To my understanding this should not affect the static instance initialization, i.e., Schema.instance. However, I run into an issue that the static instance is not initialized correctly and its properties are null, i.e.:

assert Schema.instance.distributedKeyspaces == null;
assert Schema.instance.localKeyspaces == null;

I've found that I can workaround the initialization issue in my test project by loading it explicitly:

Class.forName(Schema.class.getName());
Schema schema = Mockito.mock(Schema.class);
// which gives me:
assert Schema.instance.distributedKeyspaces != null;
assert Schema.instance.localKeyspaces != null;

Java version: openjdk version "11.0.12" 2021-07-20

Mockito version: 3.5.0

@Delfic
Copy link

Delfic commented Feb 16, 2023

This is interesting as I have the opposite issue
Using Mockito.mockStatic(Schema.class) will initialize the class, which I don't want since I want to be able to mock static methods before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants