【发布时间】:2020-05-29 01:09:50
【问题描述】:
我有一个下面的课程。我是编写junit测试的新手。我需要为此编写测试用例。 如何在测试类中为 startSchemaMaintenance 方法编写测试方法,因为它正在调用没有参数的私有方法?
public class SchemaMaintenance {
//Loading statuses overview
// NOT_STARTED = 0
// START_LOADING = 1
// IN_PROGRESS = 2
// COMPLETED = 3
// LOADING_ERROR = 4
private static volatile Integer loading_status = 0;
public void startSchemaMaintenance() throws Exception {
if (checkLoadingStatus() == 1) {
doSchemaMaintenance();
loading_status = 3;
}
}
private void doSchemaMaintenance(){
//Do something....
}
private int checkLoadingStatus() throws Exception {
if (loading_status==0 ||loading_status == 2) {
synchronized (loading_status) {
if (loading_status==0) {
loading_status = 2;
return 1;
}else if(loading_status == 2) {
while(loading_status == 2);
if((loading_status == 4)){
throw new Exception("status = " + 4);
}
}else if(loading_status == 4) {
//log.error(generateErrorMessage());
throw new Exception("status = " + 4);
}
}
}else if((loading_status == 4)){
//log.error(generateErrorMessage());
throw new Exception("status = " + 4 );
}
return loading_status;
}
}
【问题讨论】:
-
通过测试调用它的方法来测试它。
-
你的意思是调用 startSchemaMaintenance 的方法
-
没有。您知道您发布的内容不是完全有效的 Java,对吧?您在测试中设置 loadingStatus 的值并调用您的方法
-
不是一个有效的 java??
-
类模式维护是无效的 Java。您的文件的实际名称是什么?