PageRenderTime 586ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/org.jnario.tests/xtend-gen/org/jnario/spec/tests/integration/CustomizingTheSpecCreationSpec.java

http://github.com/bmwcarit/Jnario
Java | 94 lines | 57 code | 3 blank | 34 comment | 0 complexity | c14cc096841034e0e85e87365426ea4f MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (c) 2012 BMW Car IT and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. */
  8. package org.jnario.spec.tests.integration;
  9. import com.google.inject.Inject;
  10. import org.eclipse.xtend2.lib.StringConcatenation;
  11. import org.eclipse.xtext.xbase.lib.Extension;
  12. import org.jnario.jnario.test.util.BehaviorExecutor;
  13. import org.jnario.jnario.test.util.SpecTestCreator;
  14. import org.jnario.runner.CreateWith;
  15. import org.jnario.runner.ExampleGroupRunner;
  16. import org.jnario.runner.Named;
  17. import org.jnario.runner.Order;
  18. import org.junit.Test;
  19. import org.junit.runner.RunWith;
  20. /**
  21. * It is possible to customize the instantiation of features, specs and subjects.
  22. * This can be useful when a dependency injection container or a mocking
  23. * framework should be used for creating specs. To do so, create a custom `SpecCreator` and
  24. * annotate your spec with `CreateWith` and pass as a value the type of the custom `SpecCreator`.
  25. * Here is an example for a custom `SpecCreator` that uses Google Guice to create the specification:
  26. *
  27. * <pre class="prettyprint">
  28. * package org.jnario.lib;
  29. *
  30. * import com.google.inject.Guice;
  31. * import com.google.inject.util.Modules;
  32. *
  33. * public class GuiceSpecCreator extends AbstractSpecCreator {
  34. * public Object create(Class<?> klass) throws Exception {
  35. * return Guice.createInjector(Modules.EMPTY_MODULE).getInstance(klass);
  36. * }
  37. * }
  38. * </pre>
  39. *
  40. * [Here](https://gist.github.com/2869959) is another example that demonstrates
  41. * how to create a custom **SpecCreator** for [Mockito](http://code.google.com/p/mockito/).
  42. */
  43. @CreateWith(SpecTestCreator.class)
  44. @Named("Customizing the Spec Creation")
  45. @RunWith(ExampleGroupRunner.class)
  46. @SuppressWarnings("all")
  47. public class CustomizingTheSpecCreationSpec {
  48. @Inject
  49. @Extension
  50. @org.jnario.runner.Extension
  51. public BehaviorExecutor _behaviorExecutor;
  52. /**
  53. * This example uses the Google Guice to instantiate the specification.
  54. * @filter('''|.executesSuccessfully)
  55. */
  56. @Test
  57. @Named("Example")
  58. @Order(1)
  59. public void _example() throws Exception {
  60. StringConcatenation _builder = new StringConcatenation();
  61. _builder.append("package bootstrap");
  62. _builder.newLine();
  63. _builder.newLine();
  64. _builder.append("import org.jnario.runner.CreateWith");
  65. _builder.newLine();
  66. _builder.append("import org.jnario.jnario.test.util.GuiceSpecCreator");
  67. _builder.newLine();
  68. _builder.append("import com.google.inject.Inject");
  69. _builder.newLine();
  70. _builder.newLine();
  71. _builder.append("@CreateWith(typeof(GuiceSpecCreator))");
  72. _builder.newLine();
  73. _builder.append("describe \"Something\" {");
  74. _builder.newLine();
  75. _builder.append(" ");
  76. _builder.newLine();
  77. _builder.append(" ");
  78. _builder.append("@Inject String toInject");
  79. _builder.newLine();
  80. _builder.append(" ");
  81. _builder.newLine();
  82. _builder.append(" ");
  83. _builder.append("fact toInject should not be null");
  84. _builder.newLine();
  85. _builder.append(" ");
  86. _builder.newLine();
  87. _builder.append("}");
  88. _builder.newLine();
  89. this._behaviorExecutor.executesSuccessfully(_builder);
  90. }
  91. }