简体   繁体   English

使用Mockito对Junit中的私有功能进行单元测试

[英]Unit testing Private Functions in Junit with Mockito

I am Newbie to Java and Junit, 我是Java和Junit的新手,

Does Anyone know how to write test cases for private functions in Junit ? 有谁知道如何在Junit中为私有函数编写测试用例?

After Some research in Google I setup environment and wrote unit test cases for public functions and mocked parent classes with help of Mock() proxy. 在Google进行了一些研究之后,我建立了环境,并在Mock()代理的帮助下为公共功能和模拟的父类编写了单元测试用例。

For public function with inline valued arguments, I reached my expectations and tested both Positive and Negative Path. 对于具有内联值参数的公共功能,我达到了我的期望,并测试了正路径和负路径。

For Private Methods I followed 对于私有方法,我关注了

How do I test a private function or a class that has private methods, fields or inner classes? 如何测试私有函数或具有私有方法,字段或内部类的类?

Method method = targetClass.getDeclaredMethod(methodName, argClasses);
method.setAccessible(true);
return method.invoke(targetObject, argObjects);

And for fields: 对于字段:

Field field = targetClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);

code: 码:

public class universalConfiguration { 
   private Integer getTotalDocuments(Client client, String indexName, String indexType, QueryBuilder searchQueryBuilder) {
    SearchResponse response = client.prepareSearch()
             .setIndices(indexName)
             .setTypes(indexType)
             .setSize(0)
             .setQuery(searchQueryBuilder)
             .execute().actionGet();

     return (int) response.getHits().totalHits;
 }
}

This is My code I want to test this private method with this public class, i tried to use this method using getDeclaredMethod and setAccessible but I am getting errors. 这是我的代码,我想使用此公共类测试此私有方法,我尝试通过getDeclaredMethodsetAccessible使用此方法,但出现错误。

UniversalConfiguration universalConfiguration = new UniversalConfiguration();    
getTotalDocuments getTotalDocuments= universalConfiguration .
                                         getDeclaredMethod(getTotalDocuments,
                                         client,
                                         indexName,
                                         indexType, 
                                         searchQueryBuilder);           
    getTotalDocuments.setAccessible(true);

getTotalDocuments method cannot be resolved to a type getTotalDocuments方法无法解析为类型

Correct me If i did any mistakes. 纠正我,如果我有任何错误。

Change your mind about UnitTests : A UnitTest does not test code , it rather tests behavior . 改变你对单元测试 :一个单元测试测试代码 ,它,而测试的行为 It does that by using the public interface of the unit (which is not necessarily, but usually a public method) and verifies the observable behavior which is return values and or communication with dependencies . 它通过使用单元的公共接口 (不一定,但通常是public方法)来做到这一点,并验证可观察到的行为 ,即返回值和/或与依赖项的通信

private methods are implementation details that may change for refactoring and this is exactly the time when we want our Unittests not to change to guarantee that the desired behavior still exists after the change. private方法是实现细节 ,可能会因重构而更改,而这正是我们希望单元测试更改以确保更改后仍然存在所需行为的时候。

You have some issues with your code, please change it to below: 您的代码有问题,请更改为以下代码:

Class universalConfiguration = UniversalConfiguration.class;    
Class[] cArg = new Class[4];
cArg[0] = Client.class;
cArg[1] = String.class;
cArg[2] = String.class;
cArg[3] = QueryBuilder.class;
Method getTotalDocuments = universalConfiguration.                                          getDeclaredMethod(getTotalDocumentCount,cArg);           
    getTotalDocuments.setAccessible(true);

The question is a little opinion biased. 这个问题有点偏见。 However there are two simple ways you can do this: 但是,有两种简单的方法可以执行此操作:

Test them via a visible method. 通过可见方法对其进行测试。 Change the visibility to default. 将可见性更改为默认。

Default visibility will enable the methods to be seen/called at a package level (the method is no longer private) this is a very effective methodology, however your method is no longer private. 默认可见性将使方法可以在程序包级别被查看/调用(该方法不再是私有的),这是一种非常有效的方法,但是您的方法不再是私有的。

Testing via another visible method will enable you to get effective test coverage, although the test could be a little less specific. 通过另一种可见的方法进行测试可以使您获得有效的测试覆盖率,尽管测试可能不太具体。

How best to approach this will heavily depend on the code you are testing. 如何最好地解决此问题将在很大程度上取决于您正在测试的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM