Skip to content

Commit 9a9e354

Browse files
authored
Merge pull request #432 from DevDengChao/refactor/truth
Replace Assert.assertThat with Truth.assertThat
2 parents 9ee5a21 + 705b28d commit 9a9e354

File tree

10 files changed

+33
-29
lines changed

10 files changed

+33
-29
lines changed

runner/AndroidJunitRunnerSample/app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ dependencies {
5252
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion;
5353
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion;
5454
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion;
55+
56+
// https://truth.dev/
57+
androidTestImplementation 'com.google.truth:truth:' + rootProject.truthVersion;
5558
}

runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/CalculatorAddParameterizedTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,18 @@
1616

1717
package com.example.android.testing.androidjunitrunnersample;
1818

19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.runners.Parameterized.Parameters;
21+
22+
import androidx.test.filters.SmallTest;
23+
1924
import org.junit.Before;
2025
import org.junit.Test;
2126
import org.junit.runner.RunWith;
2227
import org.junit.runners.Parameterized;
2328

24-
import androidx.test.filters.SmallTest;
25-
26-
import java.lang.Iterable;
2729
import java.util.Arrays;
2830

29-
import static org.hamcrest.CoreMatchers.equalTo;
30-
import static org.hamcrest.CoreMatchers.is;
31-
import static org.junit.Assert.assertThat;
32-
import static org.junit.runners.Parameterized.Parameters;
33-
3431

