deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rafael Pestano <rmpest...@gmail.com>
Subject Deltaspike Cucumber integration
Date Mon, 05 Oct 2015 20:06:28 GMT
Hi guys,

I recently made a small PoC in TestControl module to enable cucumber tests
using deltapike,  here's an example:


@RunWith(CdiTestRunner.class)
@CucumberOptions(
    features = "src/test/resources/features/uc014.feature"
)
public class CucumberCDITest
{


    @Test
    public void fakeIt() //needed by junit runner
    {

    }


}


uc014.feature

Feature: ApplicationScoped bean test
  As a user of DeltaSpike Test Control module
  I want to inject application scoped beans
  So that I can test application scoped beans


  Scenario Outline: bean increment
    Given test bean value is set to <value>
    When bean value is incremented by 2
    Then bean value should be <result>

  Examples: examples1
  | value | result |
  | 1     | 3      |
  | 0     | 2      |
  | 10    | 12     |


public class Uc014Steps
<https://github.com/rmpestano/deltaspike/blob/master/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc014/Uc014Steps.java>
{

  @Inject
  private ApplicationScopedTestBean testBean;

  @Inject
  private ApplicationScopedTestBeanClient testBeanClient;


  @Given("^test bean value is set to (\\d+)$")
  public void initBeanValue(int value) {
    this.testBean.setValue(value);
  }

  @When("^bean value is incremented by (\\d+)$")
  public void incrementBeanValue(int amount) {
    this.testBeanClient.increment(amount);//will increment bean value
  }

  @Then("^bean value should be (\\d+)$")
  public void checkBeanValue(int result) {
    Assert.assertEquals(result, this.testBean.getValue());
  }

  @After
  public void finalCheck() {
    int value =
BeanProvider.getContextualReference(ApplicationScopedTestBean.class).getValue();
    int nextValue =
BeanProvider.getContextualReference(ApplicationScopedTestBeanClient.class).getNextValue();

    if (value == 0)
    {
      throw new IllegalStateException("new application-scoped bean
instance was created");
    }

    if (nextValue == 1)
    {
      throw new IllegalStateException("new application-scoped bean
instance was created");
    }
  }


}

It is working here: https://github.com/rmpestano/deltaspike
<https://github.com/rmpestano/deltaspike> and the changes I've made are in this
commit
<https://github.com/rmpestano/deltaspike/commit/53a91c4a592afb084c42df6ff023bfc4f1b325f8>
.


I did not created another runner, for simplicity i'm running cucumber
inside CdiTestRunner, here's relevant bits:

@Override
public void run(RunNotifier runNotifier){

if (getTestClass().getJavaClass().isAnnotationPresent(CucumberOptions.class))
{

   testContext.runCucumber(runNotifier,getTestClass());

}

else

{
    super.run(runNotifier);
}

public void runCucumber(RunNotifier runNotifier, TestClass testClass)
throws IOException, InitializationError
{
    applyBeforeClassConfig(testClass.getJavaClass());
    new Cucumber(testClass.getJavaClass()).run(runNotifier);
    applyAfterClassConfig();
}


I also needed to create an object factory
<https://github.com/rmpestano/deltaspike/blob/master/deltaspike/modules/test-control/impl/src/main/java/cucumber/runtime/CucumberObjectFactory.java>
in order to make step definitions (like Uc014Steps) CDI beans. This factory
must be in package 'cucumber.runtime' otherwise Cucumber wont use it (I
think there is way to make cucumber load it from other packages but we need
to create a cucumber backend for it)




Do you think this could be incorporated in Test Control module?


If you have interest I think we need to create a separated runner and then
we can get ride of the

    @Test
    public void fakeIt() //needed by junit runner
    {

    }

without the fake test junit runner will complain cause there is no test
method.


OBS:
I needed to declare sure-fire plugin to use a newer version
I had to upgrade junit to 4.11 because cucumber-junit depends on it

-- 
<http://www.advancedit.com.br/>Att,

Rafael M. Pestano

Desenvolvedor Java Cia. de Processamento de Dados do Rio Grande do Sul
http://rpestano.wordpress.com/
@realpestano <https://twitter.com/realpestano>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message