How to use setParameterNames method of com.tngtech.jgiven.impl.ScenarioModelBuilder class

Best JGiven code snippet using com.tngtech.jgiven.impl.ScenarioModelBuilder.setParameterNames

Source:ScenarioModelBuilder.java Github

copy

Full Screen

...82 public void scenarioStarted(Class<?> testClass, Method method, List<NamedArgument> namedArguments) {83 readConfiguration(testClass);84 readAnnotations(testClass, method);85 scenarioModel.setClassName(testClass.getName());86 setParameterNames(getNames(namedArguments));87 // must come at last88 setMethodName(method.getName());89 ParameterFormattingUtil parameterFormattingUtil = new ParameterFormattingUtil(configuration);90 List<ObjectFormatter<?>> formatter =91 parameterFormattingUtil.getFormatter(method.getParameterTypes(), getNames(namedArguments),92 method.getParameterAnnotations());93 setArguments(parameterFormattingUtil.toStringList(formatter, getValues(namedArguments)));94 setCaseDescription(testClass, method, namedArguments);95 }96 private void addStepMethod(Method paramMethod, List<NamedArgument> arguments, InvocationMode mode,97 boolean hasNestedSteps) {98 StepModel stepModel = createStepModel(paramMethod, arguments, mode);99 if (parentSteps.empty()) {100 getCurrentScenarioCase().addStep(stepModel);101 } else {102 parentSteps.peek().addNestedStep(stepModel);103 }104 if (hasNestedSteps) {105 parentSteps.push(stepModel);106 discrepancyOnLayer.push(0);107 }108 currentStep = stepModel;109 }110 StepModel createStepModel(Method paramMethod, List<NamedArgument> arguments, InvocationMode mode) {111 StepModel stepModel = new StepModel();112 stepModel.setName(getDescription(paramMethod));113 ExtendedDescription extendedDescriptionAnnotation = paramMethod.getAnnotation(ExtendedDescription.class);114 if (extendedDescriptionAnnotation != null) {115 stepModel.setExtendedDescription(extendedDescriptionAnnotation.value());116 }117 List<NamedArgument> nonHiddenArguments =118 filterHiddenArguments(arguments, paramMethod.getParameterAnnotations());119 ParameterFormattingUtil parameterFormattingUtil = new ParameterFormattingUtil(configuration);120 List<ObjectFormatter<?>> formatters =121 parameterFormattingUtil.getFormatter(paramMethod.getParameterTypes(), getNames(arguments),122 paramMethod.getParameterAnnotations());123 new StepFormatter(stepModel.getName(), nonHiddenArguments, formatters).buildFormattedWords()124 .forEach(sentenceBuilder::addWord);125 stepModel.setWords(sentenceBuilder.getWords());126 sentenceBuilder.clear();127 stepModel.setStatus(mode.toStepStatus());128 return stepModel;129 }130 private List<NamedArgument> filterHiddenArguments(List<NamedArgument> arguments,131 Annotation[][] parameterAnnotations) {132 List<NamedArgument> result = Lists.newArrayList();133 for (int i = 0; i < parameterAnnotations.length; i++) {134 if (!AnnotationUtil.isHidden(parameterAnnotations[i])) {135 result.add(arguments.get(i));136 }137 }138 return result;139 }140 @Override141 public void introWordAdded(String value) {142 sentenceBuilder.addIntroWord(value);143 }144 private void addToSentence(String value, boolean joinToPreviousWord, boolean joinToNextWord) {145 if (!sentenceBuilder.hasWords() && currentStep != null && joinToPreviousWord) {146 currentStep.getLastWord().addSuffix(value);147 } else {148 sentenceBuilder.addWord(value, joinToPreviousWord, joinToNextWord);149 }150 }151 private void addStepComment(List<NamedArgument> arguments) {152 if (arguments == null || arguments.size() != 1) {153 throw new JGivenWrongUsageException("A step comment method must have exactly one parameter.");154 }155 if (!(arguments.get(0).getValue() instanceof String)) {156 throw new JGivenWrongUsageException("The step comment method parameter must be a string.");157 }158 if (currentStep == null) {159 throw new JGivenWrongUsageException("A step comment must be added after the corresponding step, "160 + "but no step has been executed yet.");161 }162 stepCommentUpdated((String) arguments.get(0).getValue());163 }164 @Override165 public void stepCommentUpdated(String comment) {166 currentStep.setComment(comment);167 }168 private ScenarioCaseModel getCurrentScenarioCase() {169 if (scenarioCaseModel == null) {170 scenarioStarted("A Scenario");171 }172 return scenarioCaseModel;173 }174 private void incrementDiscrepancy() {175 int discrepancyOnCurrentLayer = discrepancyOnLayer.pop();176 discrepancyOnCurrentLayer++;177 discrepancyOnLayer.push(discrepancyOnCurrentLayer);178 }179 private void decrementDiscrepancy() {180 if (discrepancyOnLayer.peek() > 0) {181 int discrepancyOnCurrentLayer = discrepancyOnLayer.pop();182 discrepancyOnCurrentLayer--;183 discrepancyOnLayer.push(discrepancyOnCurrentLayer);184 }185 }186 @Override187 public void stepMethodInvoked(Method method, List<NamedArgument> arguments, InvocationMode mode,188 boolean hasNestedSteps) {189 if (method.isAnnotationPresent(IntroWord.class)) {190 introWordAdded(getDescription(method));191 incrementDiscrepancy();192 } else if (method.isAnnotationPresent(FillerWord.class)) {193 FillerWord fillerWord = method.getAnnotation(FillerWord.class);194 addToSentence(getDescription(method), fillerWord.joinToPreviousWord(), fillerWord.joinToNextWord());195 incrementDiscrepancy();196 } else if (method.isAnnotationPresent(StepComment.class)) {197 addStepComment(arguments);198 incrementDiscrepancy();199 } else {200 addTags(method.getAnnotations());201 addTags(method.getDeclaringClass().getAnnotations());202 addStepMethod(method, arguments, mode, hasNestedSteps);203 }204 }205 public void setMethodName(String methodName) {206 scenarioModel.setTestMethodName(methodName);207 }208 public void setArguments(List<String> arguments) {209 scenarioCaseModel.setExplicitArguments(arguments);210 }211 public void setParameterNames(List<String> parameterNames) {212 scenarioModel.setExplicitParameters(removeUnderlines(parameterNames));213 }214 private static List<String> removeUnderlines(List<String> parameterNames) {215 List<String> result = Lists.newArrayListWithCapacity(parameterNames.size());216 for (String paramName : parameterNames) {217 result.add(WordUtil.fromSnakeCase(paramName));218 }219 return result;220 }221 private String getDescription(Method paramMethod) {222 if (paramMethod.isAnnotationPresent(Hidden.class)) {223 return "";224 }225 Description description = paramMethod.getAnnotation(Description.class);...

Full Screen

Full Screen

setParameterNames

Using AI Code Generation

copy

Full Screen

1 public void testSetParameterNames() throws Exception {2 ScenarioModelBuilder scenarioModelBuilder = new ScenarioModelBuilder();3 scenarioModelBuilder.setParameterNames(Arrays.asList("name", "age"));4 ScenarioModel scenarioModel = scenarioModelBuilder.build();5 assertThat(scenarioModel.getParameters()).hasSize(2)6 .containsExactly("name", "age");7 }8}9public class ScenarioModelBuilder {10 private final ScenarioModel scenarioModel;11 public ScenarioModelBuilder() {12 scenarioModel = new ScenarioModel();13 }14 public ScenarioModelBuilder setParameterNames( List<String> parameters ) {15 scenarioModel.getParameters().clear();16 scenarioModel.getParameters().addAll( parameters );17 return this;18 }19 public ScenarioModel build() {20 return scenarioModel;21 }22}

