Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Groovy's MockFor doesn't work on a simple example

 
Kirk Walker
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote two simple classes and a test for them. Here is the code:

package mocking;

public class Airplane {

public void retractWheels(String retractionSystem){
System.out.println("The retraction system is: " + retractionSystem);
}

public void startEngine(String engineType){
Engine engine = new Engine();
System.out.println(engine.getEngineType("Boeing 777"));
}

}


package mocking;

public class Engine {

public String getEngineType(String airplaneType){
return "Mercedes";
}
}

package mocking;

import static org.junit.Assert.*;

import groovy.mock.interceptor.MockFor
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

class AirplaneTest {

Airplane airplane = null
@Before
public void setUp() throws Exception {
airplane = new Airplane()
}

@After
public void tearDown() throws Exception {
airplane = null
}

@Test
public void test() {
def mockEngine = new MockFor(Engine.class);

mockEngine.demand.getEngineType{airplaneType -> "goofy"}

mockEngine.use{
airplane.startEngine("Boeing");
}
}

}

Every time that I run this, it prints Mercedes. I have also tried the test like this:

package mocking;

import static org.junit.Assert.*;

import groovy.mock.interceptor.MockFor
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

class AirplaneTest {

Airplane airplane = null
@Before
public void setUp() throws Exception {
airplane = new Airplane()
}

@After
public void tearDown() throws Exception {
airplane = null
}

@Test
public void test() {
def mockEngine = new MockFor(Engine.class);

mockEngine.demand.getEngineType{"goofy"}

mockEngine.use{
airplane.startEngine("Boeing");
}
}

}

and the same outcome. Why doesn't the method get recognized? Is it because it takes in a parameter? It is really crazy if that is the case.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic