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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:03:21+00:00 2026-05-17T03:03:21+00:00

The question: Is there a way I can use a ClassLoader to test both

  • 0

The question: Is there a way I can use a ClassLoader to test both the presence and absence of the library I’m checking for?

I have a some code which will use a particular library if available, or fall back to some embedded code if not. It works fine but I’m not sure how to unit test both cases in the same run, or if it’s even possible. At the moment, my unit tests only check one case because the library is either in the main classpath or it’s not.

The code I use to check for the library availability is basically:

boolean available;
try {
  Class.forName("net.sf.ehcache.CacheManager");
  available = true;
} catch (ClassNotFoundException e) {
  available = false;
}

I could potentially alter the way this is done, if it would make unit testing easier.

  • 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-17T03:03:22+00:00Added an answer on May 17, 2026 at 3:03 am

    You are trying to do two things at once:

    • test the different implementations of your code, and
    • test that the correct implementation is selected based on the circumstances (or, more generally, that your app works regardless of whether or not the library in question is present).

    Separating the two tasks into distinct tests simplifies the problem. Thus, I would implement the two versions of your code as two Strategies, and put the code which checks the classpath and creates the necessary strategy into a Factory (Method). Then both strategies can be unit tested independent of classloader and classpath settings:

    interface MyStrategy {
      public void doStuff();
    }
    
    class MyLibraryUsingStrategy implements MyStrategy {
      public void doStuff() {
        // call the library
      }
    }
    
    class MyEmbeddedStrategy implements MyStrategy {
      public void doStuff() {
        // embedded code
      }
    }
    

    In unit tests, you can simply create and test either of the concrete strategies:

    @Test
    void testEmbeddedStrategy() {
      MyStrategy strategy = new MyEmbeddedStrategy();
      strategy.doStuff();
      // assert results
    }
    

    A simple factory method to create the appropriate strategy:

    MyStrategy createMyStrategy() {
      MyStrategy strategy;
      try {
        Class.forName("net.sf.ehcache.CacheManager");
        strategy = new MyLibraryUsingStrategy();
      } catch (ClassNotFoundException e) {
        strategy = new MyEmbeddedStrategy();
      }
      return strategy;
    }
    

    Since the factory code is fairly trivial, you may even decide not to write automated tests for it. But at any rate, testing the factory is more (part) of an integration test than a unit test; you can simply put together different setups of your app – one with and the other without the library in question – and see that both work properly.

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

Sidebar

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.