How to run junit test on a method with parameters and custom return object

I am completely new to Junit and unit testing and I would like to start unit testing my code. I would like to start with a method that looks like this:

    public Store loadStore(Integer customerId,
                Integer storeId){
        //The logic of this method selects a store from a database based on the parameter criteria
    }

      

From this article http://www.vogella.com/articles/JUnit/article.html I have read that the following statements can be made:

  • fail (String) assertTrue (true)
  • assertTrue ([message], boolean condition)
  • assertsEquals ([String message], expected, actual)
  • assertsEquals ([String message], expected, actual, tolerance)
  • assertNull ([message], object)
  • assertNotNull ([message], object)
  • assertSame ([String], expected, actual)
  • assertNotSame ([String], expected, actual)

I am confused which statement should be used for this method. Should I use multiple? Should I use it? What should you try to prove when you unit test this method?

+3


source to share


3 answers


/**
 * This is a small example to get you started.
 * This is an example of a test class to test your loadStore
 * It assumes this method is defined in a class called StoreManager.
 * It encapsulates all the tests that are required within the same test method for
 * expediency sake. You generally should restrict one core functionality per test.
 * but if writing functional test, you can groupd core functionality together.
 * 
 * Provide you default input data. Provide data that should pass as well
 * as data that should fail or error according to all the possible conditions you can think off.
 * 
 * it is fine to hardcode the data when when starting out but later you need to look
 * into defining test data in some sort of reusable way as well as easy to maintain as data changes.
 * 
 * it is good practice not to run test against DB databases.
 * 
 * 
 */

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class StoreManagerTest {

@BeforeClass
    public void before()
    {
    //in here do some pre-initilization for each test class if needed
    //database login if needed 
    //mock objects if needed etc
    }

@Before
public void beforeTest()
{
    //in here do some pre-initilization for each test if needed

}

@After
public void afterTest()
{
    //in here do some cleanup for each test if needed
}




@Test
public void testloadStore() {

        StoreManager storeManager = new StoreManager();
        Integer validCustomerId = new Integer(9876);
        Integer invalidCustomerId = new Integer(-10);

        Integer validStoreId = new Integer(2345);
        Integer invalidStoreId = new Integer(-345);

        String validCustomerName = new String("validname);

        String validStoreName = new String("validStoreName);

        //Test first with valid id.
        //expect to return a store. if store is undefined the assertTrue will fail.
        //if you get an error fail. need to invetigate why you are failing with valid store id and valid customer id.
        try{
            Store store = storeManager.loadStore(validCustomerId, validStoreId);
            assertNotNull(store);
            assertTrue(store.getStoreId().equals(validStoreId));
            assertTrue("customer id match : " , store.getCustomerId().equals(validCustomerId));
            //you can also check for other parameters that exist in store object
            assertSame("customer Name match :  ", store.getCustomerName(), validCustomerName);
            assertSame("Store Name match :  ", store.getStoreName(), validStoreName);
        }catch(Exception e){
            fail("testHashCode Failed! " + e.getMessage());
        }


        //Test  with valid id and invalid customerid
        //expect error or null store. if store is undefined the assertTrue will pass.
        //if you get an error fail perfectly valid to assume the test passed but you can write other test to test
        //whether the error condition is what was expected.
        try{
            Store store = storeManager.loadStore(invalidCustomerId, validStoreId);
            assertNull(store);
        }catch(Exception e){
            assertTrue("No Store matching :  "+invalidCustomerId, true);
        }

        //Test  with invalid  storeid and valid customerid
        //expect error or null store. if store is undefined the assertTrue will pass.
        //if you get an error fail perfectly valid to assume the test passed but you can write other test to test
        //whether the error condition is what was expected.
        try{
            Store store = storeManager.loadStore(validCustomerId, invalidStoreId);
            assertNull(store);
        }catch(Exception e){
            assertTrue(true);
        }


        //Test  with invalid  storeid and invalid customerid
        //expect error or null store. if store is undefined the assertTrue will pass.
        //if you get an error fail perfectly valid to assume the test passed but you can write other test to test
        //whether the error condition is what was expected.
        try{
            Store store = storeManager.loadStore(null, null);
            assertNull(store);
        }catch(Exception e){
            assertTrue(true);
        }


}

}

      



+1


source


The general idea might be something like this:

Store store = loadStore(1, 1);
assertTrue("Test failed", store.somePropertyOrMethodOfYourStore==SomeValueYouExpect);

      



While you should honestly make a tutorial, this is a very basic unit testing stuff in general.

+1


source


If the method is accessing a database, you can use an in-memory database instead of a production database. And in the @Before and @Before methods, you can initialize the state or clear the state.

If it is difficult to create context around an object, you can use frameworks like Mockito or Powermock to forge the context and test only parts (units).

I would recommend reading a few tutorials first.

+1


source







All Articles