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

NPE on null parameter (fixes #823) #933

Merged
merged 3 commits into from
Feb 19, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this change

import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -457,7 +452,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