我正在爲我的服務類編寫單元測試用例。下面是我在控制器代碼:運行Grails服務類的單元測試時發生MissingPropertyException異常
TuneController
def list = {
}
def listData= {
playerId="6600AE"
def tuneInstanceList = new ArrayList<Tune>()
tuneInstanceList = tuneService.calculateId(String playerId)
def editResult = [total: tuneInstanceList.size(), items: tuneInstanceList]
render editResult as JSON;
}
下面是我在TuneService代碼:以下 方法是從的ListData行動呼籲。的ListData在我的js文件中提及 test.js
List<Tune> calculateId(String playerId) {
try{
//read the sql file
String playerSql = grailsApplication.mainContext.getResource('classpath:' + Constants.PLAYER_FILE).inputStream.text
def sql = new groovy.sql.Sql(dataSource)
def params = [playerId:playerId]
def tuneInstanceList = new ArrayList<Tune>()
def results = sql.rows(playerSql, params)
tuneInstanceList = results.each {
def tune = new Tune()
tune.setPlayerId it.player_id
tuneInstanceList.add tune
}
return tuneInstanceList
}catch (Exception ex) {
log.error ex.message, ex
throw ex
}
//finally {
//sql.close()
//}
}
PLAYER_FILE.sql具有以下數據。該文件出現在的grails-app/SQL/PLAYER_FILE.sql
從曲調選擇player_Id其中player_Id =:playerId
Test.js:
Ext.onReady(function(){
// create the Data Store
var ds = new Ext.data.Store({
autoLoad: true,
proxy: new Ext.data.HttpProxy({
url: 'http://localhost:8080/music/tune/listData'}),
reader: new Ext.data.JsonReader({
results: 'total',
root:'items',
id:'id'
},
[
{name: 'playerId' }
]
)
});
var cm = new Ext.grid.ColumnModel([
{header: "Player Id", width: 120, dataIndex: 'playerId' },
]);
cm.defaultSortable = true;
// create the grid
var grid = new Ext.grid.GridPanel({
ds: ds,
cm: cm,
renderTo:'grid-example',
width:540,
height:200
}); });
下面是我寫的測試我的服務班。在這裏我得到了缺失的屬性例外。我相信這是爲 行「def sql = new groovy.sql.Sql(dataSource)」。這是例外情況中提到的。其章程。這個sql。在我的測試中,我必須提及數據源或模擬數據源。但我不太確定。
我的測試用例低於:
void testReturnList() {
TuneService tuneService = new TuneService()
List tuneList = tuneService.calculateId()
assertTrue(tuneList.size()>0)
}
groovy.lang.MissingPropertyException:沒有這樣的屬性:SQL類:pride.TuneService 可能的解決方案:在org.codehaus.groovy.runtime.ScriptBytecodeAdapter登錄 .unwrap(ScriptBytecodeAdapter.java:49) 在org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:86) 在org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java :241) at pride.TuneService.calculateId(TuneService .groovy:67) at org.codehaus.groovy.runtime。org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) (pride.TuneService $ calculateId.call(Unknown Source) 。 callsite.AbstractCallSite.call(AbstractCallSite.java:117) 在pride.TuneServiceTests.testReturnList(TuneServiceTests.groovy:23) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl .java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at junit.framework.TestCase.runTest(TestCase .java:168) at junit.framewor k.TestCase.runBare(TestCase.java:134) at junit.framework.TestResult $ 1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework .TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:232) at junit.framework.TestSuite .run(TestSuite.java:227) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java :46) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit .runner。RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner。運行(RemoteTestRunner.java:390) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
EDITED: Below is the error that I get after commenting sql.close()
顯示java.lang.NullPointerException:無法獲取屬性「mainContext '在空對象 at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:56) at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:156) at org.codehaus。 groovy.runti me.callsite.NullCallSite.getProperty(NullCallSite.java:44) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:237) at pride.TuneService.calculateId(TuneService.groovy:37) at pride.TuneService $ calculateId.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call (AbstractCallSite.java:117) 在pride.TuneServiceTests.testReturnList(TuneServiceTests.groovy:23) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorIm pl.invoke(DelegatingMethodAccessorImpl.java:25) at junit.framework.TestCase.runTest(TestCase.java:168) at junit.framework。java.lang.reflect.Method.invoke(Method.java:597) TestKase.runBare(TestCase.java:134) at junit.framework.TestResult $ 1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult .run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:232) at junit.framework.TestSuite.run (TestSuite.java:227) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83) at or g.clipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org。 eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse。 jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我該如何去關於編寫這個測試用例。思考?
對於將來的問題,請花一些時間來學習StackOverflow代碼格式化程序,使您的問題更清晰,更易於閱讀。 http://stackoverflow.com/editing-help – 2010-10-21 13:18:43
爲什麼你使用grails然後不使用GORM功能?看起來你是groovy sql。 – 2010-10-21 14:16:40
是的......你可以說亞倫。這些是我的項目中使用的自定義查詢。它的一大堆,我已經提到這裏 – MAlex 2010-10-21 18:27:36