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 - How does it execute it's methods?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default JUnit - How does it execute it's methods?

    Hello all

    I am taking a programming class that is having me test programs using JUnit. I am actually going along quite fine, but I have a question. Do the methods get called in some sort of order?

    I would think it would execute in some sort of set order, but I find it strange that I can manipulate an object I defined in the constructor in one method, and then call up that same object in another method, and it's as though it's been re-initialized to the value I set it to in the constructor.

    This is actually great, as it makes testing the object much easier. But I am wondering if what I am observing is how JUnit is supposed to operate, or if I am just misunderstanding things.

    Does this question make sense? Really, all I want to know is if when testing an object defined in the constructor of a JUnit test, each method manipulates the constructor object separately?


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

    Default Re: JUnit - How does it execute it's methods?

    Showing an example of what you're talking about would probably help but may not be necessary if I've addressed your questions adequately below.

    You ask, "Do the methods get called in some sort of order?" What methods?
    I find it strange that I can manipulate an object I defined in the constructor in one method, and then call up that same object in another method, and it's as though it's been re-initialized
    The @Before annotation can be used to ensure that the code so annotated will run before each test, so this could be responsible for the behavior you've observed.

    You should review all possible JUnit annotations and the available assertions so that you have an overview of the possibilities in mind as you use them or consider how they might be helpful to your test cases.

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

    Hisma (September 18th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default Re: JUnit - How does it execute it's methods?

    Thanks Greg. You actually answered my question perfectly. Here is the code I was running.

    This is a JUnit test class I created in eclipse -

    package edu.ncsu.csc216.cash_register;
     
    import static org.junit.Assert.*;
     
    import org.junit.Before;
    import org.junit.Test;
     
    /**
     * @author Richard Meyer
     *
     */
    public class CurrencyCollectionTest {
    	   /** New CurrencyCollection object for testing of array 
    	    * currency objects. */
    	   private CurrencyCollection cashDrawer;
    	   /** New array of Currency objects for testing of
    	    *  individual currency objects. */
    	   private Currency [] currency;
     
    	/**
             * Sets up CurrencyCollectionTest by creating a CurrencyCollection 
    	 * object to act as a cash drawer.  
    	 * @throws java.lang.Exception
    	 */
    	@Before
    	public void setUp() throws Exception {
    		//Initialize cashDrawer for CurrencyCollection array testing.
    		cashDrawer = new CurrencyCollection(10);
    	}
     
    	/**
    	 * Create a currency object to test the object properties match the index
    	 * of the CurrencyCollection object in the constructor.
    	 * Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
    	 * #getCurrencyAtIdx(int)}.
    	 */
    	@Test
    	public void testGetCurrencyAtIdx() {
    		//Currency object with the value of a nickel and a count of 10.
    		Currency nickel = new Currency(CurrencyCollection.NICKEL_VALUE,
    				CurrencyCollection.NICKEL_NAME, 10);
    		//Test nickel currency object exists in expected index location.
    		assertTrue(nickel.equals(cashDrawer.getCurrencyAtIdx(1)));
     
    		//Ensure the method can cope with index out of bounds exceptions.
    		try {
    			//Call currency index out of bounds
    			cashDrawer.getCurrencyAtIdx(8);
    			fail(); //We should never reach this point, if we do, the test fails
    		} catch (IndexOutOfBoundsException e) {
    		}
    	}
     
    	/**
    	 * Test by increasing the amount of a denomination by a set value and 
    	 * ensuring the count has been increased using the getCount() method.
    	 * Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
    	 * #modifyDenomination(int, int)}.
    	 */
    	@Test
    	public void testModifyDenomination() {
    		//Modify denomination by 4 five dollar bills.
    		cashDrawer.modifyDenomination(500, 4);
    		//Store currency collection in currency array to call up
    		//individual denominations.
    		currency = cashDrawer.getCurrencyCollection();
    		//We should expect to see 14 five dollar bills total,
    		//10 initial + the 4 we just added.
    		assertEquals(14, currency[5].getCount());
    		//Ensure the method can catch an invalid currency value.
    		try {
    			//Pass a denomination that does not exist
    			cashDrawer.modifyDenomination(10001, 2);
    			fail(); //We should never reach this point, if we do, the test fails
    		} catch (IllegalArgumentException e) {
    		}
    	}
     
    	/**
    	 * Create a new CurrencyCollection payment object to simulate currency 
    	 * deposit.  Verifies the deposit by calling the getBalance() method and 
    	 * compares it with the expected amount.
    	 * Test method for {@link
    	 * edu.ncsu.csc216.cash_register.CurrencyCollection
    	 * #depositCurrencyCollection(edu.ncsu.csc216.cash_register.
    	 * CurrencyCollection)}.
    	 */
    	@Test
    	public void testDepositCurrencyCollection() {
    		//create a CurrencyCollection object to simulate purchasing an item.
    		CurrencyCollection payment = new CurrencyCollection();
    		//Collect 5 dollars and put into new payment object.
    		payment.modifyDenomination(500, 1);
    		//Deposit the payment into the cashDrawer.
    		cashDrawer.depositCurrencyCollection(payment);
    		//Verify the total in the cashDrawer reflects the payment.
    		//Total should equal starting amount + 500.
    		assertEquals(36910, cashDrawer.getBalance());
    	}
     
    	/**
    	 * Simulates a Currency refund by passing a refund amount to the cashDrawer
    	 * object, and ensuring the getBalance() method returns the expected 
    	 * refund amount.
    	 * Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
    	 * #refundByAmount(int)}.
    	 */
    	@Test
    	public void testRefundByAmount() {
    		//Pass a refund amount (amount for a t-shirt).
    		cashDrawer.refundByAmount(1827);
    		//Ensure cash drawer total is reduced by refunded amount.
    		assertEquals(34583, cashDrawer.getBalance());
    	}
     
    	/**
    	 * Creates a new CurrencyCollection array to compare with CurrencyCollection
    	 * in our constructor method.
    	 * Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
    	 * #getCurrencyCollection()}.
    	 */
    	@Test
    	public void testGetCurrencyCollection() {
    		//Create new CurrencyCollection object for this method only.
    		CurrencyCollection collectionTest = new CurrencyCollection(10);
    		//Compare the new CurrencyCollection object with the CurrencyCollection
    		//object defined in the constructor method as arrays.
    		assertArrayEquals(collectionTest.getCurrencyCollection(),
    				cashDrawer.getCurrencyCollection());
    	}
     
    	/**
    	 * Check the total amount in CurrencyCollection object.
    	 * Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
    	 * #getBalance()}.
    	 */
    	@Test
    	public void testGetBalance() {
    		//check initial amount in cashDrawer.
    		assertEquals(36410, cashDrawer.getBalance());
    	}
     
    }

    So the code in the @Before tag (setUp() instruction) must have ensured that I have a fresh cashDrawer object for use with each method, right? That means if I manipulate the object in one of my test methods, it will be "re-initialized" by my setUp() method for use on the next method, right?

Similar Threads

  1. how to execute multiple different queries in one execute?
    By Sakina in forum JDBC & Databases
    Replies: 1
    Last Post: June 9th, 2012, 09:40 AM
  2. JUnit Testing
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2012, 11:19 AM
  3. JUnit problems
    By degude in forum Java SE APIs
    Replies: 8
    Last Post: September 19th, 2011, 05:54 AM
  4. Junit and TestNG which one is best?
    By jammychen in forum Java Theory & Questions
    Replies: 0
    Last Post: November 7th, 2010, 05:32 AM
  5. JUnit test for ER
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 09:26 PM