### Question:
AwsEC2Platform extends Platform implements SshBasedExperiment<AwsEC2Container> { AwsEC2Container createContainerFromInstance (Instance instance) { if (instance.getState().getCode() == AwsEC2Constants.AWS_TERMINATED_CODE) return null; AwsEC2Container container = containerManager.getMatchingContainer(AwsEC2Container.class, instance.getInstanceId()); if (container == null) { container = buildContainerFromInstance(instance); log.info("Found new AWS EC2 Container {}", v(DATADOG_CONTAINER_KEY, container)); containerManager.offer(container); } else { log.debug("Found existing AWS EC2 Container {}", v(DATADOG_CONTAINER_KEY, container)); } return container; } @Autowired  AwsEC2Platform(AmazonEC2 amazonEC2, ContainerManager containerManager, AwsEC2SelfAwareness awsEC2SelfAwareness); private  AwsEC2Platform(); void setFilter(@NotNull Map<String, List<String>> filter); void setGroupingTags(List<String> groupingTags); void setSSHPrivateKeys(@NotNull Map<String, String> sshPrivateKeys); Map<String, String> getImageIdToUsernameMap(); void setImageIdToUsernameMap(Map<String, String> imageIdToUsernameMap); @Override ApiStatus getApiStatus(); @JsonIgnore Collection<SubnetUtils.SubnetInfo> getRoutableCidrBlocks(); void setRoutableCidrBlocks(Collection<String> routableCidrBlocks); boolean isAddressRoutable(String privateIPAddress); @Override PlatformLevel getPlatformLevel(); @Override PlatformHealth getPlatformHealth(); @Override List<Container> generateRoster(); @Override boolean isContainerRecycled(Container container); ContainerHealth checkHealth(String instanceId); void stopInstance(String... instanceIds); void terminateInstance(String... instanceIds); void startInstance(String... instanceIds); void restartInstance(String... instanceIds); void setSecurityGroupIds(String networkInterfaceId, Collection<String> securityGroupIds); String getChaosSecurityGroupForInstance(String instanceId); boolean isContainerTerminated(String instanceId); boolean isAutoscalingGroupAtDesiredInstances(String autoScalingGroupName); boolean hasKey(String keyName); @Override void recycleContainer(AwsEC2Container container); void triggerAutoscalingUnhealthy(String instanceId); @Override String getEndpoint(AwsEC2Container container); @Override SSHCredentials getSshCredentials(AwsEC2Container container); boolean isStarted(AwsEC2Container awsEC2Container); Map<String, Set<String>> getNetworkInterfaceToSecurityGroupsMap(String instanceId); boolean verifySecurityGroupIdsOfNetworkInterfaceMap(String instanceId, Map<String, Set<String>> originalSecurityGroups);  }### Answer:
@Test public void createContainerFromInstance () { String instanceId = UUID.randomUUID().toString(); Instance instance = Mockito.spy(new Instance().withState(new InstanceState().withCode(AwsEC2Constants.AWS_TERMINATED_CODE) .withName(InstanceStateName.Terminated))); assertNull(awsEC2Platform.createContainerFromInstance(instance)); verify(containerManager, times(0)).getMatchingContainer(any(), any()); reset(awsEC2Platform, containerManager); instance = Mockito.spy(new Instance().withState(new InstanceState().withCode(0).withName(InstanceStateName.Running)) .withInstanceId(instanceId)); AwsEC2Container container = Mockito.spy(AwsEC2Container.builder().instanceId(instanceId).build()); doReturn(container).when(awsEC2Platform).buildContainerFromInstance(instance); doCallRealMethod().when(awsEC2Platform).createContainerFromInstance(instance); assertSame(container, awsEC2Platform.createContainerFromInstance(instance)); verify(containerManager, times(1)).offer(container); reset(awsEC2Platform, containerManager); doCallRealMethod().when(awsEC2Platform).createContainerFromInstance(instance); assertSame(container, awsEC2Platform.createContainerFromInstance(instance)); verify(awsEC2Platform, times(0)).buildContainerFromInstance(instance); verify(containerManager, times(0)).offer(container); }