我是Wiremock、Maven和其他东西的绝对初学者。我只是想在我的Java程序中运行Wiremock服务器。我安装了Maven,并使用这个和这个指南来尝试让Wiremock在我的java代码中运行。我的pom.xml
文件看起来像这样:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycode</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.35.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
我的Java代码驻留在一个名为testWiremock.java的文件中,看起来像这样:
public class testWiremock{
public static void main(String[] args){
WireMockServer wiremockServer = new WireMockServer(options().port(8080));
wireMockServer.start();
System.out.println("Server running successfully!");
wreMockServer.stop();
}
}
当我尝试用mvn compile
编译它时,我得到以下错误:
ppgoodman@CWGXXXXQCL wiretest % mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO]
com.mycode:test
[INFO] Building test 1.0
[INFO]
[ jar ]---
[INFO]
[INFO] --- maven-resources-plugin:2.6: resources (default-resources) @ test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /Users/ppgoodman/Documents/programming/wiretest/src/main/resources [INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[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/ppgoodman/Documents/programming/wiretest/target/classes [INFO]
[ERROR] COMPILATION ERROR :
[INFO]
[ERROR] /Users/ppgoodman/Documents/programming/wiretest/src/main/java/testWiremock.java: [4,9] cannot find symbol symbol: class WireMockServer
location: class testWiremock
[ERROR] /Users/ppgoodman/Documents/programming/wiretest/src/main/java/testWiremock.java: [4,45] cannot find symbol symbol: class WireMockServer
location: class testWiremock
[ERROR] /Users/ppgoodman/Documents/programming/wiretest/src/main/java/testWiremock.java: [4,60] cannot find symbol symbol: method options()
location: class testWiremock
[INFO] 3 errors
[INFO]
[INFO]
[INFO] BUILD FAILURE
[INFO]
[INFO] Total time: 8.432 s
[INFO] Finished at: 2822-11-13T18:29:49-05:00
[INFO]
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test: Compilation failure: Compilation failure: [ERROR] /Users/ppgoodman/Documents/programming/wiretest/src/main/java/testWiremock.java: [4,9] cannot find symbol
[ERROR]
symbol: class WireMockServer
[ERROR] location: class testwiremock
[ERROR] /Users/ppgoodman/Documents/programming/wiretest/src/main/java/testWiremock.java: [4,45] cannot find symbol [ERROR] symbol: class WireMockServer
[ERROR] location: class testWiremock
wiretest-zsh-245x68
[ERROR] /Users/ppgoodman/Documents/programming/wiretest/src/main/java/testWiremock.java: [4,60] cannot find symbol [ERROR]
symbol: method options()
[ERROR] location: class testWiremock
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
我在这里错过了什么?我一定要进口吗?如有任何帮助,我将不胜感激。
WireMock只是为了测试,但你已经把它放入你的代码库的实现范围(即在文件夹src/main/java)通过在一个主类中引用它。但是,它不能在那里可用,因为依赖项配备了<scope>test</scope>
。让它保持原样,并在您的测试类中配置WireMock(即在文件夹src/test/java中),如WireMock的文档所示:
@WireMockTest(httpPort = 8080)
public class FixedPortDeclarativeWireMockTest {
...
}
上面的代码片段与在主类中所做的效果相同。