找不到JUnitCore类

时间:2014-03-15 21:10:47

标签: java unit-testing junit filenames

我正在尝试从用户那里获取一个String / class / file / name,指定运行Junit测试的类。然而,它编译并运行,Junit测试说Class Not Found并给我一个错误。我不确定如何解决这个问题或解决它。

import java.util.Scanner;
import junit.framework.*;
import org.junit.runner.JUnitCore;

public class Utf extends TestCase
{
    public static void main(String[] args)
    {
        /*
         * creates and reads from scanner
         */
        Scanner in = new Scanner(System.in);
        String filename = in.nextLine();

        /*
         * gets Tester class' name and stores it as a String
         */
        String className = Tester.class.getName();

        /*
         * if user input String matches Tester class' name then run tests
         * else File Not Found
         */
        if (filename == className) {JUnitCore.main(className);}
        else {System.out.println("File not Found");}
        /*
         * closes scanner
         */
        in.close();

    }
}

public class Tester
{
    /*
     * tests StudentConstructor() method
     */
    public void testStudentConstructor() throws Exception
    {
        try
        {
            String uid = "000";
            String name = "Test Student";
            String email = "test@test.com";
            Student s = new Student(uid, name, email);
            assert (s.getUid() == uid);
            assert (s.getName() == name);
            assert (s.getEmail() == email);
        }
        catch(NullPointerException e){System.out.println("testStudentConstructor() failed: NullPointerException:" + e.getMessage());}
        catch(IllegalArgumentException e){System.out.println("testStudentConstructor() failed: IllegalArgumentException:" + e.getMessage());}
        catch(UnsupportedOperationException e){System.out.println("testStudentConstructor() failed: UnsupportedOperationException:" + e.getMessage());}
    };

    /*
     * tests StudentToString() method
     */
    public void testStudentToString() throws Exception
    {
        try
        {
            String uid = "000";
            String name = "Test Student";
            String email = "test@test.com";
            Student s = new Student(uid, name, email);
            s.studentToString(s);
        }
        catch(NullPointerException e) {System.out.println("testStudentToString() failed: NullPointerException:" + e.getMessage());}
        catch(IllegalArgumentException e){System.out.println("testStudentToString() failed: IllegalArgumentException:" + e.getMessage());}
        catch(UnsupportedOperationException e){System.out.println("testStudentToString() failed: UnsupportedOperationException:" + e.getMessage());}
    }

    /*
     * tests GetUid() method
     */
    public void testGetUid() throws Exception
    {
        try
        {
            String uid = "000";
            String name = "Test Student";
            String email = "test@test.com";
            Student s = new Student(uid, name, email);
            assert (s.getUid() == uid);
        }
        catch(NullPointerException e) {System.out.println("testGetUid() failed: NullPointerException:" + e.getMessage());}
        catch(IllegalArgumentException e){System.out.println("testGetUid() failed: IllegalArgumentException:" + e.getMessage());}
        catch(UnsupportedOperationException e){System.out.println("testGetUid() failed: UnsupportedOperationException:" + e.getMessage());}
    }

    /*
     * tests GetName() method
     */
    public void testGetName() throws Exception
    {
        try
        {
            String uid = "000";
            String name = "Test Student";
            String email = "test@test.com";
            Student s = new Student(uid, name, email);
            assert (s.getName() == name);
        }
        catch(NullPointerException e) {System.out.println("testGetName() failed: NullPointerException:" + e.getMessage());}
        catch(IllegalArgumentException e){System.out.println("testGetName() failed: IllegalArgumentException:" + e.getMessage());}
        catch(UnsupportedOperationException e){System.out.println("testGetName() failed: UnsupportedOperationException:" + e.getMessage());}
    }

    /*
     * tests GetEmail() method
     */
    public void testGetEmail() throws Exception
    {
        try
        {
            String uid = "000";
            String name = "Test Student";
            String email = "test@test.com";
            Student s = new Student(uid, name, email);
            assert (s.getEmail() == email);
        }
        catch(NullPointerException e) {System.out.println("testGetEmail() failed: NullPointerException:" + e.getMessage());}
        catch(IllegalArgumentException e){System.out.println("testGetEmail() failed: IllegalArgumentException:" + e.getMessage());}
        catch(UnsupportedOperationException e){System.out.println("testGetEmail() failed: UnsupportedOperationException:" + e.getMessage());}
    };
}

junit.framework.AssertionFailedError: No tests found in Utf
    at junit.framework.Assert.fail(Assert.java:57)
    at junit.framework.TestCase.fail(TestCase.java:227)
    at junit.framework.TestSuite$1.runTest(TestSuite.java:100)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:131)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:0)

Tester类中的测试方法应该有@Test注释。此类Utf也不应扩展TestCase。

import java.util.Scanner;

import org.junit.runner.JUnitCore;

public class Utf {
    public static void main(String[] args) {
    /*
     * creates and reads from scanner
     */
    Scanner in = new Scanner(System.in);
    String filename = in.nextLine();

    /*
     * gets Tester class' name and stores it as a String
     */
    String className = Tester.class.getName();

    /*
     * if user input String matches Tester class' name then run tests
     * else File Not Found
     */
    if (filename.equals(className)) {
        JUnitCore.main(className);
    } else {
        System.out.println("File not Found");
    }
    /*
     * closes scanner
     */
    in.close();

}

}

Tester.java

import junit.framework.TestCase;

public class Tester extends TestCase{

public void testStudentConstructor() throws Exception
{
    // Your code goes here
}

// Additional code

}