测试类应该只有一个公共构造函数

时间:2017-12-13 15:00:51

标签: playframework

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

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 console(test)运行上述程序时

它给了我以下异常

[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

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"))

1 个答案:

答案 0 :(得分:1)

JUnit不理解或不支持WSClient注释,因此使用它注释构造函数将无济于事。您必须在测试中实例化您自己的{{1}},并在测试完成时将其拆除。 Play documentation on WSClient详细说明了如何执行此操作。