繁体   English   中英

junit 在 scala 类中找不到测试,但在 java 类中找到测试(maven 项目)

[英]junit does not find tests in scala class but does find tests in java class (maven project)

我有一个 Maven 程序,它编译得很好。 我有这个 Scala 类 ./src/test/java/ResearchTest.scala

import org.junit.Test

class ResearchTest
{



 @Test
  def justAnExample(): Unit = {
    System.out.println("This test method should be running SCALA")
  }
}

当我运行 mvn clean test 时,它不运行任何测试,这是输出:

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< poc:webIntTest11111 >-------------------------
[INFO] Building webIntTest11111 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from paypal-snapshots:     http://nexus.paypal.com/nexus/content/repositories/snapshots/com/paypal/risk/ars/url-udf/0.0.1-SNAPSHOT/maven-metadata.xml
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ webIntTest11111 ---
[INFO] Deleting /Users/drimoch/IdeaProjects/webIntTest11111/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ webIntTest11111 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ webIntTest11111 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ webIntTest11111 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ webIntTest11111 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/drimoch/IdeaProjects/webIntTest11111/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ webIntTest11111 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.356 s
[INFO] Finished at: 2018-10-14T16:46:49+03:00
[INFO] ------------------------------------------------------------------------

我也有这个java类./src/test/java/Tests.java:

import org.junit.Test;

public class Tests {
    @Test
   public void justAnExample() {
        System.out.println("This test method should be run JAVA");
    }
}

当我现在运行 mvn clean test 时,它会运行这个测试。

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< poc:webIntTest11111 >-------------------------
[INFO] Building webIntTest11111 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from paypal-snapshots: http://nexus.paypal.com/nexus/content/repositories/snapshots/com/paypal/risk/ars/url-udf/0.0.1-SNAPSHOT/maven-metadata.xml
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ webIntTest11111 ---
[INFO] Deleting /Users/drimoch/IdeaProjects/webIntTest11111/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ webIntTest11111 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ webIntTest11111 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ webIntTest11111 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ webIntTest11111 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/drimoch/IdeaProjects/webIntTest11111/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ webIntTest11111 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running Tests
This test method should be run JAVA
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.054 s - in Tests
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.514 s
[INFO] Finished at: 2018-10-14T16:59:10+03:00
[INFO] ------------------------------------------------------------------------

我想在 Scala 中运行测试,我应该改变什么?

另外,这是我的 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>poc</groupId>
    <artifactId>webIntTest11111</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.paypal.risk.ars.bigdata.webintl</groupId>
            <artifactId>webintl-services</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.paypal.risk.ars.zonkey</groupId>
            <artifactId>spark-utils</artifactId>
            <type>test-jar</type>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.paypal.risk.ars.bigdata.webintl</groupId>
            <artifactId>webintl-services</artifactId>
            <version>2.5.0</version>
            <type>test-jar</type>
        </dependency>
    </dependencies>
</project>

当您运行 maven 测试时。 您实际上正在使用surefire插件。 默认情况下,Surefire 插件将自动包含所有具有以下通配符模式的测试类:

  • “**/Test*.java” - 包括其所有子目录和所有以“Test”开头的 Java 文件名。
  • “**/*Test.java” - 包括其所有子目录和所有以“Test”结尾的 Java 文件名。
  • "**/*Tests.java" - 包括它的所有子目录和所有以 "Tests" 结尾的 Java 文件名
  • “**/*TestCase.java” - 包括其所有子目录和所有以“TestCase”结尾的 Java 文件名。

我建议您在项目中添加一个 build.sbt 文件并使用 sbt 处理 Scala 类。 但是,如果您需要使用 maven 执行此操作,则需要将scalatest-maven-plugin 添加到您的 pom,将 scala 测试类移动到 src/test/scala。 然后你应该可以用maven进行测试。

暂无
暂无

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

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