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 8367353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:08:02+00:00 2026-06-09T13:08:02+00:00

I have coded a JPA repository method and I am now realizing it is

  • 0

I have coded a JPA repository method and I am now realizing it is impossible to unit test.

Can anyone please advise how to unit test the following method or how to refactor my repository so that it is unit-testable?

Here is the problematic method:

@Override
public List<Pli> findPlisByMultiField(String identifiant, Date dateReceptionFrom, Date dateReceptionTo, PaiementEnum paiement, AREnum ar, String numeroAR, FDVEnum FDV, ConteneurNum conteneurNum, StatutPli statut) {
    log.debug("findPlisByMultiField");

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Pli> c = criteriaBuilder.createQuery(Pli.class);
    Root<Pli> pli = c.from(Pli.class);

    List<Predicate> criteria = new ArrayList<Predicate>();

    if (identifiant != null && !identifiant.trim().equals("")) {
        ParameterExpression<String> parameterIdentifiant = criteriaBuilder.parameter(String.class, "identifiant");
        Predicate conditionIdentifiant = criteriaBuilder.like(pli.<String> get("identifiant"), parameterIdentifiant);
        criteria.add(conditionIdentifiant);
    }

    if (dateReceptionFrom != null && dateReceptionTo != null) {
        ParameterExpression<Date> parameterDateReceptionFrom = criteriaBuilder.parameter(Date.class, "dateReceptionFrom");
        ParameterExpression<Date> parameterDateReceptionTo = criteriaBuilder.parameter(Date.class, "dateReceptionTo");
        Predicate conditionDateReception = criteriaBuilder.between(pli.<Date> get("dateReception"), parameterDateReceptionFrom, parameterDateReceptionTo);
        criteria.add(conditionDateReception);
    }

    if (paiement != null) {
        if (paiement.equals(PaiementEnum.IsPaiement)) {
            Predicate conditionPaiementEnum = criteriaBuilder.equal(pli.<PaiementEnum> get("paiement"), true);
            criteria.add(conditionPaiementEnum);
        } else {
            Predicate conditionPaiementEnum = criteriaBuilder.equal(pli.<PaiementEnum> get("paiement"), false);
            criteria.add(conditionPaiementEnum);
        }
    }

    if (ar != null) {
        if (ar.equals(AREnum.IsAR)) {
            Predicate conditionAREnum = criteriaBuilder.equal(pli.<AREnum> get("AR"), true);
            criteria.add(conditionAREnum);
        } else {
            Predicate conditionAREnum = criteriaBuilder.equal(pli.<AREnum> get("AR"), false);
            criteria.add(conditionAREnum);
        }
    }

    if (numeroAR != null && !numeroAR.trim().equals("")) {
        ParameterExpression<String> parameterNumeroAR = criteriaBuilder.parameter(String.class, "numeroAR");
        Predicate conditionNumeroAR = criteriaBuilder.like(pli.<String> get("numeroAR"), parameterNumeroAR);
        criteria.add(conditionNumeroAR);
    }

    if (FDV != null) {
        if (FDV.equals(FDVEnum.IsFDV)) {
            Predicate conditionFDVEnum = criteriaBuilder.equal(pli.<FDVEnum> get("FDV"), true);
            criteria.add(conditionFDVEnum);
        } else {
            Predicate conditionFDVEnum = criteriaBuilder.equal(pli.<FDVEnum> get("FDV"), false);
            criteria.add(conditionFDVEnum);
        }
    }

    if (conteneurNum != null) {
        ParameterExpression<ConteneurNum> parameterConteneurNum = criteriaBuilder.parameter(ConteneurNum.class, "conteneurNum");
        Predicate conditionConteneurNum = criteriaBuilder.equal(pli.<ConteneurNum> get("conteneurNum"), parameterConteneurNum);
        criteria.add(conditionConteneurNum);
    }

    if (statut != null) {
        ParameterExpression<StatutPli> parameterStatut = criteriaBuilder.parameter(StatutPli.class, "statut");
        Predicate conditionStatut = criteriaBuilder.equal(pli.<StatutPli> get("statut"), parameterStatut);
        criteria.add(conditionStatut);
    }

    if (criteria.size() == 0) {
        return Pli.findAllPlis();
    } else if (criteria.size() == 1) {
        c.where(criteria.get(0));
    } else {
        c.where(criteriaBuilder.and(criteria.toArray(new Predicate[0])));
    }

    TypedQuery<Pli> q = em.createQuery(c);
    if (identifiant != null && !identifiant.trim().equals("")) {
        q.setParameter("identifiant", "%" + identifiant + "%");
    }
    if (dateReceptionFrom != null && dateReceptionTo != null) {
        q.setParameter("dateReceptionFrom", dateReceptionFrom);
        q.setParameter("dateReceptionTo", dateReceptionTo);
    }

    if (numeroAR != null && !numeroAR.trim().equals("")) {
        q.setParameter("numeroAR", "%" + numeroAR + "%");
    }

    if (conteneurNum != null) {
        q.setParameter("conteneurNum", conteneurNum);
    }

    if (statut != null) {
        q.setParameter("statut", statut);
    }

    return q.getResultList();
}
  • 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-06-09T13:08:03+00:00Added an answer on June 9, 2026 at 1:08 pm

    Well, I don’t think you will be able to Unit Test it as it’s strictly specified, but you can make a test using an in memory database (look out for HSQL) so that the app don’t need to actually go to the real database just for testing.

    That way you will be able to create an automated test that could run inside JUnit for example, mocking maybe only some of the methods.

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

Sidebar

Related Questions

How can I map an array of Doubles in JPA. I have the following
today I have coded a test case for my application, to see how transactions
In my program I have coded the following private variables and function. private string
I have a problem. I'm learning JPA. I'm using embedded OpenEJB container in unit
I have a JPA entity class (one of many) and I can run JPQL
I have a question concerning Hibernate 3.6.7 and JPA 2.0. Consider following entities (some
Hello every body I have used following code in my jpa project for working
I have a following problem with entity mapping in JPA. I have two entities,
I have coded a server in Java that will have several clients connected to
I have coded a background logging thread for my program, if a class needs

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.