### Question:
AttachmentManager implements Lifecycle { public void attachAdapterToVehicle(@Nonnull String vehicleName, @Nonnull VehicleCommAdapterFactory factory) { requireNonNull(vehicleName, "vehicleName"); requireNonNull(factory, "factory"); VehicleEntry vehicleEntry = vehicleEntryPool.getEntryFor(vehicleName); if (vehicleEntry == null) { LOG.warn("No vehicle entry found for '{}'. Entries: {}", vehicleName, vehicleEntryPool); return; } detachAdapterFromVehicle(vehicleName, true); VehicleCommAdapter commAdapter = factory.getAdapterFor(vehicleEntry.getVehicle()); if (commAdapter == null) { LOG.warn("Factory {} did not provide adapter for vehicle {}, ignoring.", factory, vehicleEntry.getVehicle().getName()); return; } commAdapter.initialize(); controllerPool.attachVehicleController(vehicleEntry.getVehicle().getName(), commAdapter); vehicleEntry.setCommAdapterFactory(factory); vehicleEntry.setCommAdapter(commAdapter); vehicleEntry.setProcessModel(commAdapter.getProcessModel()); objectService.updateObjectProperty(vehicleEntry.getVehicle().getReference(), Vehicle.PREFERRED_ADAPTER, factory.getClass().getName()); updateAttachmentInformation(vehicleEntry); } @Inject  AttachmentManager(@Nonnull TCSObjectService objectService,
                           @Nonnull LocalVehicleControllerPool controllerPool,
                           @Nonnull VehicleCommAdapterRegistry commAdapterRegistry,
                           @Nonnull VehicleEntryPool vehicleEntryPool,
                           @Nonnull @ApplicationEventBus EventHandler eventHandler,
                           @Nonnull KernelApplicationConfiguration configuration); @Override void initialize(); @Override boolean isInitialized(); @Override void terminate(); void attachAdapterToVehicle(@Nonnull String vehicleName,
                                     @Nonnull VehicleCommAdapterFactory factory); void detachAdapterFromVehicle(@Nonnull String vehicleName,
                                       boolean doDetachVehicleController); void autoAttachAdapterToVehicle(@Nonnull String vehicleName); void autoAttachAllAdapters(); AttachmentInformation getAttachmentInformation(String vehicleName); Map<String, AttachmentInformation> getAttachmentPool();  }### Answer:
@Test public void shouldNotAttachUnknownVehicle() { attachmentManager.attachAdapterToVehicle("UnknownVehicle", commAdapterFactory); verify(commAdapterFactory, times(0)).getAdapterFor(any(Vehicle.class)); verify(vehicleControllerPool, times(0)).attachVehicleController(any(String.class), any(VehicleCommAdapter.class)); }
@Test public void shouldAttachAdapterToVehicle() { VehicleCommAdapter commAdapter = new SimpleCommAdapter(VEHICLE_1); when(commAdapterFactory.getAdapterFor(VEHICLE_1)).thenReturn(commAdapter); when(commAdapterFactory.getAdapterDescription()).thenReturn(""); when(commAdapterFactory.getDescription()).thenCallRealMethod(); attachmentManager.attachAdapterToVehicle(VEHICLE_1_NAME, commAdapterFactory); verify(vehicleControllerPool, times(1)).detachVehicleController(VEHICLE_1_NAME); verify(vehicleControllerPool, times(1)).attachVehicleController(VEHICLE_1_NAME, commAdapter); assertNotNull(vehicleEntryPool.getEntryFor(VEHICLE_1_NAME)); assertThat(vehicleEntryPool.getEntryFor(VEHICLE_1_NAME).getCommAdapter(), is(commAdapter)); assertThat(vehicleEntryPool.getEntryFor(VEHICLE_1_NAME).getCommAdapterFactory(), is(commAdapterFactory)); assertThat(vehicleEntryPool.getEntryFor(VEHICLE_1_NAME).getProcessModel(), is(commAdapter.getProcessModel())); }