mercredi 8 avril 2015

Why is Gradle able to mock final classes?

The Mockito documentation says that you can't mock a final class and yet running such a test through Gradle works. Specifically, I have an unit test in an Android project that has the following:



@Mock
private ViewTreeObserver mockTreeObserver;
...
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
...
}


ViewTreeObserver is a final class.


I need to support a legacy development environment in Eclipse as well Android Studio. Studio and Gradle command-line using the wrapper both pass. Eclipse fails with



org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class android.view.ViewTreeObserver


As well as 'why?' I really need to know how to get Eclipse to pass if possible (without rewriting the test).


The various library versions are as follows:



  • gradle-2.2.1 (wrapper used by Studio and command-line)

  • mockito-core-1.9.5

  • robolectric-2.4

  • android-20 (unit test classpath configuration in Eclipse)


Aucun commentaire:

Enregistrer un commentaire