[英]Grails/groovy testing - any difference between assert and assertEquals methods
[英]How to define factory methods in Groovy/Grails?
我在groovy中建立了一個工廠,此代碼有效,但我認為可以改進(或減少)此代碼:
abstract class Processing {
abstract getDirName();
abstract getFileType();
abstract moveFile();
abstract processFile();
abstract openFile();
abstract closeFile();
}
class ProcessingBuilder {
def processingFactory
def orderProcess(String type) {
def process = processingFactory.buildProcessing(type);
process.getDirName();
process.getFileType();
process.moveFile();
process.processFile();
process.openFile();
process.closeFile();
return process;
}
}
class ProcessingFactory {
def buildProcessing(String type) {
def process = null;
if (type == "nanostring") {
process = new ProcessingNanoString();
} else if (type == "flowcore") {
process = new ProcessingFlowCore();
}
return process;
}
}
class ProcessingFlowCore extends Processing {
def getDirName() {
println "--> Get FlowCore directory structure"
}
def getFileType() {
println "--> Get FlowCore File Type"
}
def moveFile() {
println "--> Move FlowCore Files"
}
def processFile() {
println "--> Import FlowCore files to DB"
}
def openFile() {
println "--> Open FlowCore files"
}
def closeFile() {
println "--> Close FlowCore files"
}
}
要使用此工廠:
def processingFactory = new ProcessingFactory();
def processingBuilder = new ProcessingBuilder(processingFactory : processingFactory);
def process = processingBuilder.orderProcess("flowcore");
有沒有更好的方法來構建具有Groovy / grails的工廠?
如果嘗試在我的ProcessingFlowCore類中使用服務,我也會嘗試錯誤:ex:
class ProcessingNanoString extends Processing {
def directoryToolsService
def getDirName() {
println "--> Get NanoString directory structure"
def dir = directoryToolsService.findDirPathByName(nanostring).directoryPath
return dir
}
我收到了一個:ERROR errors.GrailsExceptionResolver-無法在空對象上調用方法findDirPathByName(nanostring)。(如果不在工廠,可以調用此服務)。
為什么呢
謝謝。
如果使用您的服務的類在src / groovy中,則不會自動注入。 服務只會自動注入到人工制品類中(例如grails-app下的任何事物)。 要從常規類獲取服務,請使用以下方法手動獲取對它的引用:
import org.codehaus.groovy.grails.commons.ApplicationHolder
service = ApplicationHolder.getApplication().getMainContext().getBean('directoryToolsService')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.