Guice injection leaking into other tests

92 views Asked by At

I use Guice to instantiate a VocabularyAPI object for one of my unit tests unitTest1(). However, for another test (unitTest2()), I simply use mockito's @Mock annotation to mock an instance of the same class - VocabularyAPI.

I noticed that when I only run unitTest2() - mockito's mock setting for my VocabularyAPI is configured correctly. However, when I run the entire test suite (both unitTest1() and unitTest2()), both the tests are instantiated with the settings from the injector.

How can I limit the scope of the injected object to only inside the test that it is being injected? I want to be able to use the injected object in unitTest1() and mocked object for unitTest2().

1

There are 1 answers

0
Khalid On

Any problems in using local variables?

In unitTest1():

VocabularyAPI vocabularyAPI = // inject (I'm not familiar with Guice)

In unitTest2():

VocabularyAPI vocabularyAPI = Mockito.mock(VocabularyAPI.class);