Skip to main content

How to write the test cases for private methods?

How to write the test cases for private methods?

Most of us struck while writing the test cases with the private methods and thinks about the how to write test cases for private methods. To test these methods some of the possible methods are shown below,

The following are the methods for writing the test cases,

1. Test these methods using the public methods. The public method that access the private methods has to be tested.

In this method the problem is that some times the scenario can be like we could not have a public method that returns some value. At that case even though we are able to invoke the private methods its waste to invoke as it cannot be tested properly.

2. Using the reflection concept to invoke the private methods. This works very well. I will show you an example here for your understanding of how to use the reflection in testing the private methods.

Consider the private methods inside the target.class as shown below,

class Target
{
       private int calculateAdd(int number1,int number2)
       {
              return number1 + number2;
       }
}

Now create a test class in the android test folder.

class TargetTestCase extends TestCase
{
        @override
        public void setup()
       {}
       
        @Test
        public void testCalculateAdd()
        {
                 Target target = new Target();
                  Class<T>[] paramsTypes = new Class[] {Integer.class, Integer.class}; 
                  Method calculateMethod = Target.class.getDeclaredMethods("calculateAdd",paramsTypes);

                  calculateMethod.setAcessible(true);
                   int number1 = 2;
                   int number2 = 5;
                   int returnVal = (int)calculateMethod.invoke(target,number1,number2);
                   assertEquals(7,returnVal);
        }
}


Hope you got the answer on How to write the test cases for private methods? in junit either in android or in any language.

Follow the post! Happy reading...! :-)

Comments

Popular posts from this blog

How to access the each view of item by position in Recycler View in android ?

How to access the each view of item by position in Recycler View in android ? There are some methods to access the view of each item view to change or update the view during the run time. To access the view of the item view , consider the Recycler view as below, RecyclerView mainRecyclerView = (RecyclerView)view.findViewById(R.id.main_recycler_view); RecyclerAdapter adapter = new RecyclerAdapter(mContext); mainRecyclerView.setAdapter(adapter); main.setLayoutManager(new LinearLayoutManager(mContext));  To access the itemView of each position the following functions can be used,  1. View itemView = mainRecyclerView.getChildAt(positionOfItem);  2. View itemView = mainRecyclerView.findViewHolderForAdapterPosition(Position).itemView;  3. View itemView = mainRecyclerView.findViewHolderForPosition(Position).itemView;  4. Long itemId = mainRecyclerView.getAdapter().getItemId(position);       View itemView = mainRecyclerView....

A.P.I call or async await not working in Array forEach ?

H ello Readers, Welcome back. You would have wondered why does forEach function on the array does not wait for any asynchronous function even if we provide the async await in the node js. If you are the person, wondered about this ever, this post is right for you. Non working example : Lets consider the below snippet, this will not wait for the asynchronous process to wait. I am making a setTimeout to mock the API call async process. This will result the count as, Output : count = 0 OMG !! Why it doesn't work ? Answer probably might be lying inside the Array prototype from JavaScript. Lets take a look inside the "Array.prototype.forEach" function. From the snippet its clear that, the for loop in which they call the callback function does not wait for the asynchronous process to run in the callback. So this forEach is not build for asynchronous process itself. So can't I use forEach any more for running a asynchronous function ? Answer g...

How Machine Learning Works ? - Data Rules the Kingdom

W elcome Back Readers !, For those who have not read the previous post on How Machine Learning works ? Please do read previous one before continuing this so that it would be more easy to understand. You can find the previous post here How Machine Learning Works ? - Mathematics Every Where . For other who are ready to join with me in this journey to understand the Machine Learning, Thank you for being with this post. Till now we are discussing on the the title "How Machine Learning works? " and to understand that we have seen in the first post that there exist the patterns or trends in the data and we try to find that in terms of a mathematical equation called a model and try to fit to the new data that we have not came across for predicting the values. Wow may be we got the entire process complete with this. But there are few thinks that needs to be cared while training the model or creating the model or while processing the data for the model creation. ...