Results 1 to 4 of 4

Thread: JUnit

  1. #1
    cka
    cka is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default JUnit

    Hi,
    I'm trying to write JUnit test

    I have class AClass.java
    and class AClassTest.java
    They both are in the same package and there is JUnit library
    I'm using eclipse.


    When I run AClass.java I get this in the console: "Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class"

    When I run AClassTest.java which is JUnit testclass I get this:
    [the error shows when I click on warning]



    except that there is no other messages

    this is where the classes are [if it is of any help]:





    Here's the code:
    AClass.java:
    Java Code:
    public class AClass {
    
    	private double z = 3;
    	private double v = 2;
    	private double k = 1;
    	private double u = 1;
    	private double Sf;
    
    	public static void main(String[] args) {
    		AClass aclass = new AClass(3,2,1);
    		aclass.getPower(2);
    	}
    	
    	public AClass(double z, double v, double k) {
    		this.z = z;
    		this.v = v;
    		this.k = k;		
    	}
    
    	public double getPower(double f) {
    		//System.out.println("f: "+f);
    		Sf = z+v+k+u+f;
    		//System.out.println("Sf: "+Sf);
    		return Sf;
    	}		
    }
    AClassTest.java:
    Java Code:
    import junit.framework.TestCase;
    
    public class AClassTest extends TestCase {
    
    	public AClass aclass;
    	public double z=3;
    	public double v=2;
    	public double k=1;
    	public double freq=2;
    	
    	public AClassTest(String name) {
    		super("hhjj");
    	}
    
    	protected void setUp() throws Exception {
    		aclass = new AClass(z, v, k);
    	}
    
    	protected void tearDown() throws Exception {
    		super.tearDown();
    	}
    	
    	public void testGetPower(double freq) {
    		double expected =  9;
    		assertEquals(expected, aclass.getPower(freq));
    	}
    
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    assuming that junit3.X is really in your classpath try the following inside eclipse: select the file AClassTest.java, right-click and then select Run As -> JUnit Test. i could run the following code as junit test


    Java Code:
    import junit.framework.TestCase;
    
    
    public class AClassTest extends TestCase {
    	public void testAClass() {
    		AClass a = new AClass(1, 2, 3);
    		assertEquals(10.0, a.getPower(3.0));
    	}
    }

  3. #3
    cka
    cka is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    I use Mac OS X and I'm new to it. When I sat the classpath I assumed that I've done it correctly coz there were no errors (and later, for JUnit 4, I've done it the same way and it worked).
    But after I've tried your example I decided that it must be wrong and so I've sat the classpath again.
    I think that it is correct now coz your example is working.


    I have a different question:
    If I sat the classpath and I want to use the JUnit in different project then I just add JUnit library to the given project?
    What if the project containing the JUnit library is from SVN? It shouldn't make any difference, should it?
    Last edited by cka; 07-27-2010 at 04:05 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Usually you'd structure your projects with a lib directory (or equivalent, this assumes it's not a Maven one), but also a test-lib directory (or similar name). That test lib contains libraries associated solely with testing (ie the JUnit jar). There's usually a way to set this up in the IDE, that is saying which libraries are for deployment, and which are not.

Similar Threads

  1. Junit 4. failure(s)
    By sonny in forum New To Java
    Replies: 4
    Last Post: 08-03-2010, 04:35 PM
  2. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  3. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 05:15 PM
  4. Junit
    By Azndaddy in forum New To Java
    Replies: 6
    Last Post: 06-15-2008, 06:47 AM
  5. TRying to use jUnit for Tesing
    By sandor in forum New To Java
    Replies: 3
    Last Post: 04-18-2007, 11:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •