Java Reflection application Example-junit test private method

Source: Internet
Author: User
Tags assert reflection
Remember the previous contact with JUnit, the test of the public method is very fluent, and the private method is also called in these public methods, will test coverage.

Because is the new class, the new method, makes the data step by step the cover to run down, really as long as has the patience, can be done.

But as the project gradually deepened, due to personnel costs, time costs, and so on, the maintenance of test classes do not know from which version,

Which developer modified the business logic started, has a problem, a gleam of red, but the project has been running without any problems [good luck],

And the question of time costs has always existed, but at this point you just modified part of the logic of the private method, but the test class you have to maintain this time,

So there are two points that must be addressed:

1. The original test case according to the existing logic of the "subject" of the transfer.

2. Test the private method business logic that you have modified.

There is no short cut on the 1th, according to the main logic of the transfer.

About the 2nd modifier that can modify the private method, but this method needs to change the code, so it is not very reliable, it is recommended not to use.

This article describes the test of private methods by reflection, which is convenient and practical. Can also profoundly experience the benefits of reflection.


Private method class to be tested
public class Methodclass
{
private int Add (int a, int b)
{
return a + B;
}
}

JUnit Test Classes
public class Methodclasstest extends TestCase
{
public void Testadd ()
{
Methodclass mc = new Methodclass ();

Get Class object
class<methodclass> clazz = Methodclass.class;


Get the method
Try
{
Method method = Clazz.getdeclaredmethod ("Add", new class[] {int.class, int.class});//This can also be used: Integer. TYPE

Method.setaccessible (TRUE);//suppresses access modifiers so that private methods become accessible to

Object result = Method.invoke (MC, New object[] {2, 3});

Assert.assertequals (5, result);
}
catch (Exception e)
{
E.printstacktrace ();

Assert.fail ();
}
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.