Skip to content

Commit 6f26bdb

Browse files
author
Josh Deffibaugh
committed
Reworks tests to allow for weaker access
Android tests, or tests that run on real device, run in a different package from the test runner. This caused a lot of issues when using Mockito to mock dependencies that weren't public. This led to making a lot of things public that really shouldn't be. This diff moves some tests to /test and makes them pure unit tests. Pure unit tests run on a dev computers JVM and are in the same package as the code under test. Package private deps can be mocked this way. I only had to rework a little bit of actual source code. Most of these changes reduce method and class visibility only. Also, adds copyright everywhere and adds it to Intellij for the project so the copyright will automatically be added to new classes. Also, updates the testAllModules task to run the new unit tests in addition to the Android tests.
1 parent 0798078 commit 6f26bdb

File tree

56 files changed

+533
-569
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+533
-569
lines changed

.idea/.name

Lines changed: 0 additions & 1 deletion
This file was deleted.

.idea/copyright/Optimizely.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ config common for all modules.
3636
* `git clone [email protected]:optimizely/optimizely-ab-android-sdk.git`
3737
3. Create, or use an existing, Optimizely Custom Project
3838
* put a file in `test-app/src/main/res/values/git_ignored_strings.xml`
39-
* Give it this contents `<resources><string name="optly_project_id">{CUSTOM_RPOJECT_ID}</string></resources>`
39+
* Give it this contents `<resources><string name="optly_project_id">{CUSTOM_PROJECT_ID}</string></resources>`
4040
4. Build the project (from the project root)
4141
* `./gradlew assemble`
4242
5. Run tests for all modules

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/BackgroundWatchersCacheTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright 2016, Optimizely
33
* <p/>
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -40,16 +40,14 @@
4040
import static org.mockito.Mockito.when;
4141

