From f6e520eb564fe40e685ade7f9d38634653a4a30a Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 17 Aug 2017 17:04:43 +0200 Subject: [PATCH] Update Robolectric and make broken tests compilable again --- app/build.gradle | 2 +- .../solovyev/android/calculator/Editor.java | 11 +++++--- .../android/CalculatorTestRunner.java | 4 +-- .../calculator/AndroidEditorViewTest.java | 12 ++++----- .../android/calculator/CalculatorTest.java | 17 +++++++----- .../android/calculator/EditorTest.java | 10 ++++--- .../calculator/TextHighlighterTest.java | 14 ++++++---- .../calculator/WidgetReceiverTest.java | 26 ++++++++++--------- .../calculator/model/ComparisonTest.java | 6 +++++ .../calculator/model/EvaluateTest.java | 17 ++++++++---- .../wizard/CalculatorWizardActivityTest.java | 21 +++++++++------ .../OnScreenCalculatorWizardStepTest.java | 14 +++++----- .../text/method/NumberInputFilterTest.java | 9 ++++--- 13 files changed, 100 insertions(+), 63 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 41af23cb..370370d7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -100,7 +100,7 @@ dependencies { testCompile 'junit:junit:4.12' testCompile 'net.sf.opencsv:opencsv:2.0' testCompile 'org.mockito:mockito-core:1.9.0' - testCompile 'org.robolectric:robolectric:3.0' + testCompile 'org.robolectric:robolectric:3.4.2' testCompile 'org.mockito:mockito-all:1.9.5' testCompile 'org.skyscreamer:jsonassert:1.2.3' testCompile(name: 'org.apache.http.legacy', ext: 'jar') diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java index cb7e00ed..833b0c62 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -28,6 +28,7 @@ import android.app.Application; import android.content.SharedPreferences; import android.os.AsyncTask; import android.support.annotation.NonNull; +import android.support.annotation.VisibleForTesting; import android.text.TextUtils; import com.squareup.otto.Bus; @@ -97,8 +98,11 @@ public class Editor { } } + @VisibleForTesting @Nullable - private final EditorTextProcessor textProcessor; + EditorTextProcessor textProcessor; + @Nonnull + private final Engine engine; @Nullable private AsyncHighlighter highlighterTask; @Nullable @@ -107,12 +111,11 @@ public class Editor { private EditorState state = EditorState.empty(); @Inject Bus bus; - @Inject - Engine engine; @Inject public Editor(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull Engine engine) { - textProcessor = new EditorTextProcessor(application, preferences, engine); + this.engine = engine; + this.textProcessor = new EditorTextProcessor(application, preferences, engine); } public void init() { diff --git a/app/src/test/java/org/solovyev/android/CalculatorTestRunner.java b/app/src/test/java/org/solovyev/android/CalculatorTestRunner.java index 7ccfbfa9..aebb4e0d 100644 --- a/app/src/test/java/org/solovyev/android/CalculatorTestRunner.java +++ b/app/src/test/java/org/solovyev/android/CalculatorTestRunner.java @@ -2,12 +2,12 @@ package org.solovyev.android; import android.os.Build; import org.junit.runners.model.InitializationError; -import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.robolectric.manifest.AndroidManifest; import org.robolectric.res.Fs; -public class CalculatorTestRunner extends RobolectricGradleTestRunner { +public class CalculatorTestRunner extends RobolectricTestRunner { public static final int SUPPORTED_SDK = Build.VERSION_CODES.LOLLIPOP; public CalculatorTestRunner(Class testClass) throws InitializationError { diff --git a/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java b/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java index 7ff80ed5..6473ffdb 100644 --- a/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java @@ -22,12 +22,14 @@ package org.solovyev.android.calculator; +import static org.mockito.Mockito.mock; + import android.content.SharedPreferences; -import android.os.Build; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.solovyev.common.text.Strings; @@ -38,10 +40,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import static org.mockito.Mockito.mock; - -@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(value = RobolectricGradleTestRunner.class) +@Config(constants = BuildConfig.class) +@RunWith(value = RobolectricTestRunner.class) public class AndroidEditorViewTest { @Test diff --git a/app/src/test/java/org/solovyev/android/calculator/CalculatorTest.java b/app/src/test/java/org/solovyev/android/calculator/CalculatorTest.java index 8568e342..10de410d 100644 --- a/app/src/test/java/org/solovyev/android/calculator/CalculatorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/CalculatorTest.java @@ -22,14 +22,19 @@ package org.solovyev.android.calculator; -import org.junit.Before; -import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.solovyev.android.calculator.calculations.CalculationFinishedEvent; - import static org.mockito.Mockito.doAnswer; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.solovyev.android.calculator.calculations.CalculationFinishedEvent; + +@Config(constants = BuildConfig.class) +@RunWith(RobolectricTestRunner.class) public class CalculatorTest extends BaseCalculatorTest { @Override diff --git a/app/src/test/java/org/solovyev/android/calculator/EditorTest.java b/app/src/test/java/org/solovyev/android/calculator/EditorTest.java index 88c26677..76fb3521 100644 --- a/app/src/test/java/org/solovyev/android/calculator/EditorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/EditorTest.java @@ -25,7 +25,6 @@ package org.solovyev.android.calculator; import static org.mockito.Mockito.mock; import android.content.SharedPreferences; -import android.os.Build; import android.support.annotation.NonNull; import com.squareup.otto.Bus; @@ -34,12 +33,12 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; -@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(value = RobolectricGradleTestRunner.class) +@Config(constants = BuildConfig.class) +@RunWith(value = RobolectricTestRunner.class) public class EditorTest { private Editor editor; @@ -48,6 +47,9 @@ public class EditorTest { public void setUp() throws Exception { editor = new Editor(RuntimeEnvironment.application, mock(SharedPreferences.class), Tests.makeEngine()); editor.bus = mock(Bus.class); + // real text processor causes Robolectric to crash: NullPointerException at + // org.robolectric.res.ThemeStyleSet$OverlayedStyle.equals + editor.textProcessor = null; } @Test diff --git a/app/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java b/app/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java index 35681947..1c71d053 100644 --- a/app/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java @@ -22,14 +22,17 @@ package org.solovyev.android.calculator; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import android.graphics.Color; import android.os.Build; -import jscl.MathEngine; -import jscl.NumeralBase; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.view.TextHighlighter; @@ -37,10 +40,11 @@ import org.solovyev.android.calculator.view.TextHighlighter; import java.util.Date; import java.util.Random; -import static org.junit.Assert.*; +import jscl.MathEngine; +import jscl.NumeralBase; @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(value = RobolectricGradleTestRunner.class) +@RunWith(value = RobolectricTestRunner.class) public class TextHighlighterTest { private Engine engine; diff --git a/app/src/test/java/org/solovyev/android/calculator/WidgetReceiverTest.java b/app/src/test/java/org/solovyev/android/calculator/WidgetReceiverTest.java index 5edc53a2..c990f0ae 100644 --- a/app/src/test/java/org/solovyev/android/calculator/WidgetReceiverTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/WidgetReceiverTest.java @@ -1,22 +1,24 @@ package org.solovyev.android.calculator; -import android.content.Intent; -import android.os.Build; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.robolectric.RobolectricGradleTestRunner; -import org.robolectric.annotation.Config; - import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.robolectric.RuntimeEnvironment.application; +import static org.solovyev.android.calculator.WidgetReceiver.ACTION_BUTTON_ID_EXTRA; +import static org.solovyev.android.calculator.WidgetReceiver.ACTION_BUTTON_PRESSED; +import static org.solovyev.android.calculator.WidgetReceiver.newButtonClickedIntent; import static org.solovyev.android.calculator.buttons.CppButton.four; -import static org.solovyev.android.calculator.WidgetReceiver.*; -@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(RobolectricGradleTestRunner.class) +import android.content.Intent; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@Config(constants = BuildConfig.class) +@RunWith(RobolectricTestRunner.class) public class WidgetReceiverTest { private WidgetReceiver widgetReceiver; diff --git a/app/src/test/java/org/solovyev/android/calculator/model/ComparisonTest.java b/app/src/test/java/org/solovyev/android/calculator/model/ComparisonTest.java index b72c6980..13e1b919 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/ComparisonTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/ComparisonTest.java @@ -24,8 +24,14 @@ package org.solovyev.android.calculator.model; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; import org.solovyev.android.calculator.BaseCalculatorTest; +import org.solovyev.android.calculator.BuildConfig; +@Config(constants = BuildConfig.class) +@RunWith(RobolectricTestRunner.class) public class ComparisonTest extends BaseCalculatorTest { @Before diff --git a/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java b/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java index 6c0b1fc3..fdc3494f 100644 --- a/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/model/EvaluateTest.java @@ -22,19 +22,26 @@ package org.solovyev.android.calculator.model; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.solovyev.android.calculator.BaseCalculatorTest; +import org.solovyev.android.calculator.BuildConfig; +import org.solovyev.android.calculator.jscl.JsclOperation; +import org.solovyev.android.calculator.variables.CppVariable; + import jscl.AngleUnit; import jscl.MathEngine; import jscl.NumeralBase; import jscl.math.Expression; import jscl.math.Generic; import jscl.math.function.Constant; -import org.junit.Before; -import org.junit.Test; -import org.solovyev.android.calculator.BaseCalculatorTest; -import org.solovyev.android.calculator.jscl.JsclOperation; -import org.solovyev.android.calculator.variables.CppVariable; +@Config(constants = BuildConfig.class) +@RunWith(RobolectricTestRunner.class) public class EvaluateTest extends BaseCalculatorTest { @Before diff --git a/app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivityTest.java b/app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivityTest.java index edfb8c10..4377b5c4 100644 --- a/app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivityTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/wizard/CalculatorWizardActivityTest.java @@ -22,34 +22,40 @@ package org.solovyev.android.calculator.wizard; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.choose_mode; + import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; -import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.Shadows; +import org.robolectric.android.controller.ActivityController; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowActivity; -import org.robolectric.util.ActivityController; import org.solovyev.android.calculator.BuildConfig; import org.solovyev.android.wizard.Wizard; import org.solovyev.android.wizard.WizardUi; import org.solovyev.android.wizard.Wizards; -import javax.annotation.Nonnull; import java.lang.reflect.Field; -import static org.junit.Assert.*; -import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.choose_mode; +import javax.annotation.Nonnull; @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(value = RobolectricGradleTestRunner.class) +@RunWith(value = RobolectricTestRunner.class) public class CalculatorWizardActivityTest { private ActivityController controller; @@ -63,7 +69,6 @@ public class CalculatorWizardActivityTest { activity = controller.get(); wizards = new CalculatorWizards(RuntimeEnvironment.application); activity.setWizards(wizards); - controller.attach(); controller.create(); uiField = WizardActivity.class.getDeclaredField("wizardUi"); @@ -156,7 +161,7 @@ public class CalculatorWizardActivityTest { @Test public void testShouldStartWizardActivityAfterStart() throws Exception { final ShadowActivity shadowActivity = Shadows.shadowOf(controller.get()); - WizardUi.startWizard(activity.getWizards(), CalculatorWizards.DEFAULT_WIZARD_FLOW, shadowActivity.getApplicationContext()); + WizardUi.startWizard(activity.getWizards(), CalculatorWizards.DEFAULT_WIZARD_FLOW, RuntimeEnvironment.application); assertNotNull(shadowActivity.getNextStartedActivity()); } diff --git a/app/src/test/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStepTest.java b/app/src/test/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStepTest.java index 5b8cd449..02a1bf0c 100644 --- a/app/src/test/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStepTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/wizard/OnScreenCalculatorWizardStepTest.java @@ -22,25 +22,27 @@ package org.solovyev.android.calculator.wizard; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import android.os.Build; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; -import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.android.controller.ActivityController; import org.robolectric.annotation.Config; -import org.robolectric.util.ActivityController; import org.solovyev.android.calculator.BuildConfig; import org.solovyev.android.wizard.WizardUi; -import javax.annotation.Nonnull; import java.lang.reflect.Field; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import javax.annotation.Nonnull; @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(RobolectricGradleTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class OnScreenCalculatorWizardStepTest { @Nonnull diff --git a/app/src/test/java/org/solovyev/android/text/method/NumberInputFilterTest.java b/app/src/test/java/org/solovyev/android/text/method/NumberInputFilterTest.java index f32c0282..45525436 100644 --- a/app/src/test/java/org/solovyev/android/text/method/NumberInputFilterTest.java +++ b/app/src/test/java/org/solovyev/android/text/method/NumberInputFilterTest.java @@ -1,20 +1,21 @@ package org.solovyev.android.text.method; +import static org.junit.Assert.assertEquals; + import android.os.Build; import android.text.Editable; import android.text.InputFilter; import android.text.SpannableStringBuilder; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import org.solovyev.android.calculator.BuildConfig; -import static org.junit.Assert.assertEquals; - @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(RobolectricGradleTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class NumberInputFilterTest { private Editable editable;