0

I'm trying to get a String/class/file/name from the user that specifies which class to run Junit tests on. It compiles and runs however, the Junit testing says Class Not Found and gives me an error. I'm not sure how to go about fixing this or working around it.

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)
Trent
  • 5
  • 1
  • 6

1 Answers1

0

Your test methods in class Tester should have @Test annotation. Also the class Utf should not extend 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

}
Sachin
  • 394
  • 1
  • 5
  • If you don't want to use @Test annotation. The Tester class should extend the TestCase. – Sachin Mar 15 '14 at 21:23
  • I changed my code to reflect this, im still getting class not found – Trent Mar 15 '14 at 21:30
  • Ok, i changed if (name == name) to (.equals) version and then i get this error. Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) – Trent Mar 15 '14 at 21:32
  • See this http://stackoverflow.com/questions/4228047/java-lang-noclassdeffounderror-in-junit regarding missing dependencies. – Sachin Mar 15 '14 at 21:35
  • Thanks for the link, I forgot the simplest thing in the world. Thanks for the quick responses as well. – Trent Mar 15 '14 at 21:37