我有一个Utils.scala
类,其中包含以下功能:
object Utils {
def getQueryFromFile(fileName: String): String = {
if (fileName != null) {
try {
val path = getClass.getResourceAsStream(s"/$fileName.sql")
Source.fromInputStream(path).getLines.mkString
}
catch {
case _: NullPointerException => throw new FileNotFoundException(s"queryDefinition: $fileName Does not exist")
}
}
else {
throw new InvalidFileInputException(s"FileInput.read: file name is null")
}
}
}
我在src/main/resources
中存在的文件中有两个sql查询。当我从包含Object
方法的main()
(来自src/main/scala
)的方法运行该方法时,它可以正常工作,但是当我尝试在测试用例中运行它时,ScalaTest
), 它失败。它引发了我这个错误:
queryDefinition: a.sql Does not exist
java.io.FileNotFoundException: queryDefinition: COUNT_CHECK.sql Does not exist
at com.eventdetector.utils.Utils$.getQueryFromFile(Utils.scala:19)
at com.eventdetector.budget.BudgetCheckTest$$anonfun$5.apply(BudgetCheckTest.scala:47)
此文件肯定存在于src/main/resources
中。然后我想,因为我正在从src/test/scala
目录运行测试用例文件,所以我也尝试将文件放置在src/test/resources
中,但这也无济于事。
编辑:我的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.eventdetector</groupId>
<artifactId>EventDetector</artifactId>
<version>0.1</version>
<name>${project.artifactId}</name>
<inceptionYear>2020</inceptionYear>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.11.12</scala.version>
<scala.compat.version>2.11</scala.compat.version>
<spark.version>2.4.4</spark.version>
<spec2.version>4.2.0</spec2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc</artifactId>
<version>db2jcc4</version>
</dependency>
<dependency>
<groupId>org.scalaj</groupId>
<artifactId>scalaj-http_${scala.compat.version}</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-json_${scala.compat.version}</artifactId>
<version>2.7.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc_${scala.compat.version}</artifactId>
<version>3.4.1</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.compat.version}</artifactId>
<version>3.0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<resources>
<resource>
<directory>src/main/scala</directory>
<includes>
<include>**/*.sql</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.sql</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.3.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<!-- Tests will be run with scalatest-maven-plugin instead -->
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>TestSuiteReport.txt</filereports>
<!-- Comma separated list of JUnit test class names to execute -->
<jUnitClasses>samples.AppTest</jUnitClasses>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:0)
使用Maven约定时,explicitly overriden it in POM使用不同的资源目录
<resources>
<resource>
<directory>src/main/scala</directory>
<includes>
<include>**/*.sql</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.sql</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
因此请删除该部分,或将其修复为使用src/main/resources
。
答案 1 :(得分:0)
尝试将资源src/main/resources
添加到pom.xml中的资源块中:
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.sql</include>
</includes>
<filtering>true</filtering>
</resource>
您的pom.xml中的资源应放在src/main/scala
和src/main/java
中,但是通常这些目录是用于代码的,而不是某些资源文件的目录。默认情况下,资源放置在src/main/resources
中。在测试之前,请不要忘记致电maven clean
。