当前位置: 首页 > 面试题库 >

即使在依赖项中,Maven也找不到org.junit

皮煜
2023-03-14
问题内容

我想在我的小型项目中添加一个测试(请注意,我从代码中删除了一些位并更改了程序包名称,因此,如果您看到与此有关的任何错误,则可能不是;)):

package my.pckg;


import static org.junit.Assert.*;

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


public class SignedRequestCallbackTest {


    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testCorrectSignedRequest() {
        assertTrue(false);
    }

}

(我也尝试从TestCase进行扩展以删除静态导入,但这没有帮助)

运行后mvn test,向我显示一个找不到org.junit的错误:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Test Server 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ server ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ server ---
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO] Compiling 9 source files to /Users/michael/Projects/fbmuc/server/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[4,27] package org.junit does not exist
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[6,16] package org.junit does not exist
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[7,16] package org.junit does not exist
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[8,16] package org.junit does not exist
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[14,9] cannot find symbol
symbol  : class Before
location: class my.pckgSignedRequestCallbackTest
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[18,9] cannot find symbol
symbol  : class After
location: class my.pckgSignedRequestCallbackTest
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[22,9] cannot find symbol
symbol  : class Test
location: class my.pckgSignedRequestCallbackTest
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[24,12] cannot find symbol
symbol  : method assertTrue(boolean)
location: class my.pckgSignedRequestCallbackTest
[INFO] 9 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.161s
[INFO] Finished at: Fri Feb 22 18:02:37 CET 2013
[INFO] Final Memory: 8M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project server: Compilation failure: Compilation failure:
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[4,27] package org.junit does not exist
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[4,4] static import only from classes and interfaces
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[6,16] package org.junit does not exist
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[7,16] package org.junit does not exist
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[8,16] package org.junit does not exist
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[14,9] cannot find symbol
[ERROR] symbol  : class Before
[ERROR] location: class my.pckgSignedRequestCallbackTest
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[18,9] cannot find symbol
[ERROR] symbol  : class After
[ERROR] location: class my.pckgSignedRequestCallbackTest
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[22,9] cannot find symbol
[ERROR] symbol  : class Test
[ERROR] location: class my.pckgSignedRequestCallbackTest
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[24,12] cannot find symbol
[ERROR] symbol  : method assertTrue(boolean)
[ERROR] location: class my.pckgSignedRequestCallbackTest
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我的pom.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>TestServer</name>
    <description>Test</description>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- Facebook library -->
        <dependency>
            <groupId>com.restfb</groupId>
            <artifactId>restfb</artifactId>
            <version>1.6.11</version>
        </dependency>
        <!-- Gson: Java to Json conversion -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency>
        <!-- Tigase server snapshot -->
        <dependency>
            <groupId>tigase</groupId>
            <artifactId>tigase-server</artifactId>
            <version>5.2.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>tigase</id>
            <name>Tigase repository</name>
            <url>http://maven.tigase.org</url>
        </repository>
        <repository>
            <id>tigase-snapshot</id>
            <name>Tigase repository</name>
            <url>http://build.xmpp-test.net/maven/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

mvn依赖项:tree:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Test Server 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ server ---
[INFO] my.pckg:server:jar:0.0.1-SNAPSHOT
[INFO] +- com.restfb:restfb:jar:1.6.11:compile
[INFO] +- com.google.code.gson:gson:jar:2.2.2:compile
[INFO] +- tigase:tigase-server:jar:5.2.0-SNAPSHOT:compile
[INFO] |  +- tigase:tigase-utils:jar:3.4.1-SNAPSHOT:compile
[INFO] |  \- tigase:tigase-xmltools:jar:3.4.3-SNAPSHOT:compile
[INFO] +- commons-codec:commons-codec:jar:1.7:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.240s
[INFO] Finished at: Fri Feb 22 18:07:55 CET 2013
[INFO] Final Memory: 5M/81M
[INFO] ------------------------------------------------------------------------

