首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试类应该恰好有一个公共构造函数

测试类应该恰好有一个公共构造函数
EN

Stack Overflow用户
提问于 2017-12-13 23:00:51
回答 1查看 589关注 0票数 0

我写了一个调用POST服务的程序

代码语言:javascript
复制
package itss;
import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
import org.junit.Test;
import play.libs.Json;
import play.libs.ws.WSBodyReadables;
import play.libs.ws.WSBodyWritables;
import play.libs.ws.WSClient;
import play.libs.ws.WSResponse;
import com.fasterxml.jackson.databind.JsonNode;
public class IntegrationTest implements WSBodyReadables, WSBodyWritables {
     private final WSClient ws;
        @Inject
        public IntegrationTest(WSClient ws) {
            this.ws = ws;
        }

     @Test
     public void sendSmsMessage() {
         try
         {
            JsonNode json = Json.newObject().put("calledfrom", "dsddsds");
            String url = "http://xx.xxx.xxx.xxx:8081/FS/fsqa/cd";
            CompletionStage<WSResponse> responseCompletionStage = ws.url(url).setContentType("application/json").post(json);
            responseCompletionStage.handle((result, error) -> {
                try
                {
                 System.out.println("I am called");
                 System.out.println("Body results is "+result.getBody().toString());
                 System.out.println("into error"+error.getMessage());
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                return responseCompletionStage;

            });

         }
         catch(Exception e)
         {
             e.printStackTrace();

         }
        }
}

当我通过sbt控制台(test)运行上述程序时

它给了我以下异常

代码语言:javascript
复制
[info] Test itss.IntegrationTest.initializationError started
[error] Test itss.IntegrationTest.initializationError failed: java.lang.Exception: Test class should have exactly one public constructor, took 0.005 sec
[error]     at com.novocode.junit.JUnitRunner$1.execute(JUnitRunner.java:124)
[error]     at sbt.ForkMain$Run$2.call(ForkMain.java:296)
[error]     at sbt.ForkMain$Run$2.call(ForkMain.java:286)
[error]     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[error]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[error]     at java.lang.Thread.run(Thread.java:745)
[info] Test run finished: 1 failed, 0 ignored, 1 total, 0.022s
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]         itss.IntegrationTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 4 s, completed 13 Dec, 2017 8:26:52 PM
[play-java-rest-api-example] $ [info]

谁能告诉我怎么解决这个问题。

我使用的是play framewrk 2.6

这是我的build.sbt

代码语言:javascript
复制
name := """play-java-rest-api-example"""

version := "2.6.x"

inThisBuild(
  List(
    scalaVersion := "2.12.3",
    dependencyOverrides := Seq(
       "org.codehaus.plexus" % "plexus-utils" % "3.0.18",
       "com.google.code.findbugs" % "jsr305" % "3.0.1",
       "com.google.guava" % "guava" % "22.0",
       "com.typesafe.akka" %% "akka-stream" % "2.5.6",
       "com.typesafe.akka" %% "akka-actor" % "2.5.6"
    )
  )
)
EclipseKeys.preTasks := Seq(compile in Compile)
EclipseKeys.projectFlavor := EclipseProjectFlavor.Java          
EclipseKeys.createSrc := EclipseCreateSrc.ValueSet(EclipseCreateSrc.ManagedClasses, EclipseCreateSrc.ManagedResources)   


lazy val GatlingTest = config("gatling") extend Test

lazy val root = (project in file(".")).enablePlugins(PlayJava, GatlingPlugin).configs(GatlingTest)
  .settings(inConfig(GatlingTest)(Defaults.testSettings): _*)
  .settings(
    scalaSource in GatlingTest := baseDirectory.value / "/gatling/simulation"
  )

libraryDependencies += guice
libraryDependencies += javaJpa
libraryDependencies += "com.h2database" % "h2" % "1.4.194"

libraryDependencies += "org.hibernate" % "hibernate-core" % "5.2.9.Final"
libraryDependencies += "io.dropwizard.metrics" % "metrics-core" % "3.2.1"
libraryDependencies += "com.palominolabs.http" % "url-builder" % "1.1.0"
libraryDependencies += "net.jodah" % "failsafe" % "1.0.3"

libraryDependencies += "io.gatling.highcharts" % "gatling-charts-highcharts" % "2.3.0" % Test
libraryDependencies += "io.gatling" % "gatling-test-framework" % "2.3.0" % Test
libraryDependencies ++= Seq(ws)
libraryDependencies += "org.assertj" % "assertj-core" % "3.6.2" % Test
libraryDependencies += "org.awaitility" % "awaitility" % "2.0.0" % Test
PlayKeys.externalizeResources := false

testOptions in Test := Seq(Tests.Argument(TestFrameworks.JUnit, "-a", "-v"))
EN

回答 1

Stack Overflow用户

发布于 2017-12-14 10:13:16

JUnit不理解或不支持@Inject注释,所以用它注释构造函数是没有帮助的。相反,您必须在测试中实例化您自己的WSClient,并在测试完成时拆除它。Play documentation on WSClient详细介绍了如何做到这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47796150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档