4242
/**
43-
* Created by jdeffibaugh on 8/2/16 for Optimizely.
44-
*
4543
* Tests for {@link BackgroundWatchersCache}
4644
*/
4745
@RunWith(JUnit4.class)
4846
public class BackgroundWatchersCacheTest {
4947

50-
BackgroundWatchersCache backgroundWatchersCache;
51-
Cache cache;
52-
Logger logger;
48+
private BackgroundWatchersCache backgroundWatchersCache;
49+
private Cache cache;
50+
private Logger logger;
5351

5452
@Before
5553
public void setup() {

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/DataFileCacheTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright 2016, Optimizely
33
* <p/>
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -42,21 +42,18 @@
4242
import static org.mockito.Mockito.when;
4343

4444
/**
45-
* Created by jdeffibaugh on 8/2/16 for Optimizely.
46-
*
4745
* Tests for {@link DataFileCache}
4846
*/
4947
@RunWith(AndroidJUnit4.class)
5048
public class DataFileCacheTest {
5149

52-
DataFileCache dataFileCache;
53-
Cache cache;
54-
Logger logger;
50+
private DataFileCache dataFileCache;
51+
private Logger logger;
5552

5653
@Before
5754
public void setup() {
5855
logger = mock(Logger.class);
59-
cache = new Cache(InstrumentationRegistry.getTargetContext(), logger);
56+
Cache cache = new Cache(InstrumentationRegistry.getTargetContext(), logger);
6057
dataFileCache = new DataFileCache("1", cache, logger);
6158
}
6259

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/DataFileClientTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright 2016, Optimizely
33
* <p/>
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,6 +20,8 @@
2020

2121
import org.junit.Before;
2222
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
2325
import org.slf4j.Logger;
2426

2527
import java.io.IOException;
@@ -36,16 +38,15 @@
3638
import static org.mockito.Mockito.when;
3739

3840
/**
39-
* Created by jdeffibaugh on 8/2/16 for Optimizely.
40-
* <p/>
4141
* Tests for {@link DataFileClient}
4242
*/
43+
@RunWith(JUnit4.class)
4344
public class DataFileClientTest {
4445

45-
DataFileClient dataFileClient;
46-
Logger logger;
47-
Client client;
48-
HttpURLConnection urlConnection;
46+
private DataFileClient dataFileClient;
47+
private Logger logger;
48+
private Client client;
49+
private HttpURLConnection urlConnection;
4950

5051
@Before
5152
public void setup() {

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/DataFileReschedulerTest.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright 2016, Optimizely
33
* <p/>
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,13 +19,15 @@
1919
import android.content.Intent;
2020
import android.support.test.InstrumentationRegistry;
2121

22+
import com.optimizely.ab.android.shared.Cache;
23+
2224
import org.junit.Before;
2325
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
2428
import org.mockito.ArgumentCaptor;
2529
import org.slf4j.Logger;
2630

27-
import java.util.Arrays;
28-
2931
import static junit.framework.Assert.assertEquals;
3032
import static org.mockito.Matchers.any;
3133
import static org.mockito.Mockito.mock;
@@ -34,14 +36,13 @@
3436
import static org.mockito.Mockito.when;
3537

3638
/**
37-
* Created by jdeffibaugh on 8/2/16 for Optimizely.
38-
*
3939
* Tests for {@link DataFileRescheduler}
4040
*/
41+
@RunWith(JUnit4.class)
4142
public class DataFileReschedulerTest {
4243

43-
DataFileRescheduler dataFileRescheduler;
44-
Logger logger;
44+
private DataFileRescheduler dataFileRescheduler;
45+
private Logger logger;
4546

4647
@Before
4748
public void setup() {
@@ -83,30 +84,34 @@ public void receivedActionMyPackageReplaced() {
8384
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
8485
@Test
8586
public void dispatchingOne() {
86-
Context context = InstrumentationRegistry.getTargetContext();
8787
Context mockContext = mock(Context.class);
88-
BackgroundWatchersCache backgroundWatchersCache = mock(BackgroundWatchersCache.class);
89-
when(backgroundWatchersCache.getWatchingProjectIds()).thenReturn(Arrays.asList("1"));
88+
Cache cache = new Cache(InstrumentationRegistry.getTargetContext(), logger);
89+
BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger);
90+
backgroundWatchersCache.setIsWatching("1", true);
9091
Logger logger = mock(Logger.class);
9192
DataFileRescheduler.Dispatcher dispatcher = new DataFileRescheduler.Dispatcher(mockContext, backgroundWatchersCache, logger);
92-
Intent intent = new Intent(context, DataFileService.class);
93+
Intent intent = new Intent(mockContext, DataFileService.class);
9394
dispatcher.dispatch(intent);
9495
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
9596
verify(mockContext).startService(captor.capture());
9697
assertEquals("1", captor.getValue().getStringExtra(DataFileService.EXTRA_PROJECT_ID));
9798
verify(logger).info("Rescheduled data file watching for project {}", "1");
99+
cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME);
98100
}
99101

100102
@Test
101103
public void dispatchingMany() {
102-
Context context = InstrumentationRegistry.getTargetContext();
103104
Context mockContext = mock(Context.class);
104-
BackgroundWatchersCache backgroundWatchersCache = mock(BackgroundWatchersCache.class);
105-
when(backgroundWatchersCache.getWatchingProjectIds()).thenReturn(Arrays.asList("1", "2", "3"));
105+
Cache cache = new Cache(InstrumentationRegistry.getTargetContext(), logger);
106+
BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger);
107+
backgroundWatchersCache.setIsWatching("1", true);
108+
backgroundWatchersCache.setIsWatching("2", true);
109+
backgroundWatchersCache.setIsWatching("3", true);
106110
Logger logger = mock(Logger.class);
107111
DataFileRescheduler.Dispatcher dispatcher = new DataFileRescheduler.Dispatcher(mockContext, backgroundWatchersCache, logger);
108-
Intent intent = new Intent(context, DataFileService.class);
112+
Intent intent = new Intent(mockContext, DataFileService.class);
109113
dispatcher.dispatch(intent);
110114
verify(mockContext, times(3)).startService(any(Intent.class));
115+
cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME);
111116
}
112117
}

0 commit comments

Comments
 (0)