1

I am trying to write test case for Repository method. In that Test case I want to test Investigator name using assertEquals(). Return type of method is set for that I use for each loop to retrieve result from set and then checked expected and actual result using assertEquals() but I am getting java.lang.ClassCastException: java.util.HashMap cannot be cast to com.spacestudy.model.Investigator

can any one please tell me what I am doing wrong in test case?

InvestigatorRepository

@Query("select new map(invest.sInvestigatorName as sInvestigatorName)"
            + " from Investigator invest")
    Set<Investigator> findSinvestigatorName();

I tried Like this

@RunWith(SpringRunner.class)
@DataJpaTest
public class TestInvestigatorRepository {

    @Autowired
    public TestEntityManager testEm;

    @Autowired
    InvestigatorRepository investRepo;

    @Test
    public void testFindSinvestigatorName() {

        Investigator invest = new Investigator();
        invest.setsInvestigatorName("abc");
        invest.setnInstId(60);

        Investigator saveInDb = testEm.merge(invest);

        Set<Investigator> getFromDb = investRepo.findSinvestigatorName();

        for(Investigator result : getFromDb) {

            assertEquals(saveInDb.getsInvestigatorName(),result.sInvestigatorName); 
        }     

    }
}

Stack Trace

java.lang.ClassCastException: java.util.HashMap cannot be cast to com.spacestudy.model.Investigator
    at com.spacestudy.repository.TestInvestigatorRepository.testFindSinvestigatorName(TestInvestigatorRepository.java:39)
    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:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
SpringUser
  • 931
  • 1
  • 18
  • 42

1 Answers1

1

The HQL query you specify explicitly returns a Map, but the return type specified for the method specifies Set<Investigator> which are incompatible types, hence the error.

Spring Data tries to convert the types but fails and in the end, just tries to cast the result to the desired type.

In order to fix this you need to use compatible types options are:

  • let your query return Investigators.
  • let your query return a tuple by just listing the attributes: select invest.sInvestigatorName as sInvestigatorName from Investigator invest. Spring Data should be able to map that to an Investigator although your "interesting" property names might cause some troubles.
  • Since you are really just return a single attribute you might as well make the method return a Set<String> and use the query given in the previous point.
Jens Schauder
  • 65,795
  • 24
  • 148
  • 294
  • I tried with second option I removed new map from Query but then result is not getting in ` key ,value` pair – SpringUser Jun 27 '18 at 08:28
  • Sorry, I don't understand that comment. – Jens Schauder Jun 27 '18 at 08:43
  • I tried with this Query `select invest.sInvestigatorName as sInvestigatorName from Investigator invest` . I am not getting result in json format . I want result in json format – SpringUser Jun 27 '18 at 09:30
  • Sorry, but that is a completely different question. Please start a new one for that. JSON wasn't even mentioned in the original question. – Jens Schauder Jun 27 '18 at 09:34
  • I want result in json format. I am getting that result using `new map` in Query. so I can not change that. for solving `java.lang.ClassCastException` what i can do – SpringUser Jun 27 '18 at 09:45
  • How do you get the result in JSON when you don't get a result at all because you get a `ClassCastException`? I suggest again: create a new question stating the actual problem you want solved. – Jens Schauder Jun 27 '18 at 09:48