3532
/**
3633
* JUnit4 tests for the calculator's add logic.
@@ -73,7 +70,7 @@ public static Iterable<Object[]> data() {
7370
* to reuse them in your tests.
7471
*/
7572
public CalculatorAddParameterizedTest(double operandOne, double operandTwo,
76-
double expectedResult) {
73+
double expectedResult) {
7774

7875
mOperandOne = operandOne;
7976
mOperandTwo = operandTwo;
@@ -88,7 +85,7 @@ public void setUp() {
8885
@Test
8986
public void testAdd_TwoNumbers() {
9087
double resultAdd = mCalculator.add(mOperandOne, mOperandTwo);
91-
assertThat(resultAdd, is(equalTo(mExpectedResult)));
88+
assertThat(resultAdd).isEqualTo(mExpectedResult);
9289
}
9390

9491
}

runner/AndroidJunitRunnerSample/app/src/androidTest/java/com/example/android/testing/androidjunitrunnersample/CalculatorTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616

1717
package com.example.android.testing.androidjunitrunnersample;
1818

19-
import org.junit.Before;
20-
import org.junit.Test;
21-
import org.junit.runner.RunWith;
19+
import static com.google.common.truth.Truth.assertThat;
2220

2321
import androidx.test.ext.junit.runners.AndroidJUnit4;
2422
import androidx.test.filters.SmallTest;
2523

26-
import static org.hamcrest.CoreMatchers.equalTo;
27-
import static org.hamcrest.CoreMatchers.is;
28-
import static org.junit.Assert.assertThat;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
2928

3029

3130
/**
@@ -45,25 +44,25 @@ public void setUp() {
4544
@Test
4645
public void addTwoNumbers() {
4746
double resultAdd = mCalculator.add(1d, 1d);
48-
assertThat(resultAdd, is(equalTo(2d)));
47+
assertThat(resultAdd).isEqualTo(2d);
4948
}
5049

5150
@Test
5251
public void subTwoNumbers() {
5352
double resultSub = mCalculator.sub(1d, 1d);
54-
assertThat(resultSub, is(equalTo(0d)));
53+
assertThat(resultSub).isEqualTo(0d);
5554
}
5655

5756
@Test
5857
public void subWorksWithNegativeResult() {
5958
double resultSub = mCalculator.sub(1d, 17d);
60-
assertThat(resultSub, is(equalTo(-16d)));
59+
assertThat(resultSub).isEqualTo(-16d);
6160
}
6261

6362
@Test
6463
public void divTwoNumbers() {
6564
double resultDiv = mCalculator.div(32d,2d);
66-
assertThat(resultDiv, is(equalTo(16d)));
65+
assertThat(resultDiv).isEqualTo(16d);
6766
}
6867

6968
@Test(expected = IllegalArgumentException.class)
@@ -74,7 +73,7 @@ public void divDivideByZeroThrows() {
7473
@Test
7574
public void mulTwoNumbers() {
7675
double resultMul = mCalculator.mul(32d, 2d);
77-
assertThat(resultMul, is(equalTo(64d)));
76+
assertThat(resultMul).isEqualTo(64d);
7877
}
7978

8079
}

runner/AndroidJunitRunnerSample/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ ext {
3232
runnerVersion = "1.5.0-alpha03"
3333
rulesVersion = "1.4.1-alpha06"
3434
espressoVersion = "3.5.0-alpha06"
35+
truthVersion = "1.1.3"
3536
}

runner/AndroidTestOrchestratorSample/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,7 @@ dependencies {
5050
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion;
5151
androidTestImplementation 'androidx.test:monitor:' + rootProject.monitorVersion;
5252
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion;
53+
androidTestImplementation 'com.google.truth:truth:' + rootProject.truthVersion;
5354
androidTestUtil 'androidx.test:orchestrator:' + rootProject.orchestratorVersion;
55+
5456
}

runner/AndroidTestOrchestratorSample/app/src/androidTest/java/com/example/android/testing/androidtestorchestratorsample/CalculatorAddParameterizedTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.example.android.testing.androidtestorchestratorsample;
1818

19+
import static com.google.common.truth.Truth.assertThat;
20+
1921
import org.junit.Before;
2022
import org.junit.Test;
2123
import org.junit.runner.RunWith;
@@ -26,9 +28,6 @@
2628
import java.lang.Iterable;
2729
import java.util.Arrays;
2830

29-
import static org.hamcrest.CoreMatchers.equalTo;
30-
import static org.hamcrest.CoreMatchers.is;
31-
import static org.junit.Assert.assertThat;
3231
import static org.junit.runners.Parameterized.Parameters;
3332

3433

@@ -88,6 +87,6 @@ public void setUp() {
8887
@Test
8988
public void testAdd_TwoNumbers() {
9089
double resultAdd = mCalculator.add(mOperandOne, mOperandTwo);
91-
assertThat(resultAdd, is(equalTo(mExpectedResult)));
90+
assertThat(resultAdd).isEqualTo(mExpectedResult);
9291
}
9392
}

runner/AndroidTestOrchestratorSample/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ ext {
3434
rulesVersion = "1.4.1-alpha06"
3535
espressoVersion = "3.5.0-alpha06"
3636
orchestratorVersion = "1.4.2-alpha03"
37+
truthVersion = "1.1.3"
3738
}

runner/AndroidTestOrchestratorWithTestCoverageSample/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ dependencies {
3434
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
3535
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
3636
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
37+
androidTestImplementation 'com.google.truth:truth:' + rootProject.truthVersion;
3738
androidTestUtil 'androidx.test:orchestrator:' + rootProject.orchestratorVersion
3839
androidTestUtil 'androidx.test.services:test-services:' + rootProject.testServicesVersion
40+
3941
}

runner/AndroidTestOrchestratorWithTestCoverageSample/app/src/androidTest/java/com/example/android/testing/androidtestorchestratorsample/CalculatorAddParameterizedTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.example.android.testing.androidtestorchestratorsample;
1818

19+
import static com.google.common.truth.Truth.assertThat;
20+
1921
import org.junit.Before;
2022
import org.junit.Test;
2123
import org.junit.runner.RunWith;
@@ -26,9 +28,6 @@
2628
import java.lang.Iterable;
2729
import java.util.Arrays;
2830

29-
import static org.hamcrest.CoreMatchers.equalTo;
30-
import static org.hamcrest.CoreMatchers.is;
31-
import static org.junit.Assert.assertThat;
3231
import static org.junit.runners.Parameterized.Parameters;
3332

3433

@@ -88,6 +87,6 @@ public void setUp() {
8887
@Test
8988
public void testAdd_TwoNumbers() {
9089
double resultAdd = mCalculator.add(mOperandOne, mOperandTwo);
91-
assertThat(resultAdd, is(equalTo(mExpectedResult)));
90+
assertThat(resultAdd).isEqualTo(mExpectedResult);
9291
}
9392
}

runner/AndroidTestOrchestratorWithTestCoverageSample/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ ext {
3333
rulesVersion = "1.4.1-alpha03"
3434
testServicesVersion = "1.4.1-alpha03"
3535
espressoVersion = "3.5.0-alpha03"
36+
truthVersion = "1.1.3"
3637
}

0 commit comments

Comments
 (0)