简体   繁体   English

如何在返回 void 类型的公共方法中测试局部变量?

[英]How to test local variables in a public method which returns void type?

My CUT has a public method, "sync()" which returns void and it use some variables like "syncContactEnabled" which they are set using spring and properties file.我的 CUT 有一个公共方法“sync()”,它返回 void,它使用一些变量,如“syncContactEnabled”,它们是使用 spring 和属性文件设置的。 it is my CUT:这是我的剪辑:

public class ContactSyncServiceImpl implements SyncService {

 public void sync() {
        long start = System.currentTimeMillis();
        logger.info("-----sync() start:{}", start);
        if (!syncContactEnabled) {      //***********first: want to test this if
            logger.info("Contact sync isn't enabled.");
            return;
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        Date now = new Date();
        Date startDate = null;
        String contactSyncStartDateConfig = configService.get(CONTACT_SYNC_START_DATE);
        if (StringUtils.isBlank(contactSyncStartDateConfig)) {       \\**********second: I want to test startDate value in this if-else
            startDate = new Date(now.getTime() - syncStartDaysCount * 24 * 60 * 60 * 1000l);
        } else {
            try {
                startDate = formatter.parse(contactSyncStartDateConfig);
            } catch (Exception e) {
                logger.warn("There's a problem parsing date:" + e);
            }
        }


/////////It is NOT complite code of class.......(:
}

first question:第一个问题:

  • How can I mock some variables like "syncContactEnabled" which it is set in properties file?我怎样才能模拟一些变量,比如在属性文件中设置的“syncContactEnabled”?

second question:第二个问题:

  • How can I test internal manner of "sync()" method, for example How can I test "startDate" value in if-else condition?如何测试“sync()”方法的内部方式,例如如何在 if-else 条件下测试“startDate”值?

and finally, it is my properties file:最后,这是我的属性文件:

sync.contact.enabled=false
sync.page.size=100
sync.cron=0 30 6,18 * * ?
#*/1 * * * * *
sync.start.days.count=365

first question:第一个问题:

  • How can I mock some variables like "syncContactEnabled" which it is set in properties file?我怎样才能模拟一些变量,比如在属性文件中设置的“syncContactEnabled”?

You don't.你不知道。

second question:第二个问题:

  • How can I test internal manner of "sync()" method, for example How can I test "startDate" value in if-else condition?如何测试“sync()”方法的内部方式,例如如何在 if-else 条件下测试“startDate”值?

You do not either.你也不知道。

When Unittesting you verify public observable behavior that means: What results are returned depending on the input and how does the unit communicate with its dependencies .当进行单元测试时,您验证公共可观察行为,这意味着:根据输入返回什么结果以及单元如何与其依赖通信

The variable 'syncContactEnabled' is an implementation detail that you do not verify.变量“syncContactEnabled”是您未验证的实现细节。 This is because in may change without changing the units behavior and you don't want to change the test in that case.这是因为 in 可能会在不更改单元行为的情况下更改,并且您不想在这种情况下更改测试。


I think, I just can use some test like: PowerMockito.donothing or PowerMockito.verify..... yes????我想,我可以使用一些测试,例如:PowerMockito.donothing 或 PowerMockito.verify..... 是吗???? and I can not test internal manner... – m.mjn202而且我无法测试内部方式... – m.mjn202

Avoid PowerMock(-ito) by any chance.千万不要使用PowerMock(-ito) In most cases the need of PowerMock(-ito) is a sign of bad design and the lack of dependency injection .在大多数情况下,对PowerMock(-ito)的需要是糟糕设计和缺乏依赖注入的标志。

Usually you mock dependencies , other classes your unit communicates with.通常你模拟dependencies ,你的单元与之通信的其他类。 The only exception is that your unit is an abstract class .唯一的例外是你的单元是一个抽象类

The doNothing(mock).someMethod() configuration is needed, if you mocked a real class (instead of an interface) and you need to prohibit the method ( someMethod() ) to be called (eg because it connects to a database...) doNothing(mock).someMethod()配置是必需的,如果你模拟了一个真实的类(而不是接口)并且你需要禁止调用方法( someMethod() )(例如因为它连接到数据库.. .)

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

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