Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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.
 
I don't like that guy. The tiny ad agrees with me.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic