Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:14:36+00:00 2026-06-18T12:14:36+00:00

I am completely new to Junit and unit testing and I would like to

  • 0

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 the following:

    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 assertions 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 about which assertion to use for this method. Should I use several? Should I use one? What should be trying to prove with the unit testing of this method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:14:37+00:00Added an answer on June 18, 2026 at 12:14 pm
    /**
     * 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);
            }
    
    
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've started to layout unit tests for the following bit of code: public
I've got a method that I'd like to test with JUnit/Mockito. The method takes
i am completely new to Azure. I would like to be able to retrieve
I'm completely new to loading in libraries like this, but here's where I stand:
I'm completely new to NHibernate, following along on the screencast at www.summerofnhibernate.com, which is
I´m completely new to NumPy and tried a textbook code. Unfortunately, at a certain
I`m completely new to MVC. I have to do the following: I have 4
Being completely new to Enterprise Java development and after reading the following tutorial.I managed
Completely new to mongo here. I have following fields in on of my mysql
Completely new to PHP I need to sort an array that was read in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.