- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我似乎无法构建一个与我的项目中的资源一起使用的“解决方案”。当我在构建期间运行测试时,它自然可以访问资源,但构建后测试找不到该文件。如何调整我的代码以便它在这两种情况下都能工作,或者调整我的项目。如果时间允许的话,除了我使用 maven-shade 插件的特殊情况之外,我还有兴趣了解与此相关的 Java 项目设置的基础知识。
我尝试了各种路径变化,但没有成功
我尝试过 getClass().ClassLoader 这个那个,但没有成功。
我的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>company.myproj</groupId>
<artifactId>myproj</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-beta3</version>
<!-- <scope>provided</scope> -->
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
<!-- <scope>provided</scope> -->
</dependency>
</dependencies>
<build>
<!-- Source directory configuration -->
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- // Following plugin executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- // Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testtest.xml</suiteXmlFile>
</suiteXmlFiles>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes><exclude>META-INF/versions/**</exclude></excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.testng.TestNG</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我的java
package myproj;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.testng.annotations.Test;
public class TestTest{
@Test(groups = { "basic1" })
public void Test05BasicPASS() throws URISyntaxException, IOException {
Path filePath = Paths.get(getClass().getResource("/images/hydrant.jpg").toURI());
System.out.println("My Path is: "+filePath.toString());
System.out.println("This is test 5, Basic Pass");
}
}
我的testNg TestTest.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteB1" parallel="false">
<test name="TestTest">
<groups>
<run>
<include name="basic1"/>
</run>
</groups>
<classes>
<class name="myproj.TestTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
我的目录结构:
src
-myproj
TestTest.java
-resources
-images
hydrant.jpg
TestTest.xml
pom.xml
当我运行时:mvn package -DskipTests=false测试运行并可以访问图像文件。
构建后,当我在命令行上运行时(构建到“目标”目录:java -jar C:\Dev\testproj\target\myproj-0.0.1.jar TestTest.xml
我在访问文件的行上得到了 FileSystemNotFoundException 测试结果。
最佳答案
最后,我决定 Java“规定”的约定(在特定位置需要目录和在一种情况下工作的方法而不是另一种情况)对于我正在实现的简单测试应用程序来说太复杂了。
我发现以下内容以简单的方式支持我所需要的:
对于初学者来说,我使用的图像用于测试比较,因此由于可能需要即时交换这些图像以进行调试,因此将目录置于应用程序外部会有所帮助。如果有人分享如何更改此问题的标题,我很乐意这样做。
所以,考虑到这一点。现在,我的项目根目录中有一个“images”目录,并且该目录将部署在应用程序附近,从而产生以下基本产品:
-myTestApp
myTestApp.jar
-images
baseImage.jpg
testImage.jpg
我用来从测试方法访问文件的代码如下,适用于构建测试并作为 jar 应用程序运行:
Path screenshotsPath = Paths.get(System.getProperty("user.dir"), "images");
Path baseFile = Paths.get(screenshotsPath.toString(), "baseImage.jpg" );
Path testFile = Paths.get(screenshotsPath.toString(), "testImage.jpg" );
double imageDiff = calcImageDiff(baseFile, testFile);
在 calcImageDiff 中,我仅使用以下内容,其中 file1 和 file2 是路径输入:
img1 = ImageIO.read(file1.toFile());
img2 = ImageIO.read(file2.toFile());
关于java - 如何正确地将资源添加到我的 java 项目中,以便它们可供构建的实例使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56655060/
我在 Web 应用程序中尝试了一些字节码操作,到目前为止,效果很好。现在我需要在代码中的某些特定位置注入(inject)一些字节代码。我用 switch 语句和 method.inserAt(); 尝
我正在尝试对数组中的对象值求和,然后计算每个值相对于总数的百分比。 这里是数组: [ { "ratiototale": [ [ { "0":
我在接口(interface)中有以下方法.. Task> SearchAsync(TU searchOptions); 效果很好。 现在我正在尝试进行单元测试以测试何时出现问题 - 代码会抛出异常。
我似乎无法让 reveal 开始工作,我已经严格遵守所有内容,添加 modernizr,添加插件,添加 css(reveal 显然包含在 foundation 中),在开始时初始化 foundatio
我正在尝试移植 raspberrypi's userspace 的位从 C 到 golang 的代码,我遇到了一个涉及 ioctl() 的程序。 我在执行 C 代码时遇到了问题 #define MAJ
我一直在尝试用纯 Java 制作一个简单的游戏,但在绘图时遇到了问题。我正在尝试保持相对较高的帧速率,但是 JFrame.repaint() 不能被“强制”并且只是在下一个可用机会重绘帧的请求这一事实
给定一个字符串 "2*(i+j) = 20 我想返回 ((lambda x: x >= 20), ['i']) 我可以然后直接提供给 constraint。 最佳答案 您正在寻找 eval 的替代方法
我对改进我的 javascript 代码以使其成为正确的 OOP 很感兴趣....目前我倾向于做这样的事情: jQuery(document).ready(function () { Page
我有一个调用两个动画 Action 的事件监听器。不幸的是,它们的开始有少量错开(例如,函数中的第一个首先开始)。 有谁知道正确同步它们的方法吗? 这是我的代码: $("#nav ul li a").
我只需要检查目录是否存在!但是,如果目录是“E:\Test”,其中 E: 是 CD/DVD 驱动器,并且上面没有插入磁盘,我会看到以下 Delphi 和 Windows 问题。 第一种方法: func
同样的问题: https://stackoverflow.com/questions/11294207/exchange-web-services-argumentexception-using-my
如果您跳转到 this question 中的第一个答案你会看到他使用 Employee.prototype = new Person(); 将 Person.prototype 继承到 Employ
我需要知道如何正确地遍历元素的 ArrayList 并计算元素在列表中出现的次数,而无需事先知道该元素。我尝试了几种方法并且有一种目前有效,但我觉得它很丑陋且不合适。 为了更深入地解释,我有一个 ja
我有一个用 Python 编写的(非常基本但工作完美的)AWS lambda 函数,但是它具有嵌入式凭证以连接到:1)外部网络服务2) DynamoDB 表。 该函数的作用相当基本:它针对服务发布登录
我很好奇 Tornado 推荐的查询 Redis(或任何数据库)的方法是什么。 我见过一些像 https://gist.github.com/357306 这样的例子但他们似乎都在使用对 redis
这更像是一个“我做得对吗”的问题。 快速背景故事:我已经构建了一个 gradle 插件(在一个独立的 gradle/groovy 项目中)。我在另一个 java 项目中使用它。客户项目通过以下方式引用
在我的代码中,我有: function handleMessage() { const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_
我正在努力在 Linux 中刷新嵌入式设备的先前 ROM 转储。我以前的转储包含 oob 数据。我是用nandwrite -n -N -o/dev/mtd0 backup.bin写的,然后再做一次RO
我正在尝试使用 go 为 react-router 提供服务,我已经做到了,但我遇到了麻烦,我认为我做的方式不正确,或者不完整。我正在使用 Mux。我遇到的麻烦是当我按下 时在我的应用程序中,它正确
我正在尝试 promise JSON.parse 方法,但不幸的是没有任何运气。这是我的尝试: Promise.promisify(JSON.parse, JSON)(data).then((resu
我是一名优秀的程序员,十分优秀!