### Question:
ReplayingJavascriptGenerator implements JavascriptGenerator<StepsAndConfiguration> { @Override public String generate(StepsAndConfiguration generationInformation) { long started = System.currentTimeMillis(); String generated = generateReplayingCode(generationInformation); long took = System.currentTimeMillis() - started; logger.info("Generation took {}ms", took); return generated; } ReplayingJavascriptGenerator(String baseTemplate,
                                        JavascriptGenerator<StepAndActionConfiguration> generatorByStepAndActionConfiguration,
                                        StepAndActionConfigurationSupplier stepAndActionConfigurationSupplier); @Override String generate(StepsAndConfiguration generationInformation);  }### Answer:
@Test public void canGenerateIfSuitableActionIsPresent() { final String generatedCodeForAction = "generaated"; ApplicationActionConfiguration applicationActionConfiguration = mock(ApplicationActionConfiguration.class); when(applicationActionConfiguration.getName()).thenReturn(SOME_EVENT); List<ApplicationActionConfiguration> applicationActionConfigurations = new ArrayList<>(); applicationActionConfigurations.add(applicationActionConfiguration); ApplicationConfiguration applicationConfiguration = mock(ApplicationConfiguration.class); when(applicationConfiguration.getApplicationActionConfigurationList()).thenReturn(applicationActionConfigurations); Map<String, String> testCaseStep = new HashMap<>(); testCaseStep.put(EVENT, SOME_EVENT); TestScenarioSteps testCaseSteps = new TestScenarioSteps(); testCaseSteps.add(testCaseStep); StepsAndConfiguration stepsAndConfiguration = mock(StepsAndConfiguration.class); when(stepsAndConfiguration.getTestCaseSteps()).thenReturn(testCaseSteps); when(stepsAndConfiguration.getApplicationConfiguration()).thenReturn(applicationConfiguration); StepAndActionConfiguration stepAndActionConfiguration = mock(StepAndActionConfiguration.class); JavascriptGenerator<StepAndActionConfiguration> generatorByStepAndActionConfiguration = mock(JavascriptGenerator.class); when(generatorByStepAndActionConfiguration.generate(stepAndActionConfiguration)).thenReturn(generatedCodeForAction); StepAndActionConfigurationSupplier stepAndActionConfigurationSupplier = mock(StepAndActionConfigurationSupplier.class); when(stepAndActionConfigurationSupplier.get(eq(testCaseStep), anyInt(), eq(applicationActionConfiguration))).thenReturn(stepAndActionConfiguration); ReplayingJavascriptGenerator replayingJavascriptGenerator = new ReplayingJavascriptGenerator(baseTemplate, generatorByStepAndActionConfiguration, stepAndActionConfigurationSupplier); String actual = replayingJavascriptGenerator.generate(stepsAndConfiguration); String expected = baseTemplate + System.lineSeparator() + System.lineSeparator() + generatedCodeForAction + System.lineSeparator(); assertEquals(expected, actual); }
@Test public void throwsExceptionIfSuitableActionIsNotFound() { final String generatedCodeForAction = "generaated"; ApplicationActionConfiguration applicationActionConfiguration = mock(ApplicationActionConfiguration.class); when(applicationActionConfiguration.getName()).thenReturn(SOME_EVENT); List<ApplicationActionConfiguration> applicationActionConfigurations = new ArrayList<>(); applicationActionConfigurations.add(applicationActionConfiguration); ApplicationConfiguration applicationConfiguration = mock(ApplicationConfiguration.class); when(applicationConfiguration.getApplicationActionConfigurationList()).thenReturn(applicationActionConfigurations); Map<String, String> testCaseStep = new HashMap<>(); testCaseStep.put(EVENT, "blabla"); TestScenarioSteps testCaseSteps = new TestScenarioSteps(); testCaseSteps.add(testCaseStep); StepsAndConfiguration stepsAndConfiguration = mock(StepsAndConfiguration.class); when(stepsAndConfiguration.getTestCaseSteps()).thenReturn(testCaseSteps); when(stepsAndConfiguration.getApplicationConfiguration()).thenReturn(applicationConfiguration); StepAndActionConfiguration stepAndActionConfiguration = mock(StepAndActionConfiguration.class); JavascriptGenerator<StepAndActionConfiguration> generatorByStepAndActionConfiguration = mock(JavascriptGenerator.class); when(generatorByStepAndActionConfiguration.generate(stepAndActionConfiguration)).thenReturn(generatedCodeForAction); StepAndActionConfigurationSupplier stepAndActionConfigurationSupplier = mock(StepAndActionConfigurationSupplier.class); when(stepAndActionConfigurationSupplier.get(eq(testCaseStep), anyInt(), eq(applicationActionConfiguration))).thenReturn(stepAndActionConfiguration); ReplayingJavascriptGenerator replayingJavascriptGenerator = new ReplayingJavascriptGenerator(baseTemplate, generatorByStepAndActionConfiguration, stepAndActionConfigurationSupplier); expectedException.expect(IllegalStateException.class); String actual = replayingJavascriptGenerator.generate(stepsAndConfiguration); }