File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Groovy and the fly likes Groovy's MockFor doesn't work on a simple example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Groovy Watch "Groovy New topic
Author

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

Kirk Walker
Greenhorn

Joined: Oct 08, 2013
Posts: 1
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Groovy's MockFor doesn't work on a simple example