Full Screen

Full Screen

setParameterNames

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.annotation.ScenarioStage2import com.tngtech.jgiven.junit.ScenarioTest3import com.tngtech.jgiven.tags.FeatureParameterNames4import org.junit.Test5class ParameterNamesTest extends ScenarioTest<GivenParameterNames, WhenParameterNames, ThenParameterNames> {6 void testParameterNames() {7 given().some_parameter_names_are_available_at_runtime()8 when().a_scenario_is_executed()9 then().the_parameter_names_are_set()10 }11}12class GivenParameterNames extends Stage<GivenParameterNames> {13 def some_parameter_names_are_available_at_runtime() {14 setParameterNames("a", "b", "c")15 }16}17class WhenParameterNames extends Stage<WhenParameterNames> {18 def a_scenario_is_executed() {19 }20}21class ThenParameterNames extends Stage<ThenParameterNames> {22 def the_parameter_names_are_set() {23 }24}

Full Screen

Full Screen

setParameterNames

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.Stage2import com.tngtech.jgiven.annotation.As3import com.tngtech.jgiven.annotation.Quoted4import com.tngtech.jgiven.annotation.ScenarioState5import com.tngtech.jgiven.impl.ScenarioModelBuilder6import com.tngtech.jgiven.report.model.ReportModel7class GivenSomeState extends Stage<GivenSomeState> {8 def given_some_state() {9 ScenarioModelBuilder builder = new ScenarioModelBuilder()10 builder.setParameterNames(['param1', 'param2'])11 reportModel = builder.build()12 }13}14class WhenSomeAction extends Stage<WhenSomeAction> {15 def when_some_action() {16 }17}18class ThenSomeOutcome extends Stage<ThenSomeOutcome> {19 def then_the_scenario_has_the_correct_parameter_names() {20 }21}22@As("A scenario with parameter names set at runtime")23class ScenarioWithParameterNamesSetAtRuntime extends JGivenTest<GivenSomeState, WhenSomeAction, ThenSomeOutcome> {24 def "the scenario has the correct parameter names"() {25 given().some_state()26 when().some_action()27 then().the_scenario_has_the_correct_parameter_names()28 }29}30JGivenTestRunner.runTest(ScenarioWithParameterNamesSetAtRuntime)31import com.tngtech.jgiven.Stage32import com.tngtech.jgiven.annotation.As33import com.tngtech.jgiven.annotation.Qu

Full Screen

Full Screen

setParameterNames

Using AI Code Generation

copy

Full Screen

1public class Test extends JGivenStage<Test> {2 int a;3 int b;4 int c;5 public Test the_numbers_$_and_$_and_$_are_provided(int a, int b, int c) {6 this.a = a;7 this.b = b;8 this.c = c;9 return self();10 }11 public Test the_sum_of_the_numbers_is(int sum) {12 assertThat(a + b + c).isEqualTo(sum);13 return self();14 }15 public Test createNewStage() {16 return new Test();17 }18}19public class TestTest extends JGivenTest<Test> {20 public void test() {21 given().the_numbers_$_and_$_and_$_are_provided(1, 2, 3);22 when().the_sum_of_the_numbers_is(6);23 then().nothing_happens();24 }25}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful