Reputation: 1087
I am trying to write Junit test cases using Mockito. Whenever I am trying to use stubbing i.e use when and returnThen, I am getting a compile time error that the when is unidentified for that class. The following is my JUnit snippet
@Before
public void setUp() throws Exception {
registryIndexConfig = mock(RegistryIndexConfig.class);
when(registryIndexConfig.getIndexName()).thenReturn("Demogoblin");
}
I have imported the following classes, packages
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.testng.Assert;
import static org.mockito.Mockito.mock;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.stub;
Please can someone point what am I doing wrong here?
Upvotes: 0
Views: 282
Reputation: 20608
The methods mock
, when
, and many others are static methods in the Mockito
class.
You must import them when using unqualified (with a static import):
import static org.mockito.Mockito.*;
Alternatively, you could import the Mockito
class (like you did)
import org.mockito.Mockito;
and use the methods qualified:
@Before
public void setUp() throws Exception {
registryIndexConfig = Mockito.mock(RegistryIndexConfig.class);
Mockito.when(registryIndexConfig.getIndexName()).thenReturn("Demogoblin");
}
I prefer the static import way. It makes the test code look much cleaner.
Upvotes: 3