ScalaTest 错误对象 flatspec 不是包 org.scalatest 的成员

ScalaTest error object flatspec is not a member of package org.scalatest

我再次从 scalatest.org 站点和 Maven 配置中使用示例测试,如 scalatest.org 上的参考文档中所述,但每当我 运行 mvn clean install 它抛出编译Scala 测试的时间错误。

分享下面的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.scala.example</groupId>
    <artifactId>ScalaTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <log4j.core.version>2.9.1</log4j.core.version>
        <log4j.api.version>2.9.1</log4j.api.version>
        <scala.version>2.11</scala.version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>4.5.2</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
                <!--This plugin's configuration is used to store Eclipse m2e settings
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            net.alchim31.maven
                                        </groupId>
                                        <artifactId>
                                            scala-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [3.3.1,)
                                        </versionRange>
                                        <goals>
                                            <goal>add-source</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>4.5.2</version>
                <executions>
                    <execution>
                        <id>scala-main-compile</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <source>src/main/scala</source>
                            <source>src/test/scala</source>
                        </configuration>
                    </execution>
                    <!--execution>
                        <id>scala-test-compile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <source>src/test/scala</source>
                        </configuration>
                    </execution-->
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- disable surefire -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!-- enable scalatest -->
            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <junitxml>.</junitxml>
                    <filereports>ScalaTests.txt</filereports>
                    <skipTests>false</skipTests>
                </configuration>
                <executions>
                    <execution>
                        <id>test</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--enable SCoverage-->
            <!--plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>1.1.1</version>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                    <aggregate>true</aggregate>
                </configuration>
            </plugin-->
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.scoverage</groupId>
                <artifactId>scoverage-maven-plugin</artifactId>
                <version>1.1.1</version>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                    <aggregate>true</aggregate>
                </configuration>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.10 -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql_2.10 -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.10</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.10.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.scalaj</groupId>
            <artifactId>scalaj-http_2.11</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_2.10</artifactId>
            <version>2.2.6</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Scala 测试基础测试 class 根据 scalatest.org

package com.scala.example.test

import org.scalatest._
import flatspec._
import matchers._

abstract class UnitSpec extends AnyFlatSpec with should.Matchers with OptionValues with Inside with Inspectors

实际 Scala 测试

package com.scala.example.test

import collection.mutable.Stack
import org.scalatest.flatspec.AnyFlatSpec

class StackSpecTest extends UnitSpec {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    assert(stack.pop() === 2)
    assert(stack.pop() === 1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[String]
    assertThrows[NoSuchElementException] {
      emptyStack.pop()
    }
  }
}

运行宁mvn clean install

时出错
[INFO] --- scala-maven-plugin:4.5.2:testCompile (scala-main-compile) @ ScalaTest ---
[WARNING]  Expected all dependencies to require Scala version: 2.11.0
[WARNING]  org.json4s:json4s-core_2.10:3.2.11 requires scala version: 2.10.0
[WARNING] Multiple versions of scala libraries detected!
[INFO] Using incremental compilation using Mixed compile order
[INFO] Compiler bridge file: C:\Users2061744\.sbt.0\zinc\org.scala-sbt\org.scala-sbt-compiler-bridge_2.11-1.5.3-bin_2.11.0__52.0-1.5.3_20210509T044243.jar
[INFO] compiling 4 Scala sources to C:\Code\target\test-classes ...
[ERROR] C:/Code/src/test/scala/com/scala/example/test/StackSpecTest.scala:4: object flatspec is not a member of package org.scalatest
[ERROR] C:/Code/src/test/scala/com/scala/example/test/UnitSpec.scala:4: not found: object flatspec
[ERROR] C:/Code/src/test/scala/com/scala/example/test/UnitSpec.scala:7: not found: type AnyFlatSpec
[ERROR] C:/Code/src/test/scala/com/scala/example/test/UnitSpec.scala:7: not found: value should
[ERROR] C:/Code/src/test/scala/com/scala/example/test/StackSpecTest.scala:8: value should is not a member of String
[ERROR] C:/Code/src/test/scala/com/scala/example/test/StackSpecTest.scala:12: value === is not a member of Int
[ERROR] C:/Code/src/test/scala/com/scala/example/test/StackSpecTest.scala:13: value === is not a member of Int
[ERROR] C:/Code/src/test/scala/com/scala/example/test/StackSpecTest.scala:16: not found: value it
[ERROR] 18 errors found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  43.967 s
[INFO] Finished at: 2021-06-13T12:56:17+05:30
[INFO] ------------------------------------------------------------------------

有关基于探索的其他类似文章的信息,还共享文件夹结构..

\src\main\resources - resources folder

\src\main\scala - application specific scala components to be tested

\src\main\scala - Scala test cases are here

您正在使用 scalatest 版本 2.2.6:

        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_2.10</artifactId>
            <version>2.2.6</version>
            <scope>test</scope>
        </dependency>

scalatest.org 上的当前文档适用于版本 3.*。 class 结构已更改。如果我记得 2.* 中没有 AnyFlatSpec。您应该改用 org.scalatest.FlatSpec。样本测试:https://www.scalatest.org/scaladoc/2.2.6/index.html#org.scalatest.FlatSpec

所以你的 UnitSpec 应该是这样的:

import org.scalatest._

abstract class UnitSpec extends FlatSpec with Matchers with OptionValues with Inside  with Inspectors