title: Null Pointer Exception in Unit test in spock
body: <p>i Have a class "ComparisonService" with the following fn-</p>
  
  <pre><code>public HashMap &lt;String, Map&lt;String, Object&gt;&gt; fetchTableData(DataSource dataSource, List&lt;Object&gt; tableInfo){
      table.setTableInfo(tableInfo);
      HashMap&lt;String, Map&lt;String, Object&gt;&gt; records = table.fetchData(dataSource);
      System.out.println(records.size());
      return records;
  }
  </code></pre>
  
  <p>Here table is an Object of other class Table</p>
  
  <p>I am writing a spock test for this Method-</p>
  
  <pre><code>class ComparisonSpec extends spock.lang.Specification{
  Table table=Mock()
  def DataSource dataSource
  def List&lt;Object&gt; 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(_ as String)&gt;&gt;true
      1*table.fetchData(_ as DataSource)
  }
  </code></pre>
  
  <p>When I run it I get</p>
  
  <blockquote>
    <p>null pointer Exception at
    comparison.fetchTableData(dataSource,tableInfo).</p>
  </blockquote>
  
  <p>why is that so.</p>