我有一个使用JUnit的Maven项目,用Selenium和Cucumber在Web浏览器上执行自动化测试。 我设置Maven编译一个包含所有依赖项的.jar。 所以我想要的是,当我点击.jar时,它会执行测试并输出带有测试结果的输出.json文件。
的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>TestingAutomation</groupId>
<artifactId>TestingAutomation</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>StartTests</mainClass>
</manifest>
</archive>
</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-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
StartTests.java:
import org.junit.runner.JUnitCore;
public class StartTests {
public static void main(String args[]) {
JUnitCore.main("Runner");
}
}
Runner.java:
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/main/resources/features")
public class Runner {
}
所以,当我从InteliJ运行StartTests.java时,我得到的是:
JUnit version 4.12
.Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 32181
Only local connections are allowed.
feb 05, 2017 11:17:04 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
feb 05, 2017 11:17:05 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
...
1 Scenarios (1 passed)
3 Steps (3 passed)
0m8.098s
Time: 8,14
OK (4 tests)
Process finished with exit code 0
当我执行项目的Maven构建时,Maven正确地创建了所有依赖项.jar文件但是当我执行它时双击没有任何反应。 我试图通过命令行(Windows环境)执行它:
java -jar TestingAutomation.jar
我得到的回报如下:
JUnit version 4.12
.E
Time: 0,001
There was 1 failure:
1) initializationError(Runner)
java.lang.NoClassDefFoundError: org/jboss/marshalling/Unmarshaller
生成的MANIFEST.MF文件:
Manifest-Version: 1.0
Built-By: MyName
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_121
Main-Class: StartTests