简体   繁体   English

无法运行普通 Java Wiremock

[英]Unable to run a plain Java Wiremock

I'm an absolute beginner to Wiremock, Maven and stuff.我绝对是 Wiremock、Maven 和东西的初学者。 I just want to run Wiremock server in my Java program.我只想在我的 Java 程序中运行 Wiremock 服务器。 I got Maven installed and used this and this guides to try and get Wiremock running inside my java code.我安装了 Maven 并使用这个这个指南来尝试让 Wiremock 在我的 java 代码中运行。 My pom.xml file looks like this:我的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>

And my Java code which resides in a file called testWiremock.java looks like this:我的 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();
     }
}

When I try to compile it with mvn compile , I get the following error:当我尝试使用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 

What am I missing here?我在这里错过了什么? Do I have to have any imports?我必须有任何进口商品吗? Any help would be greatly appreciated.任何帮助将不胜感激。

WireMock is just for testing but you've put it into the implementation scope of your code base (ie in folder src/main/java ) by referring to it in a main class. However, it cannot be available there because the dependency is equipped with <scope>test</scope> . WireMock 仅用于测试,但您通过在主 class 中引用它,将其放入代码库的实现 scope(即文件夹src/main/java中)。但是,它在那里不可用,因为依赖项已配备使用<scope>test</scope> Leave it as it is and configure WireMock in your test classes (ie in folder src/test/java ) like the WireMock's documentation shows it:保持原样并在您的测试类中配置 WireMock(即在文件夹src/test/java中),如WireMock 的文档所示:

@WireMockTest(httpPort = 8080)
public class FixedPortDeclarativeWireMockTest {
    ...
}

That code snippet above has the same effect as what you did in the main class.上面的代码片段与您在主要 class 中所做的效果相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM