gpt4 book ai didi

android - 在运行我的 Cucumber 场景时,类没有公共(public)构造函数 TestCase(String name) 或 TestCase()

转载 作者:太空狗 更新时间:2023-10-29 13:11:59 26 4
gpt4 key购买 nike

我正在使用 Green Coffee library在我的仪器测试中运行 Cucumber 场景。我一步一步地按照 repo 提供的示例进行操作,但这是错误:

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

当我尝试将默认构造函数添加到类中时 provided here , 它说

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

这是我的测试源代码:

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());
}

}

还有我的members.feature来源:

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'

最佳答案

关于构造函数的问题。由于 GreenCoffee 中的测试需要:

@RunWith(Parameterized.class)

@Parameters注解的静态方法必须返回一些东西的列表(但不一定是Scenario)。文档中的示例仅返回场景列表,这就是构造函数必须将单个场景作为参数的原因。

但是,您可以创建一个类来封装您可能需要传递给构造函数的场景和其他对象。例如,给定以下类:

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

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

你可以这样写:

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;
}

通过这种方式,您可以在测试构造函数中接收多个值(封装在一个对象中)。

关于android - 在运行我的 Cucumber 场景时,类没有公共(public)构造函数 TestCase(String name) 或 TestCase(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39518848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com