sebastianbenz / Jnario

Executable specifications for Java
135 stars 38 forks source link

Jnario is not working with Mockito #9

Closed BryanHunt closed 12 years ago

BryanHunt commented 12 years ago

I'm trying to get Jnario to work with Mockito, and I'm having problems. Here's a simple example:

package org.example.target

import static org.mockito.Mockito.*;
import java.util.List

describe "Test"{
    before {
        myList = mock(List::class)  << Incompatible types.  Expected java.uti.List or java.lang.Object[] but was java.lang.Object
    }

    List myList;
}

Here's something else I tried:

package org.example.target

import static org.mockito.Mockito.*;
import java.util.List

describe "Test"{
    List myList = mock(List::class) << Incompatible implicit return type.  Expected java.uti.List or java.lang.Object[] but was java.lang.Object
}

Ideally, I'd like to use @Mock, but that requires either the MockitoJUnitRunner which I understand we can't use, or

MockitoAnnotations.initMocks(this)

which does not work either.

sebastianbenz commented 12 years ago

The problem is that you access the type in xtend differently than in java: List.class => typeof(List). Here is a working example:

package demo

import static org.mockito.Mockito.*
import static org.mockito.MockitoAnnotations.*
import java.util.List
import org.mockito.Mock

describe "Mockito"{
  describe "standard"{
    val mock = mock(typeof(List)) 
    fact mock should not be null
  }

  describe "annotation"{
    @Mock List mock 
    before initMocks(this)
    fact mock should not be null
  }  
}

Jnario supports also custom test factories (see http://jnario.org/org/jnario/spec/tests/integration/SpecInstantiationSpec.html). You can create a custom factory for Mockito (see https://github.com/bmwcarit/Jnario/blob/master/org.jnario.examples/specs/gameoflife/MockitoInstantiator.java) and use it in your spec by annotating the root spec with: @InstantiateWith(MockitoInstantiator.

BryanHunt commented 12 years ago

Ah, I got it working. Thanks for your patience.