Skip to content

Commit 42542ba

Browse files
authored
Don't use JUnit's MultipleViolationsException. (#3179)
* Don't use JUnit's MultipleViolationsException. It has special meaning in JUnit which results in only the first failure being printed as test failure. Also added an example strict mode test to FIS(disabled since there are violations). * add license.
1 parent a5f2f99 commit 42542ba

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed

firebase-installations/firebase-installations.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ dependencies {
5757
testImplementation 'org.mockito:mockito-core:2.25.0'
5858
testImplementation 'org.mockito:mockito-inline:2.25.0'
5959

60+
androidTestImplementation project(':integ-testing')
6061
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
6162
androidTestImplementation 'androidx.test:runner:1.2.0'
6263
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.installations;
16+
17+
import androidx.test.core.app.ApplicationProvider;
18+
import androidx.test.ext.junit.runners.AndroidJUnit4;
19+
import com.google.firebase.FirebaseApp;
20+
import com.google.firebase.FirebaseOptions.Builder;
21+
import com.google.firebase.testing.integ.StrictModeRule;
22+
import org.junit.Ignore;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
@RunWith(AndroidJUnit4.class)
28+
public class StrictModeTest {
29+
30+
@Rule public StrictModeRule strictMode = new StrictModeRule();
31+
32+
@Test
33+
@Ignore("enable once strict mode violations are addressed")
34+
public void initializingFirebaseInstallations_shouldNotViolateStrictMode() {
35+
strictMode.runOnMainThread(
36+
() -> {
37+
FirebaseApp app =
38+
FirebaseApp.initializeApp(
39+
ApplicationProvider.getApplicationContext(),
40+
new Builder()
41+
.setApiKey("api")
42+
.setProjectId("123")
43+
.setApplicationId("appId")
44+
.build(),
45+
"hello");
46+
app.get(FirebaseInstallationsApi.class);
47+
});
48+
}
49+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.testing.integ;
16+
17+
import org.junit.internal.Throwables;
18+
19+
import java.io.PrintStream;
20+
import java.io.PrintWriter;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
public class MultipleViolationsException extends Exception {
25+
private final List<Throwable> errors;
26+
27+
private MultipleViolationsException(List<Throwable> errors) {
28+
this.errors = new ArrayList<>(errors);
29+
}
30+
31+
@Override
32+
public String getMessage() {
33+
StringBuilder sb = new StringBuilder("There were " + errors.size() + " errors:");
34+
for (Throwable e : errors) {
35+
sb.append(String.format("%n %s(%s)", e.getClass().getName(), e.getMessage()));
36+
}
37+
return sb.toString();
38+
}
39+
40+
@Override
41+
public void printStackTrace() {
42+
for (Throwable e : errors) {
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
@Override
48+
public void printStackTrace(PrintStream s) {
49+
for (Throwable e : errors) {
50+
e.printStackTrace(s);
51+
}
52+
}
53+
54+
@Override
55+
public void printStackTrace(PrintWriter s) {
56+
for (Throwable e : errors) {
57+
e.printStackTrace(s);
58+
}
59+
}
60+
61+
public static void assertEmpty(List<Throwable> errors) throws Exception {
62+
if (errors.isEmpty()) {
63+
return;
64+
}
65+
if (errors.size() == 1) {
66+
throw Throwables.rethrowAsException(errors.get(0));
67+
}
68+
throw new MultipleViolationsException(errors);
69+
}
70+
}

integ-testing/src/main/java/com/google/firebase/testing/integ/StrictModeRule.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.concurrent.atomic.AtomicReference;
2828
import org.junit.rules.TestRule;
2929
import org.junit.runner.Description;
30-
import org.junit.runners.model.MultipleFailureException;
3130
import org.junit.runners.model.Statement;
3231

3332
/**
@@ -133,7 +132,7 @@ public void evaluate() throws Throwable {
133132
runGc();
134133
StrictMode.setVmPolicy(originalVmPolicy);
135134
}
136-
MultipleFailureException.assertEmpty(new ArrayList<>(violations));
135+
MultipleViolationsException.assertEmpty(new ArrayList<>(violations));
137136
}
138137
};
139138
}

0 commit comments

Comments
 (0)