2

I am writing a Unit Test for a class as follows:

@Test
void testCreateStackResources()
{
    List<StackResource> stackResourceListExpected = new ArrayList<>();
    StackResource stackResource = new StackResource();
    stackResource.setLogicalResourceId("Sample-Logical-ID");
    stackResourceListExpected.add(stackResource);
    ListStackResourcesResult listStackResourcesResult = new ListStackResourcesResult();
    StackResourceSummary stackResourceSummary = new StackResourceSummary();
    stackResourceSummary.setLogicalResourceId("Sample-Logical-ID");
    listStackResourcesResult.setStackResourceSummaries((Collection<StackResourceSummary>) stackResourceSummary); // Problem in this line
    Mockito.when(amazonCloudFormation.listStackResources(Mockito.any(ListStackResourcesRequest.class))).thenReturn(listStackResourcesResult);
    List<StackResource> stackResourceListResult = cloudFormationManager.createStackResources(Mockito.anyString());
    Assert.assertEquals(stackResourceListExpected, stackResourceListResult);
}

Now, when I run this code, it gives me an error that I can't cast StackResourceSummary to a Collection in Java.

java.lang.ClassCastException: com.amazonaws.services.cloudformation.model.StackResourceSummary cannot be cast to java.util.Collection

On the other hand, if I make an array list before, add the object of StackResourceSummary to the list and then run the UT, it gives me the

objc[3648]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x10d19c4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10ea194e0). One of the two will be used. Which one is undefined.

This is very weird behaviour. I don't know why can't I cast this to a collection? Please help. Thanks!

PS: There is a seperate class called ListStackResourcesResult which has a setter as follows:

public void setStackResourceSummaries(java.util.Collection<StackResourceSummary> stackResourceSummaries) {
    if (stackResourceSummaries == null) {
        this.stackResourceSummaries = null;
        return;
    }

    this.stackResourceSummaries = new com.amazonaws.internal.SdkInternalList<StackResourceSummary>(stackResourceSummaries);
}

And I am trying to use this method above.

sarah
  • 247
  • 1
  • 9
  • 19

1 Answers1

1

That is because StackResourceSummary does not extend or implement anything related to a collection.

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/cloudformation/model/StackResourceSummary.html

What you need to to is create a collection and add your instance of StackResourceSummary to it. For example like so:

List<StackResourceSummary> stackResourceSummaries = new ArrayList<StackResourceSummary>();
stackResourceSummaries.add(stackResourceSummary)

or maybe like so

Arrays.asList(stackResourceSummary)

or use a third party lib like guava collections.

Then you should use that collection as an argument.

listStackResourcesResult.setStackResourceSummaries(stackResourceSummaries); // Problem gone in this line
ipper
  • 624
  • 4
  • 13
  • I did what you suggested beforehand too. That is make a list first, and then add `stackResourceSummary` to it. But that doesn't work. When I run the test, it gives me the second error describe in the above post. – sarah Jun 23 '17 at 06:45
  • That error message is a completely different issue / question. See https://stackoverflow.com/questions/18794573/objc10012-class-javalaunchhelper-is-implemented-in-both-libinstrument-dyl – Ryan Leach Jun 23 '17 at 06:59
  • That second error has nothing to do with your code. Are you running the test from cli directly (mvn test for example) or from an ide. InteliJ appears to have some issues https://stackoverflow.com/questions/42962987/after-upgrade-to-intellij-2017-1-class-javalaunchhelper-error – ipper Jun 23 '17 at 06:59