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 4 of 4

Thread: JUnit AssertEquals

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post JUnit AssertEquals

    Hey,

    I am doing a Junit test case, but I keep having issues with AssertEquals ( object expected, object actual).
    I don't know how to reference the other actual object so that it can compare to the expected. Please help, I am stuck
    public void add (Distance d) throws CustomException
    	{
     
    		//I can also convert feet to inches and add all inches together and then divided inches to feet and inches
    		Distance result = new Distance();
    		int newFeet = this.feet + d.getFeet();
    		int newInches = this.inches + d.getInches();
    		if(newInches > 11)
    		{
    			newFeet = newFeet + (newInches/12);
    			newInches = newInches % 12;
    		}
    		result.setFeet(newFeet);
    		result.setInches(newInches);
     
    		System.out.println("Addition: " + result);
    	}
    public class DistanceTest 
    {
     
    	@Test
    	public void testAdd() 
    	{
    //		Distance num = new Distance (5,5);
    //		Distance test = new Distance (5,5);
    		Distance result = new Distance (10,10);
    		assertEquals(result,num.add(test));
    		//API uses assertEquals(object expected, object actual)
    	}


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JUnit AssertEquals

    I'm not sure what you mean by "having issues," so I'll provide general guidance. The use of 'object' may have been a bad choice of words, but we'll look beyond that. The expected item could be as simple as precomputed constant, String, or boolean. For example, you can calculate in advance that 5 + 5 = 10, so to test a method that adds two numbers called myAdd( int x, int y ), you might use assertEquals() like:

    assertEquals( 10, myAdd( 5, 5 ) );

    Please come back and clarify what help you need if this doesn't clear it up for you.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JUnit AssertEquals

    Quote Originally Posted by GregBrannon View Post
    I'm not sure what you mean by "having issues," so I'll provide general guidance. The use of 'object' may have been a bad choice of words, but we'll look beyond that. The expected item could be as simple as precomputed constant, String, or boolean. For example, you can calculate in advance that 5 + 5 = 10, so to test a method that adds two numbers called myAdd( int x, int y ), you might use assertEquals() like:

    assertEquals( 10, myAdd( 5, 5 ) );

    Please come back and clarify what help you need if this doesn't clear it up for you.
    That is my problem, my add method only takes in one parameter not two, would you suggest switching up my add and subtract method to taking it two parameters, so that I can make the assertEquals work?
    currently I have
    add(Distance d)
    should I switch it to
    add (distance a, distance b)
    I think that would solve the problem?
    thanks greg

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JUnit AssertEquals

    Your add method doesn't return anything and doesn't modify the original object. How can you check anything? Everything you do in the add method is lost the second the method ends. If you want to check the result of the add method, you need to return a result.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. JUnit Testing
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2012, 11:19 AM
  2. JUnit problems
    By degude in forum Java SE APIs
    Replies: 8
    Last Post: September 19th, 2011, 05:54 AM
  3. Junit and TestNG which one is best?
    By jammychen in forum Java Theory & Questions
    Replies: 0
    Last Post: November 7th, 2010, 05:32 AM
  4. JUnit test for ER
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 09:26 PM
  5. [SOLVED] JUnit initialization error
    By raphytaffy in forum Java IDEs
    Replies: 2
    Last Post: September 20th, 2010, 05:33 PM