paulbutcher/ScalaMock — Issue #7: Can't mock Java classes?
Author: cespare
Created: 2012-02-03 03:11:56+00:00
State: open

I first noticed this when trying to mock a `DatagramSocket`, but reproduced a minimal example by just using `Object`. You can see the change as a single commit on my fork of the example scalamock project [here](https://github.com/cespare/ScalaMockExample/commit/5759df2aa82a2f4f00ba30854a44f338e6d37c56). (By the way, I'm using sbt 0.11.2. Not sure if that matters.)

Running `generate-mocks` seems to work, but creates invalid code that doesn't compile when you try to run `test`:

> test
[info] Compiling 5 Scala sources and 2 Java sources to /home/caleb/test/ScalaMockExample/target/scala-2.9.1/mock-classes...
[error] /home/caleb/test/ScalaMockExample/target/scala-2.9.1/src_managed/mock/scala/java/net/DatagramSocket$JavaFeatures.java:8: `]' expected but identifier found.
[error] public static java.lang.Class[_] implClass = (java.lang.Class[_])org.scalamock.ReflectionUtilities.getObjectField(clazz, "implClass");
[error]
...

Apologies if this is covered somewhere in the docs. I didn't read anything specifically about mocking pure Java classes, but I was hoping that would work as my code generally has at least a few java standard library dependencies that would be nice to mock.

Thanks!