java - Null Pointer Exception in Unit test in spock -


i have class "comparisonservice" following fn-

public hashmap <string, map<string, object>> fetchtabledata(datasource datasource, list<object> tableinfo){     table.settableinfo(tableinfo);     hashmap<string, map<string, object>> records = table.fetchdata(datasource);     system.out.println(records.size());     return records; } 

here table object of other class table

i writing spock test method-

class comparisonspec extends spock.lang.specification{ table table=mock() def datasource datasource def list<object> tableinfo=[1] def setup() {     //def datasource datasource  } def "first function"() {     given:     def comparisonservice comparison= new comparisonservice()     when:     comparison.fetchtabledata(datasource,tableinfo)     then:     1*table.settableinfo(_ string)>>true     1*table.fetchdata(_ datasource) } 

when run get

null pointer exception @ comparison.fetchtabledata(datasource,tableinfo).

why so.

ok couple of points can see:

first def datasource datasource trying statically , dynamically type variable. should either:

  • static: datasource datasource
  • dynamic: def datasource

second declare variable datasource never initialise it. either in test or setup need: datasource = <what ever data source is>


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -