我想要这样的测试。
package database;
import org.junit.Assert;
import org.junit.Test;
import play.libs.ws.WS;
import play.libs.ws.WSResponse;
import play.test.WithApplication;
import static play.mvc.Http.Status.OK;
import static play.test.Helpers.running;
import static play.test.Helpers.testServer;
public class UserTest extends WithApplication {
private static final long TIMEOUT = 4000;
@Test
public void testUser() {
running(testServer(3333, app), () -> {
WSResponse response = WS
.url("http://localhost:3333/api/user/1")
.get()
.get(TIMEOUT);
Assert.assertEquals(OK, response.getStatus());
});
}
}
但是,在运行激活器测试时,我一直收到以下错误。由于某种原因,连接池在执行测试之前关闭。这可能是什么问题?
/usr/lib/jvm/default/bin/java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M -Didea.launcher.port=7537 -Didea.launcher.bin.path=/usr/share/intellij-idea-ultimate-edition/bin -Dfile.encoding=UTF-8 -classpath /home/albert/.IntelliJIdea15/config/plugins/Scala/launcher/sbt-launch.jar:/usr/share/intellij-idea-ultimate-edition/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain xsbt.boot.Boot test
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256M; support was removed in 8.0
[info] Loading project definition from /mnt/hdd/repos/git/IDP/startup-job-market/project
[info] Set current project to Job Platform (in build file:/mnt/hdd/repos/git/IDP/startup-job-market/)
[info] - application - Registering formatter class formatters.TimestampFormatter for class java.sql.Timestamp
[info] - application - Creating Pool for datasource 'default'
[info] - play.api.db.HikariCPConnectionPool - datasource [default] bound to JNDI as DefaultDS
[info] - application - Shutting down connection pool.
[info] - application - Got request: GET /api/user/1
[error] - org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Pool has been shutdown
[error] - application -
! @6oa78jm5a - Internal server error, for (GET) [/api/user/1] ->
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PersistenceException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:265) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:179) [play_2.11-2.4.3.jar:2.4.3]
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:212) [play_2.11-2.4.3.jar:2.4.3]
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:94) [play_2.11-2.4.3.jar:2.4.3]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:158) [play-netty-server_2.11-2.4.3.jar:2.4.3]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:155) [play-netty-server_2.11-2.4.3.jar:2.4.3]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.6.jar:na]
at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:215) [scala-library-2.11.6.jar:na]
at scala.util.Try$.apply(Try.scala:191) [scala-library-2.11.6.jar:na]
at scala.util.Failure.recover(Try.scala:215) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [scala-library-2.11.6.jar:na]
at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [play-iteratees_2.11-2.4.3.jar:2.4.3]
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [scala-library-2.11.6.jar:na]
at scala.concurrent.Promise$class.complete(Promise.scala:55) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [scala-library-2.11.6.jar:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.13.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) ~[hibernate-entitymanager-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1619) ~[hibernate-entitymanager-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1106) ~[hibernate-entitymanager-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1033) ~[hibernate-entitymanager-5.0.4.Final.jar:5.0.4.Final]
at dao.DAO.find(DAO.java:72) ~[classes/:na]
at controllers.base.GenericRestEntityController.get(GenericRestEntityController.java:56) ~[classes/:na]
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:768) ~[classes/:na]
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:768) ~[classes/:na]
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:136) ~[play_2.11-2.4.3.jar:2.4.3]
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:127) ~[play_2.11-2.4.3.jar:2.4.3]
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:70) ~[play_2.11-2.4.3.jar:2.4.3]
at play.http.DefaultHttpRequestHandler$1.call(DefaultHttpRequestHandler.java:20) ~[play_2.11-2.4.3.jar:2.4.3]
at play.db.jpa.TransactionalAction.lambda$call$5(TransactionalAction.java:19) ~[play-java-jpa_2.11-2.4.3.jar:2.4.3]
at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:136) ~[play-java-jpa_2.11-2.4.3.jar:2.4.3]
at play.db.jpa.JPA.withTransaction(JPA.java:159) ~[play-java-jpa_2.11-2.4.3.jar:2.4.3]
at play.db.jpa.TransactionalAction.call(TransactionalAction.java:16) ~[play-java-jpa_2.11-2.4.3.jar:2.4.3]
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94) ~[play_2.11-2.4.3.jar:2.4.3]
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94) ~[play_2.11-2.4.3.jar:2.4.3]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:40) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70) [play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32) ~[play_2.11-2.4.3.jar:2.4.3]
at scala.concurrent.impl.Future$.apply(Future.scala:31) ~[scala-library-2.11.6.jar:na]
at scala.concurrent.Future$.apply(Future.scala:492) ~[scala-library-2.11.6.jar:na]
at play.core.j.JavaAction.apply(JavaAction.scala:94) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) ~[play_2.11-2.4.3.jar:2.4.3]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:104) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:103) ~[play_2.11-2.4.3.jar:2.4.3]
at scala.Option.map(Option.scala:146) ~[scala-library-2.11.6.jar:na]
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:103) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.3.jar:2.4.3]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [scala-library-2.11.6.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
... 6 common frames omitted
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:90) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:112) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:47) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.prepareQueryStatement(AbstractLoadPlanBasedLoader.java:240) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:184) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:120) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:85) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3954) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:491) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:456) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:199) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:261) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:137) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1071) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2638) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1075) ~[hibernate-entitymanager-5.0.4.Final.jar:5.0.4.Final]
... 45 common frames omitted
Caused by: java.sql.SQLException: Pool has been shutdown
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:119) ~[HikariCP-2.3.7.jar:na]
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:382) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:87) ~[hibernate-core-5.0.4.Final.jar:5.0.4.Final]
... 66 common frames omitted
[info] - application - Shutting down connection pool.
[error] Test database.AbstractControllerTest.testIndex failed: java.lang.RuntimeException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
[error] at [Source: org.jboss.netty.buffer.ChannelBufferInputStream@526df53; line: 3, column: 2], took 6.212 sec
[error] at play.libs.Json.parse(Json.java:149)
[error] at play.libs.ws.ning.NingWSResponse.asJson(NingWSResponse.java:149)
[error] at database.AbstractControllerTest.lambda$testIndex$0(AbstractControllerTest.java:82)
[error] at play.test.Helpers.running(Helpers.java:524)
[error] at database.AbstractControllerTest.testIndex(AbstractControllerTest.java:76)
[error] ...
[error] Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
[error] at [Source: org.jboss.netty.buffer.ChannelBufferInputStream@526df53; line: 3, column: 2]
[error] at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1487)
[error] at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:518)
[error] at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:447)
[error] at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2485)
[error] at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:801)
[error] at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:697)
[error] at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3604)
[error] at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3549)
[error] at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2078)
[error] at play.libs.Json.parse(Json.java:147)
[error] ... 40 more
[info] Test database.UserControllerTest ignored
[error] Failed: Total 2, Failed 1, Errors 0, Passed 0, Skipped 1
[error] Failed tests:
[error] database.AbstractControllerTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 8 s, completed 30.11.2015 17:49:24
Process finished with exit code 1
答案 0 :(得分:0)
来自文档: 注意:每次运行测试或仅测试时,都会分叉一个新进程。新进程使用默认JVM设置。自定义设置可以添加到build.sbt。例如:
也许你可以尝试禁用它
将以下代码放在build.sbt
中fork in run := false
fork in Test := false