@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void runAndExpectNoException_withReturn(Execution execution) { concurrencyRule.add(() -> { invoked.set(true); return true; }); execution.execute(concurrencyRule); assertThat(invoked.get()).isTrue(); }
Object[] paramsFromNamedMethod(Class<?> sourceClass) { String namedMethodAnnotation = frameworkMethod.getAnnotation(Parameters.class).named(); if (namedMethodAnnotation.isEmpty()) { return invokeMethodWithParams(defaultMethodName(), sourceClass); } List<Object> result = new ArrayList<Object>(); for (String name : namedMethodAnnotation.split(",")) { for (Object param : invokeNamedMethodWithParams(name.trim(), sourceClass)) result.add(param); } return result.toArray(); }
Object[] paramsFromMethod(Class<?> sourceClass) { String methodAnnotation = frameworkMethod.getAnnotation(Parameters.class).method(); if (methodAnnotation.isEmpty()) { return invokeMethodWithParams(defaultMethodName(), sourceClass); } List<Object> result = new ArrayList<Object>(); for (String methodName : methodAnnotation.split(",")) { for (Object param : invokeMethodWithParams(methodName.trim(), sourceClass)) result.add(param); } return result.toArray(); }
@Test @Parameters(method = "classGenericDefinitionSuccess") public void testClassGenericDefinitionSuccess(Class<?> type) { testXKindGenericDefinitionSuccess(type); }
@Test @Parameters(method = "methodGenericDefinitionSuccess") public void testMethodGenericDefinitionSuccess(Class<?> type) { testXKindGenericDefinitionSuccess(type); }
@Test @Parameters public void testApply(DimensionSpec dimensionSpec, Map<String, String> map) { for (Map.Entry<String, String> entry : map.entrySet()) { Assert.assertEquals( NullHandling.emptyToNullIfNeeded(entry.getValue()), dimensionSpec.getExtractionFn().apply(entry.getKey()) ); } }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void runAndExpectValue_failsForWrongValue(Execution execution) { concurrencyRule.add(callWithRetVal).expectValue(Integer.valueOf(3)); assertThatThrownBy(() -> execution.execute(concurrencyRule)) .isInstanceOf(AssertionError.class); assertThat(invoked.get()).isTrue(); }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void repeatUntilValue_throwsIfValueIsNeverTrue(Execution execution) { boolean expectedVal = true; retVal.set(false); concurrencyRule.add(callWithEventuallyCorrectRetVal) .repeatUntilValue(expectedVal) .repeatForDuration(Duration.ofSeconds(2)); assertThatThrownBy(() -> execution.execute(concurrencyRule)) .isInstanceOf(ComparisonFailure.class); assertThat(invoked.get()).isTrue(); }
@Test @Parameters(method = "getCommands") public void whenLastReceivedIsSetThenCheckAndSetLastResultSentIfValidMustNotSetIt( BaseCommand baseCommand) { ServerToClientFunctionResultSender resultSender = mock(ServerToClientFunctionResultSender.class); when(resultSender.isLastResultReceived()).thenReturn(true); baseCommand.setLastResultReceived(resultSender); verify(resultSender, times(0)).setLastResultReceived(true); }
@Test @Parameters(method = "getCommands") public void whenLastReceivedIsNotSetThenCheckAndSetLastResultSentIfValidMustSetIt( BaseCommand baseCommand) { ServerToClientFunctionResultSender resultSender = mock(ServerToClientFunctionResultSender.class); when(resultSender.isLastResultReceived()).thenReturn(false); baseCommand.setLastResultReceived(resultSender); verify(resultSender, times(1)).setLastResultReceived(true); }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void runAndExpectException_throwableInstance_wrongClass_fails(Execution execution) { Callable<?> callable = () -> { throw new IllegalArgumentException("foo"); }; concurrencyRule.add(callable).expectException(new NullPointerException("foo")); assertThatThrownBy(() -> execution.execute(concurrencyRule)) .isInstanceOf(AssertionError.class); }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void runAndExpectNoException_withNoReturn(Execution execution) { concurrencyRule.add(ConcurrencyRule.toCallable(() -> invoked.set(true))); execution.execute(concurrencyRule); assertThat(invoked.get()).isTrue(); }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void repeatForIterations(Execution execution) { int expectedIterations = 4; this.iterations.set(0); concurrencyRule.add(callWithRetValAndRepeatCount).repeatForIterations(4); execution.execute(concurrencyRule); assertThat(this.iterations.get()).isEqualTo(expectedIterations); }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void repeatForIterationsAndExpectValueForEach_failsWithOneWrongValue(Execution execution) { int expectedIterations = 4; this.iterations.set(0); concurrencyRule.add(callWithRetValAndRepeatCountAndOneWrongValue).expectValue(expectedRetVal) .repeatForIterations(expectedIterations); assertThatThrownBy(() -> execution.execute(concurrencyRule)).isInstanceOf(AssertionError.class); assertThat(this.iterations.get()).isEqualTo(stopIteration); }
@Test @Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"}) public void runAndExpectException_throwableInstance_wrongMessage_fails(Execution execution) { Callable<?> callable = () -> { throw new NullPointerException("foo"); }; concurrencyRule.add(callable).expectException(new NullPointerException("bar")); assertThatThrownBy(() -> execution.execute(concurrencyRule)) .isInstanceOf(AssertionError.class); }