Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 683279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:41:23+00:00 2026-05-14T01:41:23+00:00

I came from the Spring camp , I don’t want to use Spring ,

  • 0

I came from the Spring camp , I don’t want to use Spring , and am migrating to JavaEE6 ,
But I have problem testing DAO + JPA , here is my simplified sample :

public interface PersonDao
{
  public Person get(long id);
}

This is a very basic DAO , because I came from Spring , I believe DAO still has its value , so I decided to add a DAO layer .

public class PersonDaoImpl implements PersonDao , Serializable
{
  @PersistenceContext(unitName = "test", type = PersistenceContextType.EXTENDED)
  EntityManager entityManager ;

  public PersonDaoImpl()
  {
  }

  @Override
  public Person get(long id)
  {
    return entityManager .find(Person.class , id);
  }
}

This is a JPA-implemented DAO , I hope the EE container or the test container able to inject the EntityManager (just like Spring does).

public class PersonDaoImplTest extends TestCase
{
  @Inject 
  protected PersonDao personDao;

  @Override
  protected void setUp() throws Exception
  {
    //personDao = new PersonDaoImpl();
  }

  public void testGet()
  {
    System.out.println("personDao = " + personDao); // NULL !
    Person p = personDao.get(1L);
    System.out.println("p = " + p);
  }
}

This is my test file .

OK , here comes the problem :
Because JUnit doesn’t understand @javax.inject.Inject , the PersonDao will not be able to injected , the test will fail.

How do I find a test framework that able to inject the EntityManager to the PersonDaoImpl , and @Inject the PersonDaoImpl to the PersonDao of TestCase ?

I tried unitils.org , but cannot find a sample like this , it just directly inject the EntityManagerFactory to the TestCast , not what I want …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:41:23+00:00Added an answer on May 14, 2026 at 1:41 am

    because I came from Spring, I believe DAO still has its value, so I decided to add a DAO layer.

    I don’t really see what Spring has to do with this. And I don’t agree as I wrote in a previous answer. To me, JPA is a DAL (data access layer), and I don’t see the point of putting a data access layer on top of another data access layer. At least not systematically. But let’s not discuss this.

    This is a JPA-implemented DAO , I hope the EE container or the test container able to inject the EntityManager (just like Spring does).

    If your DAO is a managed component like a CDI managed bean, then the Java EE container should be able to inject an EntityManager in it.

    For unit testing of container-managed objects, you don’t need any kind of container. For integration testing, you will need some kind of container, just like you do for Spring beans, Hibernate/JPA entities, session beans, CDI managed beans or any other kind of container-managed object. You could use the EJB3.1 embeddable API in your tests. Also have a look at Arquillian.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came from PHP language(codeigniter), but now I learning ASP.Net MVC :) In PHP
I have a form with a hidden Came from Adwords field that will be
let me tell you a bit about where this question came from. I have
I came from a java background, and I was trying to use protocol like
So I'm using Spring Web Flow on a project and want to make use
Edit 4: the problem came from using the string name of the column instead
Let's suppose I have a string that contains 7.2769482308e+01 (this number came from 3rd
I am very new to use spring-data-jpa, but it looks very promising. I used
Disclaimer: May be a insane question but I have suffered a lot so came
I came from Java, and now I am working more with Ruby. One language

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.