DockerSteps { @When("^build image \"([^\"]+)\" from file \"([^\"]+)\"$") public void buildImage(String imageTag, String fileName) { runner.when(docker().client(dockerClient) .buildImage() .tag(imageTag) .dockerFile(fileName) .validateCommandResult((result, context) -> context.setVariable(DockerMessageHeaders.IMAGE_ID, result.getImageId()))); }  @Before void before(); @Given("^docker-client \"([^\"]+)\"$") void setClient(String id); @When("^create container \"([^\"]+)\" from \"([^\"]+)\"$") void createContainer(String containerName, String imageTag); @When("^build image \"([^\"]+)\" from file \"([^\"]+)\"$") void buildImage(String imageTag, String fileName); @Then("^start container \"([^\"]+)\"$") void startContainer(String containerId); @Then("^stop container \"([^\"]+)\"$") void stopContainer(String containerId); @Then("^(?:the )?container \"([^\"]+)\" should be running$") void containerIsRunning(String containerId); @Then("^(?:the )?container \"([^\"]+)\" should be stopped") void containerIsStopped(String containerId);  }
@Test public void testBuildImage() { com.github.dockerjava.api.DockerClient dockerJavaClient = Mockito.mock(com.github.dockerjava.api.DockerClient.class); BuildImageCmd buildCmd = Mockito.mock(BuildImageCmd.class); BuildResponseItem response = Mockito.mock(BuildResponseItem.class); DockerEndpointConfiguration endpointConfiguration = new DockerEndpointConfiguration(); endpointConfiguration.setDockerClient(dockerJavaClient); when(dockerClient.getEndpointConfiguration()).thenReturn(endpointConfiguration); when(dockerJavaClient.buildImageCmd()).thenReturn(buildCmd); when(response.isBuildSuccessIndicated()).thenReturn(true); when(response.isErrorIndicated()).thenReturn(false); when(response.getImageId()).thenReturn(UUID.randomUUID().toString()); when(buildCmd.withTag("fooImage:latest")).thenReturn(buildCmd); when(buildCmd.withDockerfile(any(File.class))).thenReturn(buildCmd); when(buildCmd.exec(any(BuildImageResultCallback.class))).thenAnswer(invocation -> { ((BuildImageResultCallback) invocation.getArguments()[0]).onNext(response); ((BuildImageResultCallback) invocation.getArguments()[0]).close(); return invocation.getArguments()[0]; }); steps.setClient("dockerClient"); steps.buildImage("fooImage:latest", "classpath:docker/Dockerfile"); Assert.assertEquals(runner.getTestCase().getActionCount(), 1L); Assert.assertTrue(runner.getTestCase().getTestAction(0) instanceof DockerExecuteAction); DockerExecuteAction action = (DockerExecuteAction) runner.getTestCase().getTestAction(0); Assert.assertEquals(action.getDockerClient(), dockerClient); Assert.assertTrue(action.getCommand() instanceof ImageBuild); Assert.assertEquals(action.getCommand().getParameters().get("tag"), "fooImage:latest"); Assert.assertEquals(action.getCommand().getParameters().get("dockerfile"), "classpath:docker/Dockerfile"); Assert.assertEquals(context.getVariable(DockerMessageHeaders.IMAGE_ID), response.getImageId()); }