The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

225035 (5) [Avatar] Offline
I'm trying to write a test to return a list from method calls to EntityManager. The line of code is:
entityManager.createNativeQuery("myQuery").setParameter(1, param1).getResultList()

Since createNativeQuery returns a Query, the setParameter is called on that. But so far I can't figure out how to set up the mocks to allow me to create a mocked Query that I can return my test list from when I call getResultList.

I've been looking at different syntax possibilities, but can't find a correct match. It would be easier if all the method calls were on entityManager and not on an object that it returns.

Forgot to mention that the error I keep getting when trying to mock this is "can't setParameter on null object".

225035 (5) [Avatar] Offline
I made a mistake. There is not createNativeQuery - there is createNamedQuery. Once I corrected the typo I was able to mock Query and EntityManager.
Kostis Kapelonis (63) [Avatar] Offline

Know that a new article is in the works that will talk exclusively about Mocking with Spock!