paulbutcher / ScalaMock

Native Scala mocking framework
http://scalamock.org/
MIT License
506 stars 100 forks source link

Can't seem to instantiate mocks for Java classes that have no default constructor #20

Closed manuelbernhardt closed 12 years ago

manuelbernhardt commented 12 years ago

Hi,

I'm trying to use a mock (using the compiler plugin) for a Java class that has no default constructor, but I get this when trying to initialize it via mock[MappingEngine]:

[error] NoSuchMethodException: eu.delving.MappingEngine.<init> (org.scalamock.MockConstructorDummy) (Class.java:1657)
[error] org.scalamock.GeneratedMockFactoryBase$class.mock(GeneratedMockFactoryBase.scala:28)
[error] CollectionProcessorSpec.mock(CollectionProcessorSpec.scala:17)
[error] CollectionProcessorSpec$$anonfun$1$$anonfun$apply$4$$anonfun$apply$5.apply(CollectionProcessorSpec.scala:27)

Is there something special that needs to be done in this case?

Thanks,

Manuel

paulbutcher commented 12 years ago

I've just tried this in a small test project, and it seems to work for me. Is there any chance you could make a small example that demonstrates the problem available?

paulbutcher commented 12 years ago

Closing as have received no feedback - please feel free to reopen if you are still having the problem and can provide an example that demonstrates it.

manuelbernhardt commented 12 years ago

Sorry for the late reply. I created a sample project here which should demonstrate the issue:

https://github.com/manuelbernhardt/scalamock-test

paulbutcher commented 12 years ago

Apologies for the delay in looking at this. I just tried to build the project which illustrates the problem, and unfortunately it's failing with unresolved dependencies:

[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: uk.org.mygrid.resources.jcoord#jcoord;1.0: not found
[warn]  :: org.osgeo#proj4j;0.1.0: not found
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::

I managed to track down a repository that has jcoord: http://build.mygrid.org.uk/maven/repository/, but I can't find one that has proj4j anywhere?

manuelbernhardt commented 12 years ago

Thanks Paul for trying this out, I now asked a colleague to check what is wrong on this end

manuelbernhardt commented 12 years ago

I just updated the sample project with another repository that should contain the missing dependencies, if you could try again. Thanks!

paulbutcher commented 12 years ago

Thanks Manuel - that works now, and I can reproduce the problem.

It looks like the issue is that the the constructor is going recursive during instantiation of the mock - I've not picked through the MappingEngine code in enough detail to work out why, but the root cause is the fact that Mock$MappingEngine inherits from MappingEngine, and therefore its constructor is called when the mock is instantiated. The fix would be to generate mocks for not just directly mocked classes, but also for all the classes that they inherit from.

I'm afraid that, given that the compiler plugin has turned out to be a dead-end and ScalaMock 3 is moving to using macros instead, I'm probably not going to be able to justify the time that would be required to fix this :-(