I have currently inherited an Android application that has zero code coverage, and my first job is to get some unit tests written for it. So I decided to use it as an opportunity to learn Robolectric.
However, I run the initial problems, getting two simple dummy tests to run.
Here is my code in the test file:
@Config(constants = BuildConfig.class)
@RunWith(RobolectricGradleTestRunner.class)
public class SplashActivityTest {
private SplashActivity activity;
@Before
public void setup() {
activity = Robolectric.setupActivity(SplashActivity.class);
}
@Test
public void dummy() {
String test = "POP!";
assertTrue("POP!",
test.equals("POP!"));
}
@Test
public void dummyTwo() {
String test = "POP!!";
assertTrue("POP!!",
test.equals("POP!!"));
}
}
The problem is that the action extends another class called baseactivity, and this class uses its own Application class.
In this custom application class, Picasso is created as a Singleton using the following code:
picasso = new Picasso.Builder(getApplicationContext()).downloader(new OkHttpDownloader(picassoClient)).build();
Picasso.setSingletonInstance(picasso);
When I run the tests, I get the following error:
java.lang.IllegalStateException: Singleton . com.squareup.picasso.Picasso.setSingletonInstance(Picasso.java:677)
, , , , . , , ? - ? Unit test, , , .
EDIT: "mock" Robolectric , , , .
, test/java :
public class TestMyApplication extends MyApplication
implements TestLifecycleApplication {
@Override
public void onCreate() {
super.onCreate();
initPicasso();
}
@Override
protected void initPicasso() {
}
@Override public void beforeTest(Method method) {
}
@Override public void prepareTest(Object test) {
}
@Override public void afterTest(Method method) {
}
}
, MyApplication, , @Override initPicasso, , , , , Picasso Singleton .
, , - Application , Robolectric , ?
:
@Config(constants = BuildConfig.class, application = TestMyApplication.class)
, , TestMyApplication, , Robolectric .