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

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
LVL 20
Sathish David  Kumar NArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Long to Integer :

Integer myInteger = new Integer(myLong.intValue());
0
CEHJCommented:
0
Sathish David Kumar NArchitectAuthor 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)
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Sathish David Kumar NArchitectAuthor Commented:
@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();
        }
    }
0
CEHJCommented:
The third parameter of the method you're testing is Long/long?
0
Sathish David Kumar NArchitectAuthor Commented:
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
0
Sathish David Kumar NArchitectAuthor Commented:
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));
0
Sathish David Kumar NArchitectAuthor Commented:
the problem is second time mock jdbctemplatequeryForObject
0
CEHJCommented:
I don't think i'm conversant enough with these different specialized APIs to answer this sorry
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JUnit

From novice to tech pro — start learning today.