### Question:
InstallTargetCommand implements Action { @Override public Object execute() throws Exception { SupportedTarget st = SupportedTarget.which( this.targetName ); if( st == null ) { this.out.println( "Unknown target: " + this.targetName + ". Make sure it is correct or install it manually." ); } else if( this.roboconfVersion == null ) { this.out.println( "Error: the Roboconf version could not be determined." ); } else for( String cmd : st.findCommands( this.roboconfVersion )) { this.logger.fine( "Executing " + cmd + "..." ); this.out.println( "Executing " + cmd + "..." ); this.session.execute( cmd ); } return null; } InstallTargetCommand(); @Override Object execute();  }### Answer:
@Test public void testExecute_noTarget() throws Exception { InstallTargetCommand itc = new InstallTargetCommand(); ByteArrayOutputStream os = new ByteArrayOutputStream(); itc.out = new PrintStream( os, true, "UTF-8" ); itc.execute(); Assert.assertTrue( os.toString( "UTF-8" ).contains( "Unknown target" )); }
@Test public void testExecute_noRoboconfVersion() throws Exception { InstallTargetCommand itc = new InstallTargetCommand(); ByteArrayOutputStream os = new ByteArrayOutputStream(); itc.out = new PrintStream( os, true, "UTF-8" ); itc.targetName = "openstack"; itc.execute(); Assert.assertTrue( os.toString( "UTF-8" ).contains( "the Roboconf version" )); }
@Test public void testExecute_valid() throws Exception { InstallTargetCommand itc = new InstallTargetCommand(); ByteArrayOutputStream os = new ByteArrayOutputStream(); itc.out = new PrintStream( os, true, "UTF-8" ); itc.targetName = "docker"; itc.roboconfVersion = "0.5"; final String expected = "bundle:install --start mvn:net.roboconf/roboconf-target-docker/0.5"; Session session = Mockito.mock( Session.class ); Mockito.when( session.execute( expected )).thenReturn( null ); for( Class<?> c = itc.getClass(); c != null; c = c.getSuperclass()) { try { Field field = c.getDeclaredField( "session" ); field.setAccessible( true ); field.set( itc, session ); } catch( NoSuchFieldException e ) { } } itc.execute(); String s = os.toString( "UTF-8" ); Assert.assertFalse( s.contains( "the Roboconf version" )); Assert.assertFalse( s.contains( "Unknown target" )); Mockito.verify( session ).execute( expected ); }