getsadzeg
getsadzeg

Reputation: 660

Class has no public constructor TestCase(String name) or TestCase() while running my Cucumber scenario

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'

Upvotes: 3

Views: 1629

Answers (2)

getsadzeg
getsadzeg

Reputation: 660

Solved problem by just fixing the structure.

code details in this commit

Upvotes: 1

Mauricio Togneri
Mauricio Togneri

Reputation: 143

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.

Upvotes: 1

Related Questions