【问题标题】:junit does not find tests in scala class but does find tests in java class (maven project)junit 在 scala 类中找不到测试,但在 java 类中找到测试(maven 项目)
【发布时间】:2019-03-19 02:28:02
【问题描述】:

我有一个 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>

【问题讨论】:

  • 我认为你需要@RunWith(classOf[JUnitRunner]),除非你使用 ScalaTest 和 Maven。
  • 我放弃了,现在我只是在使用 Scalatest,无论如何谢谢

标签: java scala maven junit


【解决方案1】:

当你运行 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 进行测试了。

【讨论】:

  • 非常感谢您的回答,但在这样做之后,我能够运行 scalatest 测试,但不能运行我想要的 Junit 测试
猜你喜欢
  • 1970-01-01
  • 2015-08-27
  • 2017-08-30
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
相关资源
最近更新 更多