This might be a very stupid question but bear with me. I was asked to make BlobServiceClient a static variable so that its loaded only once in the entire lifecycle of the program, so what I did is
private static BlobServiceClient BLOB_SERVICE_CLIENT = new BlobServiceClientBuilder()
.connectionString(CONNECTION_STRING)
.buildClient();
Now the issue is with testing, there are many unit test cases, which internally call the class where this static variable is, and since static variable are loaded at the start of the program, this should be mocked. So I use expectations to mock this for a unit test case.
@Capturing
BlobServiceClientBuilder blobServiceClientBuilder;
@Capturing
BlobServiceClient blobServiceClient;
public void mockBlobServiceClientBuilderStatic(){
new Expectations() {
{
new BlobServiceClientBuilder();
returns(blobServiceClientBuilder) ;
}
{
blobServiceClientBuilder.connectionString(anyString);
returns(blobServiceClientBuilder);
}
{
blobServiceClientBuilder.buildClient();
returns(blobServiceClient);
}
};
}
The problem is when I run the whole suite of test cases it throws an error saying
mockit.internal.MissingInvocation: Missing invocation of:
com.azure.storage.blob.BlobServiceClientBuilder#BlobServiceClientBuilder()
on mock instance: com.azure.storage.blob.BlobServiceClientBuilder@361cd35c
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: Missing invocation
at com.azure.storage.blob.BlobServiceClientBuilder.<init>(BlobServiceClientBuilder.java)
at com.org.proj.someClass.storage.BlobStorageHelperTest$1.<init>(BlobStorageHelperTest.java:33)
at com.org.proj.someClass.storage.BlobStorageHelperTest.mockBlobServiceClientBuilderStatic(BlobStorageHelperTest.java:31)
at com.org.proj.someClass.storage.BlobStorageHelperTest.notALargeCheckTest(BlobStorageHelperTest.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:498)
... 6 more
I understand that, when I run a single testcase, in that instance of JVM the static will be initialized using my mock, but if I run multiple in the same instance of JVM, my mocks in each testcase is redundant and throwing the error. How do I make single and multiple test case runs happy at the same time? I am left helpless in finding a solution.
I tried using reflection but I couldnt get it to work. Can I somehow check if this class is initialized so I can run the static mock block to mock it, else not run it if its mocked?
I didn't run your code, but I can say that the error occurs because your code creates a static field initialized at class loading time, which can cause issues with mocking frameworks and unit testing. The static initialization happens before test methods run, making it difficult for mocking frameworks to intercept and mock the BlobServiceClient creation.
Try Lazy initialization and see if that works.