使用Eclipse在junit中初始化错误

时间:2015-02-24 14:07:38

标签: junit

以Junit身份运行会导致初始化错误:

import java.io.IOException;
//import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import Test.BaseTest;
import Utils.TestUtils;
@RunWith(Parameterized.class)
public class LoginTest extends BaseTest
{
    String UserName;
    String Password;
    String VerifyText;

    public LoginTest(String UserName,String Password,String VerifyText)
    {
        this.UserName=UserName;
        this.Password=Password;
        this.VerifyText=VerifyText;
    }

    @Parameters
    public static Collection<Object[]>readData()
    {
        return Arrays.asList(TestUtils.readTestData("LoginTest"));
    }
    @BeforeClass
    public static void Init()
    {
        System.out.println("Before class called ....");
    }
    @AfterClass
    public static void CloseBrowser()
    {
        System.out.println("Afetr class called....");

    }
    @Before
    public void openSite() throws IOException
    {
        Initialize();
        dr=new FirefoxDriver();
        driver=new EventFiringWebDriver(dr);
        System.out.println("Before called....");
        driver.get("http://www.rediff.com");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    }
    @Test
    public void LoginTest()throws IOException,InterruptedException
    {
        System.out.println("Test called");
        //skipping the test case
        if(TestUtils.IsSkipped("LoginTest"))
        {
            Assume.assumeTrue(false);
        }
        String result=TestUtils.Login(UserName,Password,VerifyText);
        if(result.contains("Failed"))
        {
            //Take screen shot
            String filename="LoginFailed_"+UserName;
            TestUtils.takeScreenshot(filename);
            //error
            Assert.assertTrue(result, false);

        }
        //logout
        TestUtils.Logout();
    }

}

堆栈追踪:

LoginTest (1)
testSuite1.LoginTest
initializationError(testSuite1.LoginTest)
java.lang.Exception: No public static parameters method on class testSuite1.LoginTest
    at org.junit.runners.Parameterized.getParametersMethod(Parameterized.java:309)
    at org.junit.runners.Parameterized.<init>(Parameterized.java:280)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:28)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

5 个答案:

答案 0 :(得分:3)

如果使用JUnit

,则导入不正确
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

请改用:

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;

错误消息表示JUnit正在寻找使用org.junit.runners.Parameterized.Parameters注释的方法,但它找不到任何方法,因为您的注释使用org.testng.annotations.Parameters

答案 1 :(得分:1)

解决方案非常简单。 我得到了初始化错误,因为我在eclipse测试类文件中选择了错误的文本。 因此,要运行测试Java类文件,我已选择Test Method Name或在java文件中运行所有测试用例,我已选择Java File Name

然后右键单击并运行JUNIT测试。

答案 2 :(得分:0)

不确定原因,但它接缝junit不喜欢以大写字母开头的测试名称:

@Test
public void LoginTest()throws IOException,InterruptedException{
 ...
}

尝试将其更改为loginTest()

对我有用。

答案 3 :(得分:0)

我在项目中添加了J单元,它对我有用。右键单击项目->单击属性->选择Java构建路径->选择库->选择添加库->选择并添加Junit

答案 4 :(得分:0)

好吧,这里的问题是您的Eclipse构建路径的问题。如果未添加JUnit库,则会引发此错误。 将JUnit添加到您的项目中。 右键单击Project->单击Properties->选择Java构建路径->选择库->选择添加库->选择JUnit