Update Robolectric and make broken tests compilable again

This commit is contained in:
serso 2017-08-17 17:04:43 +02:00
parent 5feca3f549
commit f6e520eb56
13 changed files with 100 additions and 63 deletions

View File

@ -100,7 +100,7 @@ dependencies {
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
testCompile 'net.sf.opencsv:opencsv:2.0' testCompile 'net.sf.opencsv:opencsv:2.0'
testCompile 'org.mockito:mockito-core:1.9.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.mockito:mockito-all:1.9.5'
testCompile 'org.skyscreamer:jsonassert:1.2.3' testCompile 'org.skyscreamer:jsonassert:1.2.3'
testCompile(name: 'org.apache.http.legacy', ext: 'jar') testCompile(name: 'org.apache.http.legacy', ext: 'jar')

View File

@ -28,6 +28,7 @@ import android.app.Application;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils; import android.text.TextUtils;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
@ -97,8 +98,11 @@ public class Editor {
} }
} }
@VisibleForTesting
@Nullable @Nullable
private final EditorTextProcessor textProcessor; EditorTextProcessor textProcessor;
@Nonnull
private final Engine engine;
@Nullable @Nullable
private AsyncHighlighter highlighterTask; private AsyncHighlighter highlighterTask;
@Nullable @Nullable
@ -107,12 +111,11 @@ public class Editor {
private EditorState state = EditorState.empty(); private EditorState state = EditorState.empty();
@Inject @Inject
Bus bus; Bus bus;
@Inject
Engine engine;
@Inject @Inject
public Editor(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull Engine engine) { 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() { public void init() {

View File

@ -2,12 +2,12 @@ package org.solovyev.android;
import android.os.Build; import android.os.Build;
import org.junit.runners.model.InitializationError; import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest; import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.Fs; 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 static final int SUPPORTED_SDK = Build.VERSION_CODES.LOLLIPOP;
public CalculatorTestRunner(Class<?> testClass) throws InitializationError { public CalculatorTestRunner(Class<?> testClass) throws InitializationError {

View File

@ -22,12 +22,14 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import static org.mockito.Mockito.mock;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
@ -38,10 +40,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static org.mockito.Mockito.mock; @Config(constants = BuildConfig.class)
@RunWith(value = RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(value = RobolectricGradleTestRunner.class)
public class AndroidEditorViewTest { public class AndroidEditorViewTest {
@Test @Test

View File

@ -22,14 +22,19 @@
package org.solovyev.android.calculator; 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 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 { public class CalculatorTest extends BaseCalculatorTest {
@Override @Override

View File

@ -25,7 +25,6 @@ package org.solovyev.android.calculator;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
@ -34,12 +33,12 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @Config(constants = BuildConfig.class)
@RunWith(value = RobolectricGradleTestRunner.class) @RunWith(value = RobolectricTestRunner.class)
public class EditorTest { public class EditorTest {
private Editor editor; private Editor editor;
@ -48,6 +47,9 @@ public class EditorTest {
public void setUp() throws Exception { public void setUp() throws Exception {
editor = new Editor(RuntimeEnvironment.application, mock(SharedPreferences.class), Tests.makeEngine()); editor = new Editor(RuntimeEnvironment.application, mock(SharedPreferences.class), Tests.makeEngine());
editor.bus = mock(Bus.class); editor.bus = mock(Bus.class);
// real text processor causes Robolectric to crash: NullPointerException at
// org.robolectric.res.ThemeStyleSet$OverlayedStyle.equals
editor.textProcessor = null;
} }
@Test @Test

View File

@ -22,14 +22,17 @@
package org.solovyev.android.calculator; 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.graphics.Color;
import android.os.Build; import android.os.Build;
import jscl.MathEngine;
import jscl.NumeralBase;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.text.TextProcessor;
import org.solovyev.android.calculator.view.TextHighlighter; 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.Date;
import java.util.Random; import java.util.Random;
import static org.junit.Assert.*; import jscl.MathEngine;
import jscl.NumeralBase;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(value = RobolectricGradleTestRunner.class) @RunWith(value = RobolectricTestRunner.class)
public class TextHighlighterTest { public class TextHighlighterTest {
private Engine engine; private Engine engine;

View File

@ -1,22 +1,24 @@
package org.solovyev.android.calculator; 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.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.robolectric.RuntimeEnvironment.application; 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.buttons.CppButton.four;
import static org.solovyev.android.calculator.WidgetReceiver.*;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) import android.content.Intent;
@RunWith(RobolectricGradleTestRunner.class)
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 { public class WidgetReceiverTest {
private WidgetReceiver widgetReceiver; private WidgetReceiver widgetReceiver;

View File

@ -24,8 +24,14 @@ package org.solovyev.android.calculator.model;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; 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.BaseCalculatorTest;
import org.solovyev.android.calculator.BuildConfig;
@Config(constants = BuildConfig.class)
@RunWith(RobolectricTestRunner.class)
public class ComparisonTest extends BaseCalculatorTest { public class ComparisonTest extends BaseCalculatorTest {
@Before @Before

View File

@ -22,19 +22,26 @@
package org.solovyev.android.calculator.model; 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.AngleUnit;
import jscl.MathEngine; import jscl.MathEngine;
import jscl.NumeralBase; import jscl.NumeralBase;
import jscl.math.Expression; import jscl.math.Expression;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.math.function.Constant; 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 { public class EvaluateTest extends BaseCalculatorTest {
@Before @Before

View File

@ -22,34 +22,40 @@
package org.solovyev.android.calculator.wizard; 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.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows; import org.robolectric.Shadows;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity; import org.robolectric.shadows.ShadowActivity;
import org.robolectric.util.ActivityController;
import org.solovyev.android.calculator.BuildConfig; import org.solovyev.android.calculator.BuildConfig;
import org.solovyev.android.wizard.Wizard; import org.solovyev.android.wizard.Wizard;
import org.solovyev.android.wizard.WizardUi; import org.solovyev.android.wizard.WizardUi;
import org.solovyev.android.wizard.Wizards; import org.solovyev.android.wizard.Wizards;
import javax.annotation.Nonnull;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import static org.junit.Assert.*; import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.choose_mode;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(value = RobolectricGradleTestRunner.class) @RunWith(value = RobolectricTestRunner.class)
public class CalculatorWizardActivityTest { public class CalculatorWizardActivityTest {
private ActivityController<WizardActivity> controller; private ActivityController<WizardActivity> controller;
@ -63,7 +69,6 @@ public class CalculatorWizardActivityTest {
activity = controller.get(); activity = controller.get();
wizards = new CalculatorWizards(RuntimeEnvironment.application); wizards = new CalculatorWizards(RuntimeEnvironment.application);
activity.setWizards(wizards); activity.setWizards(wizards);
controller.attach();
controller.create(); controller.create();
uiField = WizardActivity.class.getDeclaredField("wizardUi"); uiField = WizardActivity.class.getDeclaredField("wizardUi");
@ -156,7 +161,7 @@ public class CalculatorWizardActivityTest {
@Test @Test
public void testShouldStartWizardActivityAfterStart() throws Exception { public void testShouldStartWizardActivityAfterStart() throws Exception {
final ShadowActivity shadowActivity = Shadows.shadowOf(controller.get()); 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()); assertNotNull(shadowActivity.getNextStartedActivity());
} }

View File

@ -22,25 +22,27 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import android.os.Build; import android.os.Build;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; 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.annotation.Config;
import org.robolectric.util.ActivityController;
import org.solovyev.android.calculator.BuildConfig; import org.solovyev.android.calculator.BuildConfig;
import org.solovyev.android.wizard.WizardUi; import org.solovyev.android.wizard.WizardUi;
import javax.annotation.Nonnull;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import static org.junit.Assert.assertFalse; import javax.annotation.Nonnull;
import static org.junit.Assert.assertTrue;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricGradleTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class OnScreenCalculatorWizardStepTest { public class OnScreenCalculatorWizardStepTest {
@Nonnull @Nonnull

View File

@ -1,20 +1,21 @@
package org.solovyev.android.text.method; package org.solovyev.android.text.method;
import static org.junit.Assert.assertEquals;
import android.os.Build; import android.os.Build;
import android.text.Editable; import android.text.Editable;
import android.text.InputFilter; import android.text.InputFilter;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.solovyev.android.calculator.BuildConfig; import org.solovyev.android.calculator.BuildConfig;
import static org.junit.Assert.assertEquals;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricGradleTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class NumberInputFilterTest { public class NumberInputFilterTest {
private Editable editable; private Editable editable;