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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:23:17+00:00 2026-06-10T22:23:17+00:00

I have been pushing myself to learn more and more Unit Testing techniques as

  • 0

I have been pushing myself to learn more and more Unit Testing techniques as of late and ran into something I just can’t figure out- namely, how to even get started. I am trying to unit test a factory with a very, very simple single method. I have simplified it using example names for you helpful folks.

public class HandlerFactory extends SecondHandlerFactory
{
    //To hold singleton of this class.
    private static SecondHandler factoryInstance = null;

    private HandlerFactory() throws HandlerCreationException
    {
            super();
    }

    protected InterfaceExample createSomethingByThisKey(String key) throws HandlerCreationException
    {
        InterfaceExample myNewHandler = null;

        if (StringFunctions.isEqualIgnoreCase(key, "Phone"))
        {
            myNewHandler = new PhoneHandler();
        }
    }
}

I’m having major trouble figuring out to set up this test to even fall into the createSomethingByKey(). Even then, I can’t even figure out how I would do my assertEquals() method correctly.

Right now, I have stuff set up to to do a createInstance(); but nothing seems to make any sense to me in how to fall into this. The added variable and interfaces are making me hella confused here. It seems simple to test and is a very, very short class- but not being exposed to this (Java isn’t even my first language) is kind of a hard wall for me to get over).

Any help on how to set up either a standard JUnit or Mockito (doesn’t matter really) on how to set all this up? Also the fact that’s protected is making me even more clueless since proper standard is to have Unit Tests in another package.

  • 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-10T22:23:19+00:00Added an answer on June 10, 2026 at 10:23 pm

    Honestly I am not sure what’s your difficulties. I can only guess 2:

    1. You don’t know what to test on such factory.
    2. You have problem testing a protected method

    For 1, it is in fact not that difficult, just assume the method you are testing is public:

    public class HandlerFactory extends SecondHandlerFactory
    {
        public InterfaceExample createSomethingByThisKey(String key) throws HandlerCreationException
        {
            InterfaceExample myNewHandler = null;
    
            if (StringFunctions.isEqualIgnoreCase(key, "Phone"))
            {
                myNewHandler = new PhoneHandler();
            }
        }
    }
    

    The logic in this method is simply creating a handler instance base on the key. So your test should aim to see if correct type of handler is created according to your key. Therefore the tests should be something like:

    @Test
    testCreateSomethingByThisKeyGivenKeyForPhoneKey {
        InterfaceExample result = handelerFactory.createSomethingByThisKey("Phone");
        assertTrue("result is a PhoneHandler", result instanceof PhoneHandler);
    }
    
    @Test
    testCreateSomethingByThisKeyGivenKeyForUnknownKey {
        InterfaceExample result = handelerFactory.createSomethingByThisKey("NONEXIST");
        assertNull("result", result);
    }
    

    For problem 2 (restricted access to protected method), you usually have 2 choices:
    We normally have test sits in same package as the system under test (SUT), so you should be able to invoke the protected method.

    You may also choose to create a Test-Specific Subclass (TSS) which expose a method which directly delegate to the protected method in SUT (or override to loosen the visibility of the method), and have your test performed against the TSS. Of course, you should make sure that your SUT can be extended. It may looks something like this

    public class Sut {
      protected Foo methodToTest(Param param) {
        //....
      }
    }
    

    Create a TSS like this:

    public class SutTss extends Sut {
      @Override
      public Foo methodToTest(Param param) {  // visibility loosen
        return super.methodToTest(param);
      }
    }
    

    Then you can freely test on that protected method (it is seldomly used in Java I believe, coz we can always invoke protected method by putting test under same package as SUT)

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

Sidebar

Related Questions

I have been pushing projects to Heroku without any issues. Just installed the Heroku_Accounts
I have been running into some issues with animating multiple CALayers at the same
I have been developing locally for some time and am now pushing everything to
I have been following the excellent book Programming Ground Up, wanting to learn assembly.
so, just wondering if this is even possible i have been using the editor
I have been asked for a school project to submit my work by pushing
I'm on holidays at present and have been doing some work, committing and pushing
I have been playing with ASP.NET Web API. I am looking to see can
Have been trying to encrypt an xml file to a string so that I
Have been working on this question for a couple hours and have come close

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.