如何在Junit的@BeforeClass中使用@Parameters



我想使用BeforeClass中的Parameters作为不同角色的不同用户登录。

我使用

:

  • JUnitParams 1.0.2
  • junit 4.12
  • Selenium wedbriver 2.53.1
这是我的代码FirefoxLogin.java
public class FirefoxLogin {    
public static WebDriver login(User user, String language){
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.example.org");        
    WebElement loginField = driver.findElement(By.id("username"));
    WebElement passwordField = driver.findElement(By.id("password"));
    WebElement commit = driver.findElement(By.id("kc-login"));
    loginField.sendKeys(user.getLogin());
    passwordField.sendKeys(user.getPassword());
    commit.click();
    setLanguage(driver, user, language);
    return driver;
}
public static void logout(WebDriver driver) {
    driver.findElement(By.linkText("Logout")).click();
    driver.quit();
}
protected static void setLanguage(WebDriver driver, User user, String language) {
    // method to set language
}
}

User.java

public class User {
public static final User USERONE = new User("loginOne", "passwordOne");
public static final User USERTWO = new User("loginTwo", "passwordTwo");
private String login;
private String password;
User(String login, String password) {
    this.login=login;
    this.password=password;
}
public String getLogin(){
    return login;
}
public String getPassword(){
    return password;
}
public static List<User> getUser() {
    List<User> users = new ArrayList<>();
    users.add(USERONE);
    users.add(USERTWO);
    return  users;
}

}

AddFieldTest.java

@RunWith(JUnitParamsRunner.class)
public class AddFieldTest {
private static WebDriver driver;
private static final Object[] userValues() {
    return $(
            $(User.USERONE, "en"),
            $(User.USERTWO, "en")
    );
}
@Parameters(method = "userValues")
@BeforeClass
public static void init(User mUser ,String mLanguage) {
     driver = FirefoxLogin.login(mUser, mLanguage);
}
@AfterClass
public static void close() {
    FirefoxLogin.logout(driver);
}
private Object[] valuesForTest() {
    return $(
            $("street", null, null),
            $(null, "city", null),
            $(null, null, "country"),
            $("ulica", "city", null),
            $("ulica", "city", "country")
    );
}
@Test
@Parameters(method = "valuesForTest")
public void test(String mStreet, String mCity, String mCountry)  {
}

}

下面是mu输出:java.lang.IllegalArgumentException:错误的参数数

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

在这种情况下,异常是由init方法的参数引起的-它们是不允许的。此外,您不能用@Parameters注释@BeforeClass方法-参见此处的讨论。

JUnitParams只支持测试方法的参数化。

@BeforeClass是一个一次性的动作,它应该为测试准备系统

相关内容

  • 没有找到相关文章

最新更新