mvn版本:

Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)
Maven home: /usr/share/maven
Java version: 1.6.0_41, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: de_CH, platform encoding: MacRoman
OS name: "mac os x", version: "10.8.2", arch: "x86_64", family: "mac"

请注意,我不是Java专家也不是maven专家,只是入门(尤其是maven)。从其他文章和问题中所看到的,我应该将测试包含在其中,src/test/java并将“真实”代码包含在其中src/main/java-我已经这样做了。

另外,我删除了整个〜/ .m2 /文件夹,但仍然无法正常工作。

我也跑了,mvn test -X但没有帮助我。如果我应该张贴它,请告诉我。


问题答案:

除非有充分的理由,否则不应覆盖<sourceDirectory>POM <build>元素中的设置。此属性确定Maven在何处查找 非测试
代码。此属性的默认值为src/main/java。该<testSourceDirectory>属性设置 测试
代码的路径(默认为src/test/java。通过将设置<sourceDirectory>为simple
src,Maven认为整个目录包含主应用程序代码。由于该src目录包含src/test/java,因此Maven然后尝试将测试代码编译为主应用程序的一部分。


这很重要,因为在编译主应用程序时(在此compile阶段中),Maven忽略了具有test范围的依赖项。test- compile在主编译之后,测试代码在单独的阶段(阶段)中进行编译。

因此,由于Maven尝试将测试代码作为主应用程序的一部分进行编译,因此它省略了junit依赖性,并且它们在类路径中不可用。这里的解决方案是根本不指定<sourceDirectory>POM中的元素。



 类似资料:
  • 我和我的朋友正在做一个Java maven项目,它的设置和我们从Git得到的项目是一样的。在我的设置中,Maven正确地导入了所有依赖项,但对于我的朋友,它找不到任何依赖项。 我们尝试过的事情: 右键单击project,单击maven并单击Reimport。 我们都可以上网,所以这也不是问题。而且,Maven在IntelliJ中设置为自动导入。

  • 我的IntelliJ IDEA看到了JUnit 4 Maven依赖项。 但不解析来自这些依赖项的任何符号 有什么想法吗?

  • 我想在Java应用程序中包括Postgres JDBC驱动程序,所以我将其添加为maven依赖项。我从这个列表中选择了最后一个版本,令我惊讶的是,它恰好是由Atlassian主持的。现在我收到这个错误: 丢失的工件postgresql: postgresql: jar: 9.4.1208-jdbc42-atlassian托管 我还尝试了一个不由Atlassian托管的旧版本,但得到了相同的错误!有

  • 我创建了一个Maven项目,并添加了所需的依赖项。当我尝试构建项目时,会出现以下错误: 我的依赖关系确实表明,图书馆在那里: 其中包括必要的包: 以下是我的简历: 我对Java比较陌生,对Maven也很陌生,但是当我右键单击和时,我输入了适当的GroupID和artifactID和版本(如Maven Central中列出的),它会自动下载文件。不确定我还缺少什么。感谢任何帮助。 编辑:我应该注意,

  • 在LoginStorageProviderFactory中,它有一个来自LoginStorageProvier的导入,在最后一个类中,它有一个来自外部项目(也是我的项目)中的用户类的导入。 如果我把这个用户类移到jar模块中,错误就会消失,并且一切都能很好地工作;但我的目标是将项目与用户在其他项目中的实现和EAR项目分离。 所以,我认为错误的原因是缺少依赖项,Maven不能解决它,也没有报告错误,

  • Android studio找不到依赖项,这显然是在我的本地存储库中。错误: 错误:未能找到:com.poppy:tutti-frutti-dtos:1.0.0-快照打开文件 在项目结构对话框中打开 _remote.repositories maven-metadata-local.xml tutti-frutti-dtos-1.0.0-snapshot.jar tutti-frutti-dtos