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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:39:58+00:00 2026-05-25T06:39:58+00:00

Below is a method that I’m having a hard time figuring out how to

  • 0

Below is a method that I’m having a hard time figuring out how to test using JUnit.
This method is difficult to test because it depends on the results of other methods (e.g. getClosestDcoumentCode).

Based on my reading of JUnit, this suggests I should refactor the method. But how? And if refactoring is not necessary, how do you test a method that depends on other methods?

Thank you,

Elliott

private static String findPrincipal(List<DocumentKey> documentkeys_) {
    Hashtable<String, Integer> codecounts = new Hashtable<String, Integer>();
    for (DocumentKey document : documentkeys_) {
        int x = 0;
        String closestCode = getClosestDocumentCode(document.candidates);
        if (closestCode == null) continue;
        int thecount = 0;
        if (codecounts.containsKey(closestCode))
            thecount = codecounts.get(closestCode);
        if (document.hasKey)
            thecount += 2;
        else
            thecount++;
        codecounts.put(closestCode, new Integer(thecount));
        x++;

    }
    String closestCode = getClosestCode(codecounts);
    return closestCode;
}
  • 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-25T06:39:58+00:00Added an answer on May 25, 2026 at 6:39 am

    It sounds to me like getClosestCode and getClosestDocumentCode belong to a different set of responsibilities than the findPrincipal method. So you’ll want to begin by separating these into two different classes. Create an interface for each class to implement. The class that implements the findPrincipal method can then rely on the other interface as a constructor argument, like this:

    public class PrincipalFinderImpl implements PrincipalFinder
    {
        private CodeFinder codeFinder;
        public PrincipalFinderImpl(CodeFinder codeFinder) {
            this.codeFinder = codeFinder;
        }
        public String findPrincipal(List<DocumentKey> documentkeys_) {
            Hashtable<String, Integer> codecounts = new Hashtable<String, Integer>();
            for (DocumentKey document : documentkeys_) {
                int x = 0;
                String closestCode = codeFinder.getClosestDocumentCode(document.candidates);
                if (closestCode == null) continue;
                int thecount = 0;
                if (codecounts.containsKey(closestCode))
                    thecount = codecounts.get(closestCode);
                if (document.hasKey)
                    thecount += 2;
                else
                    thecount++;
                codecounts.put(closestCode, new Integer(thecount));
                x++;
    
            }
            String closestCode = codeFinder.getClosestCode(codecounts);
            return closestCode;
        }
    }
    

    Now it should be easy to create another class the implements the CodeFinder interface, either manually or using a Mocking framework. You can then control the results of each call to getClosestCode and getClosestDocumentCode, and ensure that each of these methods gets called with exactly the arguments you expect it to be called with.

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

Sidebar

Related Questions

I'm using iTextSharp to generate PDFs. I've added a test method below that makes
Currently i am creating an extension method that accepts parameters. Using the below example,
I am using below method for validating that one of radio button must be
The method below that updates the database is called UpdateHold(). This instance of the
The problem is that this is a JAAS login application. The below method uses
Below is a method that I want to ship off into a background worker
I Send Iqueryable to a function that is below void Method<T>(ref IQueryable<T> qu) {
I'm using the below method to reply to mails coming in to a business
In the method below titleForHeaderInSection , for some reason the NSLog prints out the
I have the below LINQ method that I use to create the empty EmploymentPLan

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.