简体   繁体   中英

installation of JUnit on ecllipse

  1. I have downloaded ecllipse kepler - latest version from ecllipse website.
  2. I downloaded junit 4.10 jar file from sourseforge website (did not get a copy on github)
  3. After opening ecllipse i added my jar file as external jars (it was successfully added)
  4. I imported by saying "import static org.junit.Assert.*;"
  5. then for the testing method i wrote on the top @Test then followed by method definition
  6. Now ecllipse is poping me an error for @Test that -- "cannot convert from test to annotation" and it not even giving me an option to run my class as JUnit test.

Am I missing any step somewhere?

I am pasting my code here :

package test;

import static org.junit.Assert.*;

public class Test {

    @Test
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        assertEquals(0, voidMethod(), 0);
    }

    static int voidMethod()
    {
        return 0;
    }
}

Thanks in advance

You need to import the junit Test class. Use this:

import org.junit.Test;

It would also be best to name your method something other than Test so as not to conflict with this import.

Also, you can't use a static method as a Junit test method and your Junit tests shouldn't have arguments. You will get an exception stating that the test method should not be static, and the test method should not take parameters.

The whole point of Junit Tests is that you have many individual tests that can be run independently of each other. You don't have any arguments passed into the test. Everything you need should be set up in the test or before the test using one of the @Before annotations.

Try something like this:

package test;

import static org.junit.Assert.*;
import org.junit.Test;

public class TestClass {

    @Test
    public void myTestMethod() {
        // TODO Auto-generated method stub
        assertEquals(0, voidMethod(), 0);
    }

    static int voidMethod()
    {
        return 0;
    }
}

unfortunately I had put same name to my class as 'Test' which was actually crating problems..

Thanks a lot...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM