java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

Sathish David  Kumar N
Sathish David  Kumar N used Ask the Experts™
on
PowerMockito.when(this.jdbcTemplate.queryForObject(Matchers.anyString(), Matchers.<Object[]> any(),
                        Matchers.<Class<Long>> any())).thenReturn(0L);

 PowerMockito.when(this.jdbcTemplate.queryForObject(Matchers.anyString(), Matchers.<Object[]> any(),
                        Matchers.<Class<Integer>> any())).thenReturn(1);

I have right PowerMockito test case for my class . both queryForObject in same method
while execute  2 second queryForObject in my main class  its giving error  
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Long to Integer :

Integer myInteger = new Integer(myLong.intValue());
Top Expert 2016

Commented:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
      XXXXXXXXXXXXXXXXXXXXXXX(This is our class details)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
      at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
      at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
      at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
      at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
      at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
      at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
      at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
      at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
      at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
      at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

@Test
    public void executeTest() {



        PowerMockito.when(this.jdbcTemplate.queryForObject(Matchers.anyString(), Matchers.<Object[]> any(),
                        Matchers.<Class<Long>> any())).thenReturn(0L);
        PowerMockito.when(this.jdbcTemplate.queryForObject(Matchers.anyString(), Matchers.<Object[]> any(),
                        Matchers.<Class<Integer>> any())).thenReturn(1);

        try {
            this.classObject.execute( this.cContext);
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
Top Expert 2016

Commented:
The third parameter of the method you're testing is Long/long?
if (filename.startsWith(test)) {
                countQuery = this.query1;
            } else if (filename.startsWith(Constants.ID) {
                countQuery = this.queryCountCond;
            }
            final long reCount = this.jdbcTemplate
                    .queryForObject(countQuery, new Object[] { filename }, Long.class);

   ----------------------  some logic ------------------
            // Update the status in x tble
            this.jdbcTemplate.update(this.query4, new Object[] { status, filename });

            //  failure records insert
            if (!Constants.SUCCESS.equalsIgnoreCase(status)) {
                errRecCount = this.jdbcTemplate.queryForObject(this.errQuery,
                        new Object[] { filename }, Integer.class);    //integer is RowMapper<T> here its fail
                                    
      again some logic
yes I changed lot of code

actual one this
   PowerMockito.when(this.jdbcTemplate.queryForObject(Matchers.anyString(), Matchers.<Object[]> any(),
                        Matchers.<Class<Long>> any())).thenReturn(new Long(10));
        PowerMockito.when(this.jdbcTemplate.queryForObject(Matchers.anyString(), Matchers.<Object[]> any(),
                        Matchers.<Class<Integer>> any())).thenReturn(new Integer(1));
the problem is second time mock jdbctemplatequeryForObject
Top Expert 2016

Commented:
I don't think i'm conversant enough with these different specialized APIs to answer this sorry
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
I also don't have a lot of experience in this area, but I have a go and nothing really lost if it doesn't work...

I think the issue may be to do with type erasure. I think that the testing framework need to know if it is an Integer or Long at runtime, by which time those types are erased and so it can't distinguish the calls. Can you try this instead?

PowerMockito.when(this.jdbcTemplate.queryForObject(Matchers.anyString(), Matchers.<Object[]> any(),
                        Matchers.eq(Long.class))).thenReturn(new Long(10));
PowerMockito.when(this.jdbcTemplate.queryForObject(Matchers.anyString(), Matchers.<Object[]> any(),
                        Matchers.eq(Integer.class))).thenReturn(new Integer(1));

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial