Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JUnit initialization error

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JUnit initialization error

    Hi all, I'm trying a simple JUnit test with Eclipse but I'm getting an initialization error (attached screenshot). Just looking for some help as to how to clear this error. Here are my classes:

    IntegerCalculator
    public class IntegerCalculator {
     
    	private int amount = 0;
     
    	public IntegerCalculator() {}
    	public IntegerCalculator(int amount) {
    		this.amount = amount;
    	}
     
    	public int getAmount() {
    		return amount;
    	}
     
    	public void add(int val) {
    		amount += val;	// This code is correct.
    	}
     
    	public void subtract(int val) {
    		amount = val;	// This code is wrong.
    	}
     
    	public void divide(int val) {
    		amount /= val;	// This code could generate and error.
    	}
     
    }

    IntegerCalculatorTest
    import static org.junit.Assert.*;
     
    import org.junit.Before;
    import org.junit.Test;
     
     
    public class IntegerCalculatorTest {
     
    	private IntegerCalculator n;
     
    	@Before
    	public void setUp() throws Exception {
    		n = new IntegerCalculator(7);
    	}
     
    	@Test
    	public void testAdd() {
    		n.add(5);
    		assertEquals("(7 + 5)", 12, n.getAmount());
    	}
     
    	@Test
    	public void testSubtract() {
    		n.subtract(3);
    		assertEquals("(7 - 3)", 4, n.getAmount());
    	}
     
    	@Test
    	public void testDivide() {
    		n.divide(0);
    		fail();
    	}
     
    }
    Attached Images Attached Images


  2. #2
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: JUnit initialization error

    hello, i just imported your code in eclipse and i could compile and run your JUnit code succesful. what i created in eclipse is the following: i create a new source folder named test and then i right-clicked this folder and then choosed new -> JUnit Test Case and enter the name IntegerCalculatorTest and then i just copy/pasted your code and finished. when i created the JUnit Test Case i was asked if i wanted to create a JUnit 3 or JUnit 4 and i choosed JUnit 4. try the same and report the result.

    Last edited by j2me64; September 20th, 2010 at 04:00 PM.

  3. The Following User Says Thank You to j2me64 For This Useful Post:

    copeg (September 20th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JUnit initialization error

    Works great! Thanks!

Similar Threads

  1. Initialization parameter of servlets
    By rakesh in forum Java Servlet
    Replies: 3
    Last Post: April 13th, 2010, 03:14 AM
  2. Re-Initialization of FrameView app components??
    By DarkJoy in forum Java Networking
    Replies: 2
    Last Post: November 13th, 2009, 01:19 AM