Skip to content
This repository has been archived by the owner on Jan 14, 2019. It is now read-only.

Commit

Permalink
fix npe when parameter value is null (fixes #823, via #933)
Browse files Browse the repository at this point in the history
  • Loading branch information
sseliverstov authored and baev committed Feb 19, 2017
1 parent d565ae1 commit 88a99ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -457,7 +458,7 @@ private void fireAddParameterEvents(ITestResult iTestResult) {
}

String name = getNameForParameter(parameter.value(), methodParameterNames, i);
String value = parameters[i].toString();
String value = Objects.toString(parameters[i]);

getLifecycle().fire(new AddParameterEvent(name, value, ParameterKind.ARGUMENT));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void setUp() {

// mocking test method parameters
ConstructorOrMethod constructorOrMethod = mock(ConstructorOrMethod.class);
when(constructorOrMethod.getMethod()).thenReturn(parametrizedTestMethod(0, null, null));
when(constructorOrMethod.getMethod()).thenReturn(parametrizedTestMethod(0, null, null, null));
method = mock(ITestNGMethod.class);
when(method.getConstructorOrMethod()).thenReturn(constructorOrMethod);
testResult = mock(ITestResult.class);
Expand Down Expand Up @@ -130,13 +130,14 @@ public void parametrizedTest() {
double doubleParameter = 10.0;
String stringParameter = "string";
String anotherStringParameter = "anotherString";
String nullValueParameter = null;
when(testResult.getTestContext()).thenReturn(testContext);
when(testResult.getName()).thenReturn(DEFAULT_TEST_NAME);
when(testResult.getTestContext().getSuite().getName()).thenReturn(DEFAULT_SUITE_NAME);
when(testResult.getTestContext().getCurrentXmlTest().getName()).thenReturn(DEFAULT_XML_TEST_NAME);
when(testResult.getTestClass().getName()).thenReturn(DEFAULT_CLASS_NAME);
when(testResult.getMethod().getMethodName()).thenReturn(DEFAULT_TEST_NAME);
when(testResult.getParameters()).thenReturn(new Object[]{doubleParameter, stringParameter, anotherStringParameter});
when(testResult.getParameters()).thenReturn(new Object[]{doubleParameter, stringParameter, anotherStringParameter, nullValueParameter});

doReturn(new Annotation[0]).when(testngListener).getMethodAnnotations(testResult);

Expand All @@ -145,20 +146,21 @@ public void parametrizedTest() {
testngListener.onTestStart(testResult);

String suiteUid = testngListener.getSuiteUid(testContext);
String testName = String.format("%s[%s,%s,%s]",
DEFAULT_TEST_NAME, Double.toString(doubleParameter), stringParameter, anotherStringParameter);
String testName = String.format("%s[%s,%s,%s,%s]",
DEFAULT_TEST_NAME, Double.toString(doubleParameter), stringParameter, anotherStringParameter, nullValueParameter);
verify(allure).fire(eq(withExecutorInfo(new TestCaseStartedEvent(suiteUid, testName).withLabels(
AllureModelUtils.createTestSuiteLabel(DEFAULT_SUITE_NAME),
AllureModelUtils.createTestGroupLabel(DEFAULT_XML_TEST_NAME),
AllureModelUtils.createTestClassLabel(DEFAULT_CLASS_NAME),
AllureModelUtils.createTestMethodLabel(DEFAULT_TEST_NAME)))));

ArgumentCaptor<AddParameterEvent> captor = ArgumentCaptor.forClass(AddParameterEvent.class);
verify(allure, times(2)).fire(captor.capture());
verify(allure, times(3)).fire(captor.capture());

Iterator<AddParameterEvent> addParameterEvents = captor.getAllValues().iterator();
assertParameterEvent("doubleParameter", doubleParameter + "", addParameterEvents.next(), false);
assertParameterEvent("valueFromAnnotation", anotherStringParameter, addParameterEvents.next(), true);
assertParameterEvent("valueFromAnnotation", "null", addParameterEvents.next(), true);
assertFalse(addParameterEvents.hasNext());
}

Expand All @@ -175,9 +177,9 @@ private void assertParameterEvent(@SuppressWarnings("UnusedParameters") String e
}

@SuppressWarnings("UnusedParameters")
public Method parametrizedTestMethod(@Parameter double doubleParameter, String notParameter, @Parameter("valueFromAnnotation") String stringParameter) {
public Method parametrizedTestMethod(@Parameter double doubleParameter, String notParameter, @Parameter("valueFromAnnotation") String stringParameter, @Parameter("valueFromAnnotation") String nullStringParameter) {
try {
return getClass().getDeclaredMethod("parametrizedTestMethod", double.class, String.class, String.class);
return getClass().getDeclaredMethod("parametrizedTestMethod", double.class, String.class, String.class, String.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 88a99ee

Please sign in to comment.