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

  • Home
  • SEARCH
  • 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 6068703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:44:37+00:00 2026-05-23T09:44:37+00:00

I want to write a Junit test for my dao, but I have a

  • 0

I want to write a Junit test for my dao, but I have a problem. Here is the method I want to test:

 public boolean boo(final String param) {
            final Query query = this.entityManager.createNamedQuery("queryName");
            query.setParameter(1, param);
            boolean isExists = false;
            if(query.getResultList().size() != 0) {
                isExists = true;
            }
            return isExists;
        }

The problem with this method is :

query.setParameter(1, param);

When I write something like :

   @Test
    public void test() {        
        when(entityManager.createNamedQuery(queryName)).thenReturn(query);
        when(query.getResultList()).thenReturn(new ArrayList());
        //when(query.setParameter(1,project.getName())).thenCallRealMethod();
        projectDao.boo(name);

    }

The query and entityManager are mocked.
I have NPE, and this is not a surprise, and I cannot call the method because the query is and interface.
So could somebody tell me the best way to set parameters in NamedQueries while testing?

  • 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-23T09:44:37+00:00Added an answer on May 23, 2026 at 9:44 am

    You’re supposed to be creating a mock of the Query interface like this…

    @Test
    public void test() { 
    
        Query query = mock(Query.class);
    
        when(entityManager.createNamedQuery(queryName)).thenReturn(query);
    
        ...
    

    Maybe you forgot the double quotes around the String literal “queryName”. From your code I cannot see where the variable queryName is defined on the last line above.

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

Sidebar

Related Questions

I want to write a Robotium/Junit test for an android app. At certain steps,
I have the following JUnit test. The method that I'm testing is quite simple,
I have a very simple method that I am trying to unit test: public
I want to write an JUnit @Rule (version 4.10) to setup some objects (an
I have an ant build file that contains JUnit test suite that I would
suppose we have two numbers i want write program which print common bits subsequent
I am trying to write a unit test for the following class: @Transactional public
I have a class like this: public final class Foo { public native int
I'm trying to write JUnit tests for my code but with in some of
I am fairly new to Java. I have constructed a single JUnit test class

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.