play framework 2.1.x测试与slf4j-api冲突

时间:2013-12-29 19:03:24

标签: playframework-2.1 slf4j

我有一个Play Framework 2.1.0应用程序,即使是最简单的单元测试也不能运行,因为系统问题我很难追踪。鉴于这条路线:

POST      /foo/bar     controllers.FooBar.test()

...我有一个简单的测试来验证路线是否存在:

@Test
public void fooBarRouteExists() {
    running(fakeApplication(fakeGlobal()), new Runnable() {
        @Override
        public void run() {
            final Result result = route(fakeRequest(POST, "/foo/bar"));
            assertThat(result).isNotNull();
        }
    });
}

...运行时会产生以下异常:

 hsl [323]> play test
 [info] Loading project definition from /Users/hsl/Documents/workspace/test_project/project
 [info] Set current project to test_project (in build file:/Users/hsl/Documents/workspace/test_project/)
 [info] Compiling 1 Java source to /Users/hsl/Documents/workspace/test_project/target/scala-2.10/test-classes...
 SLF4J: Class path contains multiple SLF4J bindings.
 SLF4J: Found binding in [file:/Users/hsl/Documents/workspace/test_project/target/scala-2.10/classes/org/slf4j/impl/StaticLoggerBinder.class]
 SLF4J: Found binding in [jar:file:/usr/local/play-2.1.0/repository/local/ch.qos.logback/logback-classic/1.0.7/jars/logback-classic.> jar!/org/slf4j/impl/StaticLoggerBinder.class]
 SLF4J: Found binding in [jar:file:/usr/local/play-2.1.0/repository/cache/org.apache.activemq/activemq-all/jars/activemq-all-5.9.0.> jar!/org/slf4j/impl/StaticLoggerBinder.class]
 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
 [error] Test controllers.FooBar.test failed: null
 [error]     at org.slf4j.LoggerFactory.bind(slf4j:LoggerFactory.java):128)
 [error]     at org.slf4j.LoggerFactory.performInitialization(slf4j:LoggerFactory.java):107)
 [error]     at org.slf4j.LoggerFactory.getILoggerFactory(slf4j:LoggerFactory.java):295)
 [error]     at org.slf4j.LoggerFactory.getLogger(slf4j:LoggerFactory.java):269)
 [error]     at play.api.Logger$.<init>(Logger.scala:179)
 [error]     at play.api.Logger$.<clinit>(Logger.scala)
 [error]     at play.api.Application$class.$init$(Application.scala:266)
 [error]     at play.api.test.FakeApplication.<init>(Fakes.scala:187)
 [error]     at play.test.FakeApplication.<init>(FakeApplication.java:28)
 [error]     at play.test.Helpers.fakeApplication(Helpers.java:97)
 [error]     at controllers.FooBar.test(FooBar.java:20)
 [error]     ...
 [error] Caused by: java.lang.UnsupportedOperationException: This code should have never made it into slf4j-api.jar
 [error]     at org.slf4j.impl.StaticLoggerBinder.<init>(impl:StaticLoggerBinder.java):63)
 [error]     at org.slf4j.impl.StaticLoggerBinder.<clinit>(impl:StaticLoggerBinder.java):44)
 [error]     ... 42 more
 [error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0
 [error] Failed tests:
 [error]    controllers.FooBar
 java.lang.RuntimeException: Tests unsuccessful
    at scala.sys.package$.error(package.scala:27)
    at scala.Predef$.error(Predef.scala:123)
    at sbt.Tests$.showResults(Tests.scala:192)
    at sbt.Defaults$$anonfun$testTasks$6.apply(Defaults.scala:296)
    at sbt.Defaults$$anonfun$testTasks$6.apply(Defaults.scala:294)
    at sbt.Scoped$$anonfun$hf4$1.apply(Structure.scala:580)
    at sbt.Scoped$$anonfun$hf4$1.apply(Structure.scala:580)
    at scala.Function1$$anonfun$compose$1.apply(Function1.scala:49)
    at sbt.coped$Reduced$$anonfun$combine$1$$anonfun$apply$12.apply(Structure.scala:311)
    at sbt.Scoped$Reduced$$anonfun$combine$1$$anonfun$apply$12.apply(Structure.scala:311)
    at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:41)
    at sbt.std.Transform$$anon$5.work(System.scala:71)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:232)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:232)
    at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
    at sbt.Execute.work(Execute.scala:238)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:232)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:232)
    at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
    at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)
 [error] (test:test) Tests unsuccessful> 

有人可以帮我弄清问题是什么吗?

1 个答案:

答案 0 :(得分:1)

关键错误消息位于顶部:

 SLF4J: Class path contains multiple SLF4J bindings.
 SLF4J: Found binding in [file:/Users/hsl/Documents/workspace/test_project/target/scala-2.10/classes/org/slf4j/impl/StaticLoggerBinder.class]
 SLF4J: Found binding in [jar:file:/usr/local/play-2.1.0/repository/local/ch.qos.logback/logback-classic/1.0.7/jars/logback-classic.> jar!/org/slf4j/impl/StaticLoggerBinder.class]
 SLF4J: Found binding in [jar:file:/usr/local/play-2.1.0/repository/cache/org.apache.activemq/activemq-all/jars/activemq-all-5.9.0.> jar!/org/slf4j/impl/StaticLoggerBinder.class]
 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

这很有趣,看起来你已经在你的测试项目中放了(一些)sl4j的源代码,所以Play正在编译它。这会导致您遇到的问题。

从测试项目中删除slf4j的源代码,它应该可以工作。

顺便说一句,您仍然可以访问该资源以供参考 - 例如如果您使用的是Eclipse,则可以将相应的源附加到项目中的jar。 play "eclipse with-sources"将自动执行此操作,以便将已将源jar文件正确发布到其Maven或Ivy存储库的依赖项。