2016-09-15 14 views
3

我使用Green Coffee library在我的仪器测试中运行Cucumber场景。我跟着例如通过回购一步一步提供的,但这里的错误:类运行我的黄瓜场景时没有公共构造函数TestCase(String name)或TestCase()

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' 
+0

有趣;你是对的; GreenCoffeeTest只有构造函数需要场景;所以似乎不可能扩展到具有默认构造函数或ctor取得字符串的类。 – GhostCat

+0

是的,我创建了一个[问题](https://github.com/mauriciotogneri/green-coffee/issues/1)。那么GreenCoffeeTest类必须有默认的空构造函数或者什么? – getsadzeg

+0

不知道。对我来说,这两件东西(GreenCoffee)和编号似乎根本就不能像现在一样与对方玩。 – GhostCat

回答

1

关于有关构造函数的问题。由于这样的事实,在GreenCoffee测试要求:

@RunWith(Parameterized.class) 

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

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

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

这样,您可以接收多个值(封装在一个对象)测试构造。

+0

是的,我得到了这个构造函数,谢谢。但我解决了上面的错误,只是修复了结构。 – getsadzeg