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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:31:47+00:00 2026-06-05T20:31:47+00:00

When trying to run a junit test I get the following error – java.lang.ClassCastException:

  • 0

When trying to run a junit test I get the following error –

java.lang.ClassCastException: business.Factory cannot be cast to services.itemservice.IItemsService
at business.ItemManager.get(ItemManager.java:56)
at business.ItemMgrTest.testGet(ItemMgrTest.java:49)

The specific test that is causing the problem is

@Test
public void testGet() {
        Assert.assertTrue(itemmgr.get(items));
}

The code it is testing is…

public boolean get(Items item)  { 

        boolean gotItems = false;       

        Factory factory = Factory.getInstance();

        @SuppressWarnings("static-access")
        IItemsService getItem = (IItemsService)factory.getInstance();

        try {
            getItem.getItems("pens", 15, "red", "gel");
            gotItems = true;
        } catch (ItemNotFoundException e) {
            // catch
            e.printStackTrace();
            System.out.println("Error - Item Not Found");
        }
        return gotItems;
    }

The test to store items, which is nearly identical, works just fine…

The factory class is..

public class Factory {

    private Factory() {}
    private static Factory Factory = new Factory();
    public static Factory getInstance() {return Factory;}




    public static IService getService(String serviceName) throws ServiceLoadException {
        try {
            Class<?> c = Class.forName(getImplName(serviceName));
            return (IService)c.newInstance();
        } catch (Exception e) {
            throw new ServiceLoadException(serviceName + "not loaded");
        }
    }



    private static String getImplName (String serviceName) throws Exception {
        java.util.Properties props = new java.util.Properties();
            java.io.FileInputStream fis = new java.io.FileInputStream("config\\application.properties");
                props.load(fis);
                    fis.close();
                    return props.getProperty(serviceName);
}
}
  • 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-05T20:31:48+00:00Added an answer on June 5, 2026 at 8:31 pm

    Your Factory.getInstance method returns a Factory object, and Factory is not IItemsService. Maybe you need to change the following:

    @SuppressWarnings("static-access")
    IItemsService getItem = (IItemsService)factory.getInstance();
    

    to:

    @SuppressWarnings("static-access")
    IItemsService getItem = (IItemsService)factory.getService(serviceName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run JUnit test with a 1.5 JRE, but get the
I'm trying to get JUnit 4 tests to properly run in Eclipse Indigo SR1,
I'm trying to run Jettys ServletTester in my JUnit test. I created a simple
I'm trying to run Junit test from my ant build.xml file. I read here
When trying to run test with JMockit on OpenJDK 6, I'm facing the error:
I'm trying to run my junit test (to verify that a properties file loads
I am new to Java and am trying to run a unit test on
I am receiving the following error when I attempt to run my Spring JUnit
I'm trying to run some JUnit test units in Eclipse 3.5, but with no
I am trying to run android instrumentation Junit test using command line.I am using

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.