Best JGiven code snippet using com.tngtech.jgiven.impl.ScenarioModelBuilder
Source:ScenarioModelBuilderTest.java
...27import java.util.List;28import org.junit.Test;29import org.junit.runner.RunWith;30@RunWith(DataProviderRunner.class)31public class ScenarioModelBuilderTest extends ScenarioTestBaseForTesting<GivenTestStep, WhenTestStep, ThenTestStep> {32 public ScenarioModelBuilder getScenarioModelBuilder() {33 ScenarioModelBuilder scenarioModelBuilder = new ScenarioModelBuilder();34 scenarioModelBuilder.setReportModel(new ReportModel());35 return scenarioModelBuilder;36 }37 public void startScenario(String title) {38 getScenario().setModel(new ReportModel());39 getScenario().startScenario(title);40 }41 @Test42 public void testAddTagDynamically() {43 ReportModel reportModel = new ReportModel();44 ScenarioModelBuilder scenarioModelBuilder = getScenarioModelBuilder();45 scenarioModelBuilder.setReportModel(reportModel);46 scenarioModelBuilder.scenarioStarted("Test");47 scenarioModelBuilder.tagAdded(DynamicTag.class, "A", "B");48 scenarioModelBuilder.tagAdded(DynamicTag.class);49 assertThat(reportModel.getTagMap()).hasSize(3);50 Iterator<Tag> iterator = reportModel.getTagMap().values().iterator();51 assertThat(iterator.next().getValues()).containsExactly("A");52 assertThat(iterator.next().getValues()).containsExactly("B");53 assertThat(iterator.next().getValues()).containsExactly("default");54 }55 @DataProvider56 public static Object[][] testData() {57 return new Object[][] {58 {5, 6, 30},59 {2, 2, 4},60 {-5, 1, -5},61 };62 }63 @Test64 @UseDataProvider("testData")65 public void test(int a, int b, int expectedResult) throws Throwable {66 String description = "values can be multiplied";67 startScenario(description);68 given().$d_and_$d(a, b);69 when().both_values_are_multiplied_with_each_other();70 then().the_result_is(expectedResult);71 getScenario().finished();72 ScenarioModel model = getScenario().getScenarioModel();73 assertThat(model.getDescription()).isEqualTo(description);74 ScenarioCaseModel case0 = model.getCase(0);75 assertThat(case0.getExecutionStatus()).isEqualTo(ExecutionStatus.SUCCESS);76 assertThat(case0.getCaseNr()).isEqualTo(1);77 assertThat(case0.getExplicitArguments()).isEmpty();78 assertThat(case0.getSteps()).hasSize(3);79 assertThat(case0.getSteps()).extracting("failed").isEqualTo(asList(false, false, false));80 assertThat(case0.getSteps()).extracting("pending").isEqualTo(asList(false, false, false));81 assertThat(case0.getSteps()).extracting("skipped").isEqualTo(asList(false, false, false));82 StepModel step0 = case0.getSteps().get(0);83 assertThat(step0.getWords()).hasSize(4);84 assertThat(step0.getCompleteSentence()).isEqualTo("Given " + a + " and " + b);85 assertThat(extractIsArg(step0.getWords())).isEqualTo(Arrays.asList(false, true, false, true));86 StepModel step2 = case0.getSteps().get(2);87 assertThat(step2.getWords()).hasSize(3);88 assertThat(extractIsArg(step2.getWords())).isEqualTo(Arrays.asList(false, false, true));89 }90 public static List<Boolean> extractIsArg(List<Word> words) {91 ArrayList<Boolean> result = Lists.newArrayList();92 for (Word word : words) {93 result.add(word.isArg());94 }95 return result;96 }97 @DataProvider98 public static Object[][] argumentTestData() {99 return new Object[][] {100 {null, "null"},101 {"Foo", "Foo"},102 {123, "123"},103 {true, "true"},104 {new String[] {"a"}, "a"},105 {new String[] {}, ""},106 {new String[][] {{"a", "b"}, {"c"}}, "a, b, c"},107 };108 }109 @Test110 @UseDataProvider("argumentTestData")111 public void testArrayArguments(Object argument, String expected) throws Throwable {112 startScenario("test");113 given().an_array(argument);114 getScenario().finished();115 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();116 assertThat(step.getWords().get(2).getValue()).isEqualTo(expected);117 }118 @Test119 public void characters_are_not_dropped_when_using_the_As_annotation() throws Throwable {120 startScenario("Scenario with a @As tag");121 given().a_step_with_a_bracket_after_a_dollar(42);122 getScenario().finished();123 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();124 assertThat(step.getCompleteSentence()).isEqualTo("Given a step with a bracket after a dollar 42 ]");125 }126 @Test127 public void As_on_overridden_methods_is_correctly_evaluated() throws Throwable {128 ExtendedGivenTestStep stage = getScenario().addStage(ExtendedGivenTestStep.class);129 startScenario("Scenario with a @As tag");130 stage.abstract_step();131 getScenario().finished();132 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();133 assertThat(step.getCompleteSentence()).isEqualTo("an overridden description");134 }135 @Test136 public void setName_is_working_correctly_on_CurrentStep() throws Throwable {137 GivenTestStep stage = getScenario().addStage(GivenTestStep.class);138 startScenario("Test Scenario");139 stage.given().a_step_that_sets_the_name();140 getScenario().finished();141 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();142 assertThat(step.getName()).isEqualTo("another name");143 assertThat(step.getCompleteSentence()).isEqualTo("given another name");144 }145 @Test146 public void setName_is_working_correctly_on_CurrentStep_with_steps_with_arguments() throws Throwable {147 GivenTestStep stage = getScenario().addStage(GivenTestStep.class);148 startScenario("Test Scenario");149 stage.given().a_step_that_sets_the_name_with_an_argument("argument");150 getScenario().finished();151 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();152 assertThat(step.getName()).isEqualTo("another name argument");153 assertThat(step.getCompleteSentence()).isEqualTo("given another name argument");154 }155 @Test156 public void setComment_is_working_correctly_on_CurrentStep() throws Throwable {157 GivenTestStep stage = getScenario().addStage(GivenTestStep.class);158 startScenario("Test Scenario");159 stage.given().a_step_that_sets_a_comment();160 getScenario().finished();161 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();162 assertThat(step.getComment()).isEqualTo("a comment");163 }164 @Test165 public void a_custom_AsProvider_can_be_used() throws Throwable {166 startScenario("Scenario with a @As tag");167 given().a_step_with_an_As_annotation_and_a_custom_provider();168 getScenario().finished();169 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();170 assertThat(step.getCompleteSentence())171 .isEqualTo("Given Custom AsProvider output: a_step_with_an_As_annotation_and_a_custom_provider");172 }173 @Test174 public void timings_are_evaluated_with_filler_words() throws Throwable {175 startScenario("nasiges");176 given().something().and().something_else();177 getScenario().finished();178 ScenarioModel scenarioModel = getScenario().getScenarioModel();179 }180 @Test181 public void camel_case_is_supported_in_steps() throws Throwable {182 startScenario("Scenario camel case steps");183 given().aStepInCamelCase();184 getScenario().finished();185 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();186 assertThat(step.getCompleteSentence()).isEqualTo("Given a step in camel case");187 }188 @Test189 public void camel_case_is_supported_in_steps_with_parameters() throws Throwable {190 startScenario("Scenario camel case steps with parameter");191 given().aStepInCamelCaseWithA$Parameter("dollar");192 getScenario().finished();193 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();194 assertThat(step.getCompleteSentence()).isEqualTo("Given a step in camel case with a dollar parameter");195 }196 @Test197 public void all_uppercase_steps_are_formatted_correctly() throws Throwable {198 startScenario("Scenario with all uppercase step");199 given().ALLUPPERCASE();200 getScenario().finished();201 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();202 assertThat(step.getCompleteSentence()).isEqualTo("Given ALLUPPERCASE");203 }204 @Test205 public void the_Description_annotation_on_intro_words_is_evaluated() throws Throwable {206 startScenario("Scenario with an @As annotation");207 given().an_intro_word_with_an_as_annotation().something();208 getScenario().finished();209 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();210 assertThat(step.getWords().get(0).getValue()).isEqualTo("another description");211 }212 @Test213 public void filler_words_are_prepended_to_stage_methods() throws Throwable {214 startScenario("Scenario with filler words");215 given().there().is().something();216 getScenario().finished();217 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();218 assertThat(step.getCompleteSentence()).isEqualTo("Given there is something");219 }220 @Test221 public void the_Description_annotation_on_filler_words_is_evaluated() throws Throwable {222 startScenario("Scenario with @As annotation on filler words");223 given().a().filler_word_with_an_as_annotation().and_with().something_else();224 getScenario().finished();225 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();226 assertThat(step.getCompleteSentence()).isEqualTo("Given a Filler Word and with something else");227 }228 @Test229 public void filler_words_can_be_joined_to_previous_words() throws Throwable {230 startScenario("Scenario with filler word joined to a previous word");231 given().there().is().something_filled().comma().another().something_filled().and_with().something_else();232 getScenario().finished();233 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();234 assertThat(step.getCompleteSentence())235 .isEqualTo("Given there is something filled, another something filled and with something else");236 }237 @Test238 public void filler_words_can_surround_words() throws Throwable {239 startScenario("Scenario with filler words surrounding a word");240 given().there().is().open_bracket().something().close_bracket();241 getScenario().finished();242 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();243 assertThat(step.getCompleteSentence())244 .isEqualTo("Given there is (something)");245 }246 @Test247 public void filler_words_can_be_used_at_the_end_of_a_sentence() throws Throwable {248 startScenario("Scenario with filler words at the end of sentences");249 given().something().colon().and().something_else().full_stop();250 getScenario().finished();251 StepModel firstStep = getScenario().getScenarioCaseModel().getFirstStep();252 StepModel secondStep = getScenario().getScenarioCaseModel().getStep(1);253 assertThat(firstStep.getCompleteSentence()).isEqualTo("Given something:");254 assertThat(secondStep.getCompleteSentence()).isEqualTo("and something else.");255 }256 @Test257 public void printf_annotation_uses_the_PrintfFormatter() throws Throwable {258 startScenario("printf_annotation_uses_the_PrintfFormatter");259 given().a_step_with_a_printf_annotation_$(5.2);260 getScenario().finished();261 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();262 assertThat(step.getWords().get(2).getFormattedValue()).isEqualTo(String.format("%.2f", 5.2));263 }264 @Test265 public void testTagEquals() {266 assertThat(new Tag("test", "1")).isEqualTo(new Tag("test", "1"));267 }268 abstract static class AbstractStage {269 public abstract void abstract_step();270 }271 static class ConcreteStage extends AbstractStage {272 @Override273 public void abstract_step() {274 }275 }276 @Test277 public void abstract_steps_should_appear_in_the_report_model() throws Throwable {278 ConcreteStage stage = addStage(ConcreteStage.class);279 startScenario("Test");280 stage.abstract_step();281 getScenario().finished();282 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();283 assertThat(step.getWords().get(0).getFormattedValue()).isEqualTo("abstract step");284 }285 @Test286 public void DoNotIntercept_methods_are_not_appearing_in_the_report() throws Throwable {287 DoNotInterceptClass stage = addStage(DoNotInterceptClass.class);288 startScenario("Test");289 stage.do_not_intercept();290 stage.normal_step();291 getScenario().finished();292 StepModel step = getScenario().getScenarioCaseModel().getFirstStep();293 assertThat(step.getWords().get(0).getFormattedValue()).isEqualTo("normal step");294 }295 static class DoNotInterceptClass {296 @DoNotIntercept297 public void do_not_intercept() {298 }299 public void normal_step() {300 }301 }302 @Test303 public void error_message_is_correctly_stored() throws Throwable {304 FailingTestStage stage = addStage(FailingTestStage.class);305 startScenario("Test");306 stage.a_failing_test();307 try {308 getScenario().finished();309 } catch (Exception ignore) {310 // exception is ignored on purpose311 }312 ScenarioCaseModel scenarioCaseModel = getScenario().getScenarioCaseModel();313 assertThat(scenarioCaseModel.getErrorMessage()).isEqualTo("java.lang.IllegalArgumentException: test error");314 assertThat(scenarioCaseModel.getStackTrace().get(0)).matches(315 "com.tngtech.jgiven.impl.ScenarioModelBuilderTest\\$FailingTestStage.a_failing_test\\(ScenarioModelBuilderTest.java:\\d+\\)");316 }317 static class FailingTestStage {318 FailingTestStage a_failing_test() {319 throw new IllegalArgumentException("test error");320 }321 }322 static class ExtendedGivenTestStep extends AbstractStage {323 @Override324 @As("an overridden description")325 public void abstract_step() {326 }327 }328 @IsTag(value = "default")329 @Retention(RetentionPolicy.RUNTIME)...
Source:MockScenarioModelBuilder.java
...14import com.tngtech.jgiven.config.TagConfiguration;15import com.tngtech.jgiven.exception.JGivenWrongUsageException;16import com.tngtech.jgiven.format.ObjectFormatter;17import com.tngtech.jgiven.impl.Config;18import com.tngtech.jgiven.impl.ScenarioModelBuilder;19import com.tngtech.jgiven.impl.util.AssertionUtil;20import com.tngtech.jgiven.impl.util.WordUtil;21import com.tngtech.jgiven.report.model.*;22import xyz.multicatch.mockgiven.core.annotations.as.AsProviderFactory;23import xyz.multicatch.mockgiven.core.annotations.caseas.CaseAsFactory;24import xyz.multicatch.mockgiven.core.annotations.caseas.CaseAsProviderFactory;25import xyz.multicatch.mockgiven.core.annotations.description.AnnotatedDescriptionFactory;26import xyz.multicatch.mockgiven.core.annotations.description.DescriptionData;27import xyz.multicatch.mockgiven.core.annotations.description.DescriptionQueue;28import xyz.multicatch.mockgiven.core.annotations.description.InlineWithNext;29import xyz.multicatch.mockgiven.core.annotations.tag.AnnotationTagExtractor;30import xyz.multicatch.mockgiven.core.annotations.tag.AnnotationTagUtils;31import xyz.multicatch.mockgiven.core.resources.TextResourceProvider;32import xyz.multicatch.mockgiven.core.scenario.cases.CaseDescription;33import xyz.multicatch.mockgiven.core.scenario.cases.CaseDescriptionFactory;34import xyz.multicatch.mockgiven.core.scenario.cases.ExtendedScenarioCaseModel;35import xyz.multicatch.mockgiven.core.scenario.methods.DescriptionFactory;36import xyz.multicatch.mockgiven.core.scenario.methods.arguments.ArgumentUtils;37import xyz.multicatch.mockgiven.core.scenario.methods.arguments.ParameterFormatterFactory;38import xyz.multicatch.mockgiven.core.scenario.methods.arguments.ParameterFormatterUtils;39import xyz.multicatch.mockgiven.core.scenario.state.CurrentScenarioState;40import xyz.multicatch.mockgiven.core.scenario.steps.ExtendedStepModel;41import xyz.multicatch.mockgiven.core.scenario.steps.StepCommentFactory;42import xyz.multicatch.mockgiven.core.scenario.steps.StepModelFactory;43import xyz.multicatch.mockgiven.core.utils.ExceptionUtils;44public class MockScenarioModelBuilder extends ScenarioModelBuilder {45 private static final Set<String> STACK_TRACE_FILTER = ImmutableSet46 .of("sun.reflect", "com.tngtech.jgiven.impl.intercept", "com.tngtech.jgiven.impl.intercept", "$$EnhancerByCGLIB$$",47 "java.lang.reflect", "net.sf.cglib.proxy", "com.sun.proxy");48 private static final boolean FILTER_STACK_TRACE = Config.config()49 .filterStackTrace();50 private final Stack<ExtendedStepModel> parentSteps = new Stack<>();51 private final CurrentScenarioState currentScenarioState;52 private final StepCommentFactory stepCommentFactory;53 private final DescriptionFactory descriptionFactory;54 private final CaseDescriptionFactory caseDescriptionFactory;55 private final DescriptionQueue descriptionQueue;56 private AbstractJGivenConfiguration configuration;57 private StepModelFactory stepModelFactory;58 private AnnotationTagExtractor annotationTagExtractor;59 private ParameterFormatterFactory formatterFactory;60 private ExtendedScenarioModel scenarioModel;61 private ExtendedScenarioCaseModel scenarioCaseModel;62 private ExtendedStepModel currentStep;63 private Word introWord;64 private long scenarioStartedNanos;65 private ReportModel reportModel;66 public MockScenarioModelBuilder(CurrentScenarioState currentScenarioState, TextResourceProvider textResourceProvider) {67 this.currentScenarioState = currentScenarioState;68 this.stepCommentFactory = new StepCommentFactory();69 this.descriptionFactory = new DescriptionFactory(new AsProviderFactory(), new AnnotatedDescriptionFactory(), textResourceProvider);70 this.caseDescriptionFactory = new CaseDescriptionFactory(new CaseAsFactory(), new CaseAsProviderFactory());71 this.descriptionQueue = new DescriptionQueue();72 this.configuration = new DefaultConfiguration();73 initializeDependentOnConfiguration();74 }75 public MockScenarioModelBuilder(76 CurrentScenarioState currentScenarioState,77 StepCommentFactory stepCommentFactory,78 DescriptionFactory descriptionFactory,79 CaseDescriptionFactory caseDescriptionFactory,80 DescriptionQueue descriptionQueue81 ) {82 this.currentScenarioState = currentScenarioState;83 this.stepCommentFactory = stepCommentFactory;84 this.descriptionFactory = descriptionFactory;85 this.caseDescriptionFactory = caseDescriptionFactory;86 this.descriptionQueue = descriptionQueue;87 this.configuration = new DefaultConfiguration();88 initializeDependentOnConfiguration();89 }...
Source:MockScenarioBase.java
1package xyz.multicatch.mockgiven.core.scenario;2import java.lang.reflect.Method;3import java.util.List;4import com.tngtech.jgiven.impl.ScenarioBase;5import com.tngtech.jgiven.impl.ScenarioModelBuilder;6import com.tngtech.jgiven.report.model.NamedArgument;7import com.tngtech.jgiven.report.model.ReportModel;8import com.tngtech.jgiven.report.model.ScenarioCaseModel;9import com.tngtech.jgiven.report.model.ScenarioModel;10import xyz.multicatch.mockgiven.core.resources.TextResourceProvider;11import xyz.multicatch.mockgiven.core.scenario.creator.ByteBuddyStageClassCreator;12import xyz.multicatch.mockgiven.core.scenario.model.MockScenarioModelBuilder;13import xyz.multicatch.mockgiven.core.scenario.state.CurrentScenarioState;14public class MockScenarioBase extends ScenarioBase {15 protected final TextResourceProvider textResourceProvider;16 private final CurrentScenarioState currentScenarioState;17 private final ScenarioModelBuilder modelBuilder;18 private boolean initialized = false;19 public MockScenarioBase(TextResourceProvider textResourceProvider) {20 this.textResourceProvider = textResourceProvider;21 this.currentScenarioState = new CurrentScenarioState();22 this.modelBuilder = new MockScenarioModelBuilder(currentScenarioState, textResourceProvider);23 initScenarioExecutor();24 }25 public MockScenarioBase(TextResourceProvider textResourceProvider, CurrentScenarioState currentScenarioState, ScenarioModelBuilder scenarioModelBuilder) {26 this.textResourceProvider = textResourceProvider;27 this.currentScenarioState = currentScenarioState;28 this.modelBuilder = scenarioModelBuilder;29 initScenarioExecutor();30 }31 private void initScenarioExecutor() {32 MockScenarioExecutor scenarioExecutor = new MockScenarioExecutor();33 scenarioExecutor.setStageClassCreator(new ByteBuddyStageClassCreator());34 setExecutor(scenarioExecutor);35 }36 public void setModel(ReportModel reportModel) {37 assertNotInitialized();38 modelBuilder.setReportModel(reportModel);39 }...
ScenarioModelBuilder
Using AI Code Generation
1import com.tngtech.jgiven.impl.ScenarioModelBuilder;2import com.tngtech.jgiven.impl.ScenarioModel;3import com.tngtech.jgiven.impl.ScenarioModelWriter;4import com.tngtech.jgiven.impl.util.WordUtil;5public class ScenarioModelBuilderExample {6 public static void main(String[] args) {7 ScenarioModelBuilder scenarioModelBuilder = new ScenarioModelBuilder();8 .given().a_value_of( 42 )9 .when().the_value_is_incremented()10 .then().the_value_is( 43 )11 .build();12 System.out.println( new ScenarioModelWriter().writeToString( scenarioModel ) );13 }14}
ScenarioModelBuilder
Using AI Code Generation
1public class ScenarioModelBuilderTest {2 public static void main(String[] args) {3 ScenarioModelBuilder builder = new ScenarioModelBuilder();4 builder.startScenario("Test Scenario");5 builder.startGivenStage();6 builder.startStep();7 builder.addWord("Given");8 builder.addWord("I");9 builder.addWord("have");10 builder.addWord("a");11 builder.addWord("test");12 builder.addWord("scenario");13 builder.endStep();14 builder.endGivenStage();15 builder.startWhenStage();16 builder.startStep();17 builder.addWord("When");18 builder.addWord("I");19 builder.addWord("run");20 builder.addWord("this");21 builder.addWord("test");22 builder.endStep();23 builder.endWhenStage();24 builder.startThenStage();25 builder.startStep();26 builder.addWord("Then");27 builder.addWord("I");28 builder.addWord("should");29 builder.addWord("see");30 builder.addWord("the");31 builder.addWord("test");32 builder.addWord("scenario");33 builder.addWord("pass");34 builder.endStep();35 builder.endThenStage();36 builder.endScenario();37 ScenarioModel scenarioModel = builder.build();38 System.out.println(scenarioModel.toHtml());39 }40}
ScenarioModelBuilder
Using AI Code Generation
1public class ScenarioModelBuilderTest {2 public void testScenarioModelBuilder() {3 ScenarioModelBuilder scenarioModelBuilder = new ScenarioModelBuilder();4 scenarioModelBuilder.addGivenStep("Given I have $1 in my account");5 scenarioModelBuilder.addWhenStep("When I request $1");6 scenarioModelBuilder.addThenStep("Then I should have $1 less in my account");7 scenarioModelBuilder.addStep("And I should have $1 more in my account");8 ScenarioModel scenarioModel = scenarioModelBuilder.build();9 System.out.println(scenarioModel);10 }11}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!