• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Unit Testing with a Persistence Context

 
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm struggling to find anything that will help explain how to unit test when I have an EntityManager (EM) injected into my bean using the @PersistenceContext annotation. I found this great article, but as far as I can tell I can't inject a mock EM. If it is possible to do so, please can someone explain how or point me to some references? Alternatively, is there any way to mock a PU and database access so I am not accessing a database for every test?

Edit: I am using JUnit and Mockito.
Edit: Actually added the link to the article!
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I imagine you're declaring your EM something like this:



Why not just create a package private setter for the field?



Now the framework gives you an EM at runtime. But at test time, you can inject your mock.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alternatively, is there any way to mock a PU and database access so I am not accessing a database for every test?



Unit tests should not access the database. But you should try your hand at integration tests.

Most classes you write should implement an interface in order to be able to test one class at a time during unit testing.
 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I am more than a little stuck, and whilst I'm happy to just ignore any kind of testing whilst trying to learn some of the EE technologies, it's something I really need to get to grips with. I'm making a small little app where a user can register, login and keep track of the costs involved in buying, selling and moving from an old to a new house. I want to test that I can create a user, here's what I have so far:

User.java

UserBeanLocal.java

UserBean.java

UserBeanTest.java

Whilst I have some code in 2 of the methods and I know they work because I used a singleton bean to test them, it's not really in the spirit of unit testing or TDD. As mentioned right at the top, I'm a little perplexed as to how to continue. Any chance of a leg up?
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashley Bye wrote:Whilst I have some code in 2 of the methods and I know they work because I used a singleton bean to test them, it's not really in the spirit of unit testing or TDD. As mentioned right at the top, I'm a little perplexed as to how to continue. Any chance of a leg up?



I don't understand why you need a singleton bean.



To unit test this mock, I'd....



Now I don't use/know Mockito. I use JMock. I could help you with syntax if you aren't tied to mockito.
 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:I don't understand why you need a singleton bean.



Nope, I don't either. I probably meant to go for stateless and just wasnt paying attention to what I wrote!

Janeice DelVecchio wrote:I use JMock. I could help you with syntax if you aren't tied to mockito.



I'll have a think about that one but appreciate the offer of help. Having just had a look through the syntax for JMock I find that of mockito easier to read. However, since I haven't really done any unit testing before and am at a omplete loss as to where / how to start (other than knowing I should test each unit) it might not make that much difference. Can I get back to you on the one? I suspect what I will do is finish this project so I know how the various elemnts interact, then tackle it again with tests to see just how different the final outcome is; plus I'll have a better idea of what I'm doing programming-wise and what I can and can't do with the various elements!
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic