3

I'm using Green Coffee library to run Cucumber scenarios in my instrumentation tests. I followed example provided by repo step-by-step, but here's the error:

junit.framework.AssertionFailedError: Class pi.survey.features.MembersFeatureTest has no public constructor TestCase(String name) or TestCase()

And when I try to add default constructor to the class like provided here, it says

no default constructor available in 'com.mauriciotogneri.greencoffee.GreenCoffeeTest'

Here's my test's source code:

package pi.survey.features;

import android.support.test.rule.ActivityTestRule;

import com.mauriciotogneri.greencoffee.GreenCoffeeConfig;
import com.mauriciotogneri.greencoffee.GreenCoffeeTest;
import com.mauriciotogneri.greencoffee.Scenario;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;

import pi.survey.MainActivity;
import pi.survey.steps.memberSteps;

@RunWith(Parameterized.class)
public class MembersFeatureTest extends GreenCoffeeTest {
    @Rule
    public ActivityTestRule<MainActivity> activity = new ActivityTestRule<>(MainActivity.class);

    public MembersFeatureTest(Scenario scenario) {
        super(scenario);
    }



    @Parameterized.Parameters
    public static Iterable<Scenario> scenarios() throws IOException {
        return new GreenCoffeeConfig()
                .withFeatureFromAssets("assets/members.feature")
                .scenarios();
    }

    @Test
    public void test() {
        start(new memberSteps());
    }

}

And my members.feature source:

Feature: Inserting info to server



  Scenario: Invalid members
          When I introduce an invalid members
          And  I press the login button
          Then I see an error message saying 'Invalid members'
Community
  • 1
  • 1
getsadzeg
  • 640
  • 3
  • 9
  • 19
  • Interesting; you are correct; the GreenCoffeeTest has only constructors taking scenarios; so it seems impossible to extend to a class that has a default constructor or a ctor taking a string. – GhostCat Sep 16 '16 at 06:54
  • Yes and I created an [issue](https://github.com/mauriciotogneri/green-coffee/issues/1). So GreenCoffeeTest class must have default empty constructor or something? – getsadzeg Sep 16 '16 at 09:03
  • No idea. To me it seems that those two thingies (GreenCoffee) and cocumber simply can't play with each other as of now. – GhostCat Sep 16 '16 at 09:42

2 Answers2

1

Solved problem by just fixing the structure.

code details in this commit

getsadzeg
  • 640
  • 3
  • 9
  • 19
1

Regarding the questions about the constructors. Due to the fact that tests in GreenCoffee require:

@RunWith(Parameterized.class)

The static method annotated with @Parameters must return a list of something (but not necessarily Scenario). The examples in the documentation simply return a list of scenarios, that's why the constructor must take a single Scenario as a parameter.

However, you can create a class that encapsulates the scenario and other objects that you may need to pass to the constructor. For example, given the following class:

public class TestParameters
{
    public final String name;
    public final Scenario scenario;

    public TestParameters(String name, Scenario scenario)
    {
        this.name = name;
        this.scenario = scenario;
    }
}

You can write:

public TestConstructor(TestParameters testParameters)
{
    super(testParameters.scenario);
}

@Parameters
public static Iterable<TestParameters> parameters() throws IOException
{
    List<TestParameters> testParametersList = new ArrayList<>();

    List<Scenario> scenarios = new GreenCoffeeConfig()
            .withFeatureFromAssets("...")
            .scenarios();

    for (Scenario scenario : scenarios)
    {
        testParametersList.add(new TestParameters(scenario.name(), scenario));
    }

    return testParametersList;
}

In this way you can receive multiple values (encapsulated in an object) in the test constructor.

  • Yes I got this constructor thing,thanks. But I fixed the error with answer above, just fixing the structure. – getsadzeg Sep 22 '16 at 04:23