Skip to content

Remove deprecated jUnit3 references from runner sample #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions runner/AndroidJunitRunnerSample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ android {
productFlavors {
}

useLibrary 'android.test.runner'

useLibrary 'android.test.base'
useLibrary 'android.test.mock'

testOptions {
devices {
// run with ../gradlew -P android.sdk.channel=3 nexusOneApi30DebugAndroidTest
nexusOneApi30 (com.android.build.api.dsl.ManagedVirtualDevice) {
// A lower resolution device is used here for better emulator performance
device = "Nexus One"
apiLevel = 30
// Also use the AOSP ATD image for better emulator performance
systemImageSource = "aosp-atd"
abi = "x86"
}
}
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import junit.framework.TestSuite;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
import org.junit.runner.RunWith;

import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnitRunner;
Expand All @@ -36,27 +38,23 @@
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static com.example.android.testing.androidjunitrunnersample.HintMatcher.withHint;

/**
* JUnit4 Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner}.
* This class uses the JUnit4 syntax for tests.
* <p>
* With the new AndroidJUnit runner you can run both JUnit3 and JUnit4 tests in a single test
* suite. The {@link AndroidRunnerBuilder} which extends JUnit's
* {@link AllDefaultPossibilitiesBuilder} will create a single {@link
* TestSuite} from all tests and run them.
* Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner}.
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class CalculatorInstrumentationTest {


/**
* Use {@link ActivityScenario} to create and launch of the activity.
* Use {@link ActivityScenarioRule} to create and launch of the activity before each test, and close
* it after each test.
*/
@Before
public void launchActivity() {
ActivityScenario.launch(CalculatorActivity.class);
}
@Rule
public ActivityScenarioRule<CalculatorActivity> mActivityScenarioRule
= new ActivityScenarioRule<>(CalculatorActivity.class);

@Test
public void noOperandShowsComputationError() {
Expand Down Expand Up @@ -105,5 +103,4 @@ private void performOperation(int btnOperationResId, String operandOne,
// Check the expected test is displayed in the Ui
onView(withId(R.id.operation_result_text_view)).check(matches(withText(expectedResult)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,45 @@

package com.example.android.testing.androidjunitrunnersample;

import junit.framework.TestSuite;

import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static com.example.android.testing.androidjunitrunnersample.HintMatcher.withHint;

import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnitRunner;
import android.test.ActivityInstrumentationTestCase2;

import static com.example.android.testing.androidjunitrunnersample.HintMatcher.withHint;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* JUnit3 Ui Tests for {@link CalculatorActivity} using the {@link AndroidJUnitRunner}. This class
* uses the Junit3 syntax for tests.
*
* <p> With the new AndroidJUnit runner you can run both JUnit3 and JUnit4 tests in a single test
* test suite. The {@link AndroidRunnerBuilder} which extends JUnit's {@link
* AllDefaultPossibilitiesBuilder} will create a single {@link TestSuite} from all tests and run
* them. </p>
* Ui Tests for {@link CalculatorActivity} operation hints using the {@link AndroidJUnitRunner}.
*/
@LargeTest
public class OperationHintInstrumentationTest
extends ActivityInstrumentationTestCase2<CalculatorActivity> {

private CalculatorActivity mActivity;

public OperationHintInstrumentationTest() {
super(CalculatorActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();

// Espresso does not start the Activity for you we need to do this manually here.
mActivity = getActivity();
}

public void testPreconditions() {
assertThat(mActivity, notNullValue());
}

@RunWith(AndroidJUnit4.class)
public class OperationHintInstrumentationTest {

/**
* Use {@link ActivityScenarioRule} to create and launch of the activity before each test, and close
* it after each test.
*/
@Rule
public ActivityScenarioRule<CalculatorActivity> mActivityScenarioRule
= new ActivityScenarioRule<>(CalculatorActivity.class);

@Test
public void testEditText_OperandOneHint() {
String operandOneHint = mActivity.getString(R.string.type_operand_one_hint);
String operandOneHint = getApplicationContext().getString(R.string.type_operand_one_hint);
onView(withId(R.id.operand_one_edit_text)).check(matches(withHint(operandOneHint)));
}

@Test
public void testEditText_OperandTwoHint() {
String operandTwoHint = mActivity.getString(R.string.type_operant_two_hint);
String operandTwoHint = getApplicationContext().getString(R.string.type_operant_two_hint);
onView(withId(R.id.operand_two_edit_text)).check(matches(withHint(operandTwoHint)));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.example.android.testing.androidjunitrunnersample.suite;

import com.example.android.testing.androidjunitrunnersample.CalculatorInstrumentationTest;
import com.example.android.testing.androidjunitrunnersample.OperationHintLegacyInstrumentationTest;
import com.example.android.testing.androidjunitrunnersample.OperationHintInstrumentationTest;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
Expand All @@ -26,5 +26,5 @@
* Runs all Junit3 and Junit4 Instrumentation tests.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorInstrumentationTest.class, OperationHintLegacyInstrumentationTest.class})
@Suite.SuiteClasses({CalculatorInstrumentationTest.class, OperationHintInstrumentationTest.class})
public class InstrumentationTestSuite {}
2 changes: 1 addition & 1 deletion runner/AndroidJunitRunnerSample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.agpVersion = "7.0.2"
ext.agpVersion = "7.1.0-alpha13"
repositories {
// Insert local test repo here
google()
Expand Down
2 changes: 1 addition & 1 deletion runner/AndroidJunitRunnerSample/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.useAndroidX=true
android.useAndroidX=true

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip