forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a SkipTestInjection annotation to disable injecting the test clas…
…s. This may be useful for outside rules that wants to inject the test class from some other Hilt component. This annotation may be used directly on the test class or on some other annotation, as typically outside rules would have some other code generators on their own to generate needed entry points. RELNOTES=Add @SkipTestInjection PiperOrigin-RevId: 608724405
- Loading branch information
1 parent
02d62d6
commit c40811e
Showing
9 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (C) 2024 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.testing; | ||
|
||
import static java.lang.annotation.RetentionPolicy.CLASS; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation used for skipping test injection in a Hilt Android Test. This may be useful if | ||
* building separate custom test infrastructure to inject the test class from another Hilt | ||
* component. This may be used on either the test class or an annotation that annotates | ||
* the test class. | ||
*/ | ||
@Retention(CLASS) | ||
@Target({ElementType.TYPE}) | ||
public @interface SkipTestInjection {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
javatests/dagger/hilt/android/testing/SkipTestInjectionAnnotationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (C) 2024 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.testing; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import javax.inject.Inject; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.annotation.Config; | ||
|
||
@HiltAndroidTest | ||
@SkipTestInjectionAnnotationTest.TestAnnotation | ||
@RunWith(AndroidJUnit4.class) | ||
@Config(application = HiltTestApplication.class) | ||
public final class SkipTestInjectionAnnotationTest { | ||
@Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); | ||
|
||
@SkipTestInjection | ||
@interface TestAnnotation {} | ||
|
||
@Inject String string; // Never provided, shouldn't compile without @SkipTestInjection | ||
|
||
@Test | ||
public void testCannotCallInjectOnTestRule() throws Exception { | ||
IllegalStateException exception = | ||
assertThrows( | ||
IllegalStateException.class, | ||
() -> rule.inject()); | ||
assertThat(exception) | ||
.hasMessageThat() | ||
.isEqualTo("Cannot inject test when using @TestAnnotation"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
javatests/dagger/hilt/android/testing/SkipTestInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (C) 2024 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.testing; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import javax.inject.Inject; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.annotation.Config; | ||
|
||
@HiltAndroidTest | ||
@SkipTestInjection | ||
@RunWith(AndroidJUnit4.class) | ||
@Config(application = HiltTestApplication.class) | ||
public final class SkipTestInjectionTest { | ||
@Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); | ||
|
||
@Inject String string; // Never provided, shouldn't compile without @SkipTestInjection | ||
|
||
@Test | ||
public void testCannotCallInjectOnTestRule() throws Exception { | ||
IllegalStateException exception = | ||
assertThrows( | ||
IllegalStateException.class, | ||
() -> rule.inject()); | ||
assertThat(exception) | ||
.hasMessageThat() | ||
.isEqualTo("Cannot inject test when using @SkipTestInjection"); | ||
} | ||
} |