简体   繁体   English

Android Studio 中的 Dagger2 问题

[英]Dagger2 issue in Android Studio

I'm writing some JUnits for my Android app which use Dagger2.我正在为使用 Dagger2 的 Android 应用程序编写一些 JUnit。 To demonstrate my problem I simplified my test.为了证明我的问题,我简化了我的测试。 In gradle I have those dependencies related to Junits and dagger:在 gradle 我有那些与 Junits 和 dagger 相关的依赖项:

    //dependency injection
api "com.google.dagger:dagger:$dagger_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
api "com.google.dagger:dagger-android:$dagger_version"
api "com.google.dagger:dagger-android-support:$dagger_version"
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"

//unit testing
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//dagger testing
testImplementation "com.google.dagger:dagger:$dagger_version"
testAnnotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"

Here is my Test class这是我的测试 class

@RunWith(MockitoJUnitRunner.class)
public class Test extends TestCase {

  @Inject
  String greating;

  @Before
  public void setUp() {
    TestComponent component = DaggerTestComponent.builder().testModule(new TestModule()).build();
    component.inject(this);
  }

  @org.junit.Test
  public void test() {
      assertEquals("Hello!", greating);
  }
} 

Component for DI DI 组件

@Singleton
@Component(modules = {TestModule.class})
public interface TestComponent {
  void inject(Test test);
}

Test module for DI DI 测试模块

@Module
public class TestModule {
  @Singleton
  @Provides
  public String providesHello() {
    return "Hello!";
  }
}

My problem is that during the execution of the test, DaggerTestComponent is generated, but Android Studio underlines with red color the new TestModule() on line where is TestComponent component = DaggerTestComponent.builder().testModule(new TestModule()).build();我的问题是,在测试执行过程中,生成了 DaggerTestComponent,但是 Android Studio 用红色下划线new TestModule()在哪里是TestComponent component = DaggerTestComponent.builder().testModule(new TestModule()).build(); On hower it shows me testModule(TestModule) in builder cannot be aplied to mytestpackage.TestModule.然而,它告诉我构建器中的 testModule(TestModule) 不能应用于 mytestpackage.TestModule。 If I open generated DaggerTestComponent it look ok but contains error cannot resolve symbol TestModule on it's every occurence.如果我打开生成的 DaggerTestComponent 它看起来不错但包含错误无法解析符号 TestModule 在它的每一次出现。

DaggerTestComponent is generated under DaggerTestComponent 是在下面生成的

\app\build\generated\ap_generated_sources\debugUnitTest\out\mytestpackage

My test and related classes are located in我的测试和相关课程位于

\app\src\test\java\mytestpackage

Regardless this, the Test works fine and ends as passed .无论如何,测试工作正常并以pass结束。

How do I get rid off this annoying error?如何摆脱这个恼人的错误? Do I need to configure additional classpaths in gradle or set something else in AndroidStudio?我是否需要在 gradle 中配置其他类路径或在 AndroidStudio 中设置其他内容? Thanks in advance!提前致谢!

Finally problem solved by adding最后通过添加解决了问题

android.sourceSets {
  test.java.srcDirs += 
  [file("$buildDir/generated/ap_generated_sources/debugUnitTest/out")]
}

Thanks to Daivid for solution - https://stackoverflow.com/a/61329262/4345570感谢Daivid的解决方案 - https://stackoverflow.com/a/61329262/4345570

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM