【问题标题】:Maven, JUnit and Dagger - Dagger module could not be loadedMaven、JUnit 和 Dagger - 无法加载 Dagger 模块
【发布时间】:2014-12-04 18:41:41
【问题描述】:

注意:我仅通过命令行构建/测试。

我在使用 Dagger 时遇到了问题,并且能够在一个非常小的测试项目中重现相同的问题。尝试在单元测试中使用 Dagger 时,运行“mvn clean test”时出现以下错误:

sanity(com.mycompany.app.AppTest): Module adapter for class com.mycompany.app.AppTest$TestModule could not be loaded. Please ensure that code generation was run for this module.

在应用程序中,Dagger 编译/构建正常,注入运行良好。唯一的问题是单元测试,我觉得单元测试并没有从 dagger-compiler 中获取结果,但不知道如何测试/修复这个问题。

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <build>
      <pluginManagement>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <configuration>
                      <source>1.7</source>
                      <target>1.7</target>
                      <compilerArgument>-proc:none</compilerArgument>
                  </configuration>
              </plugin>
          </plugins>
      </pluginManagement>
  </build>

  <dependencies>

    <dependency>
        <groupId>com.squareup.dagger</groupId>
        <artifactId>dagger</artifactId>
        <version>1.2.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.squareup.dagger</groupId>
        <artifactId>dagger-compiler</artifactId>
        <version>1.2.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

  </dependencies>
</project>

AppTest.java

package com.audible.hushpuppy.dagger;

import org.junit.Before;
import org.junit.Test;

import dagger.Module;
import dagger.ObjectGraph;

import static org.junit.Assert.assertNull;

public class AppTest {

    @Module(injects = AppTest.class)
    public class TestModule{
    }

    @Before
    public void setUp() throws Exception {
        ObjectGraph.create(new TestModule());
    }

    @Test
    public void sanity() throws Exception {
        assertNull(null);
    }
}

【问题讨论】:

  • 我认为你应该在课堂上至少注射一次。应该再提供一个 dagger 编译器的作用域

标签: maven junit dagger


【解决方案1】:

啊哈——显然注释处理在较早的提交中完全关闭,我完全错过了 pom 中的这一行:

<compilerArgument>-proc:none</compilerArgument>

【讨论】:

  • 如果这解决了你的问题,你可以接受你自己的答案:)
猜你喜欢
  • 2019-11-18
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多