Phil Phil - 21 days ago 10
Java Question

ClassNotFoundException trying to run my unit tests with SBT

I have a Java project that I'm building using SBT. I have a single JUnit test class. My

build.sbt
has the line:

libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test


When I run
sbt clean compile test
I hit a
ClassNotFoundException
. However, the compiled class is right there in the right directory and the directory is added to the
test
classpath. What am I missing?

[phil@oc0364286225 myproj-client]$ ls target/test-classes/org/myproj/client/
ClientAndServerTest.class test.local.properties





[phil@oc0364286225 myproj-client]$ sbt clean compile test
[info] Loading project definition from /home/phil/foogit/myproj-client/project
[info] Set current project to myproj Java Client (in build file:/home/phil/foogit/myproj-client/)
[success] Total time: 0 s, completed Sep 24, 2016 11:48:58 AM
[info] Updating {file:/home/phil/foogit/myproj-client/}root...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Java source to /home/phil/foogit/myproj-client/target/classes...
[info] Compiling 1 Java source to /home/phil/foogit/myproj-client/target/test-classes...
[error] Execution of test org.myproj.client.ClientAndServerTest failed: java.lang.ClassNotFoundException: org.myproj.client.ClientAndServerTest
[error] at sbt.classpath.ClasspathFilter.loadClass(ClassLoaders.scala:63)
[error] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[error] at com.novocode.junit.JUnitRunner$1.execute(JUnitRunner.java:122)
[error] at sbt.TestRunner.runTest$1(TestFramework.scala:76)
[error] at sbt.TestRunner.run(TestFramework.scala:85)
[error] at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:202)
[error] at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:202)
[error] at sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:185)
[error] at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:202)
[error] at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:202)
[error] at sbt.TestFunction.apply(TestFramework.scala:207)
[error] at sbt.Tests$$anonfun$9.apply(Tests.scala:216)
[error] at sbt.Tests$$anonfun$9.apply(Tests.scala:216)
[error] at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:44)
[error] at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:44)
[error] at sbt.std.Transform$$anon$4.work(System.scala:63)
[error] at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226)
[error] at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226)
[error] at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
[error] at sbt.Execute.work(Execute.scala:235)
[error] at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226)
[error] at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226)
[error] at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
[error] at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
[error] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[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)
[error] Error: Total 1, Failed 0, Errors 1, Passed 0
[error] Error during tests:
[error] org.myproj.client.ClientAndServerTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 4 s, completed Sep 24, 2016 11:49:01 AM





[phil@oc0364286225 myproj-client]$ sbt 'inspect run' 'show test:fullClasspath'
[info] Loading project definition from /home/phil/foogit/myproj-client/project
[info] Set current project to myproj Java Client (in build file:/home/phil/foogit/myproj-client/)
[info] Input task: Unit
[info] Description:
[info] Runs a main class, passing along arguments provided on the command line.
[info] Provided by:
[info] {file:/home/phil/foogit/myproj-client/}root/compile:run
[info] Defined at:
[info] (sbt.Defaults) Defaults.scala:777
[info] Dependencies:
[info] compile:run::streams
[info] runtime:fullClasspath
[info] compile:run::runner
[info] compile:run::mainClass
[info] Delegates:
[info] compile:run
[info] *:run
[info] {.}/compile:run
[info] {.}/*:run
[info] */compile:run
[info] */*:run
[info] Related:
[info] test:run
[info] List(Attributed(target/test-classes), Attributed(target/classes), Attributed(/home/phil/foogit/myproj-client/lib/org.eclipse.persistence.moxy-2.5.2.jar), Attributed(/home/phil/foogit/myproj-client/lib/org.eclipse.persistence.antlr-2.5.2.jar), Attributed(/home/phil/foogit/myproj-client/lib/jersey-media-moxy-2.9.1.jar), Attributed(/home/phil/foogit/myproj-client/lib/jersey-media-multipart-2.12.jar), Attributed(/home/phil/foogit/myproj-client/lib/org.eclipse.persistence.core-2.5.2.jar), Attributed(/home/phil/foogit/myproj-client/lib/org.eclipse.persistence.asm-2.5.2.jar), Attributed(/home/phil/foogit/myproj-client/lib/jersey-entity-filtering-2.9.1.jar), Attributed(/home/phil/.sbt/boot/scala-2.10.5/lib/scala-library.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.jersey.core/jersey-client/jars/jersey-client-2.22.2.jar), Attributed(/home/phil/.ivy2/cache/javax.ws.rs/javax.ws.rs-api/jars/javax.ws.rs-api-2.0.1.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.jersey.core/jersey-common/jars/jersey-common-2.22.2.jar), Attributed(/home/phil/.ivy2/cache/javax.annotation/javax.annotation-api/jars/javax.annotation-api-1.2.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.jersey.bundles.repackaged/jersey-guava/bundles/jersey-guava-2.22.2.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.hk2/hk2-api/jars/hk2-api-2.4.0-b34.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.hk2/hk2-utils/jars/hk2-utils-2.4.0-b34.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.hk2.external/aopalliance-repackaged/jars/aopalliance-repackaged-2.4.0-b34.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.hk2.external/javax.inject/jars/javax.inject-2.4.0-b34.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.hk2/hk2-locator/jars/hk2-locator-2.4.0-b34.jar), Attributed(/home/phil/Apps/activator-dist-1.3.6/repository/org.javassist/javassist/3.18.1-GA/bundles/javassist.jar), Attributed(/home/phil/.ivy2/cache/org.glassfish.hk2/osgi-resource-locator/jars/osgi-resource-locator-1.0.1.jar), Attributed(/home/phil/.ivy2/cache/org.myproj/myproj-core/jars/myproj-core-1.0.0.jar), Attributed(/home/phil/.ivy2/cache/org.myproj/myproj-jax-rs/jars/myproj-jax-rs-1.0.0.jar), Attributed(/home/phil/Apps/activator-dist-1.3.6/repository/com.novocode/junit-interface/0.11/jars/junit-interface.jar), Attributed(/home/phil/Apps/activator-dist-1.3.6/repository/junit/junit/4.11/jars/junit.jar), Attributed(/home/phil/Apps/activator-dist-1.3.6/repository/org.hamcrest/hamcrest-core/1.3/jars/hamcrest-core.jar), Attributed(/home/phil/Apps/activator-dist-1.3.6/repository/org.scala-sbt/test-interface/1.0/jars/test-interface.jar))
[success] Total time: 0 s, completed Sep 24, 2016 11:53:01 AM

Answer

The solution was to add the following lines to build.sbt:

unmanagedClasspath in Test += baseDirectory.value / "WebContent" / "WEB-INF" / "classes"
fork in Test := true
Comments