'JUnit, Mockito and Spring ApplicationContext: Trouble mocking a property

Problem description: I have trouble setting up a mock for a particular spring bean to return the correct mock test resource location on my development box, rather than the runtime web application root. I am sure I am doing something silly with my refactoring. I hope someone sees it.

I am using Quartz to execute a job over Spring. The Quartz job is working fine and picking up the Spring applicationcontext ok. The only thing left is to wire up a configuration property for the test or runtime location where the web resources are located. :

JUnit 4, Spring 3.1, Quartz, Java 8, Mockito

The interface:

public interface ITitle {
  /**
   * Gets the root of the application.
   * @return location String
   */
  public String getRuntimeLocation();
}

The implementation:

@Component("Title")
public class Title implements ITitle, ApplicationContextAware {

  private String location;

  /**
   * Gets the root of the application.
   * @return location String
  */
 public String getRuntimeLocation() {
  String location = "";

  config = getConfig();
  log.debug("Class Title --- Method getRuntimeLocation -- config is " + config );

  location = config.getRuntimeLocation();
  log.debug("Class Title --- Method getRuntimeMethod -- runtime location is " + location );

  return location;
  }
 }

The Unit Test

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.generators.analytics.serialised.ITitle;

import java.io.File;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath*:/WEB-INF/conf/analytics-context.xml"
})
public class GeneratorTest {

  private AnalyticsGenerator generator;

  @Mock
  private IConfig config;

  @Mock
  private ApplicationContext applicationContext;

  @Mock
  private ITitle title;

  // Set a logger
  private static Logger log =   LoggerFactory.getLogger(GeneratorTest.class);
  private JobExecutionContext job;

  /**
   * Initialises the test parameters.
   */
  @Before
  public void setUp() throws Exception {
      //create a generator object
      generator = new AnalyticsGenerator();

      MockitoAnnotations.initMocks(this);

      when(applicationContext.getBean("Query")).thenReturn(query);
      when(applicationContext.getBean("Config")).thenReturn(config);

            when(config.getRuntimeLocation()).thenReturn(“/Users/me/dev/workarea/");

     generator.executeInternal(ctx);
     }

/**
 * Expected: Json exists
 */
@Test
public void testThatExecuteInternalCreatesAJsonFile() throws JobExecutionException {

    generator.executeInternal(job);

    File returnedJson = new File("classpath:/google-analytics.json");

    Assert.assertNotNull("The JSON file does not exist", returnedJson );
}

/**
 * Remove objects from memory.
 */
@After
public void tearDown() throws Exception {
    generator = null;
}

}

The spring xml file

<?xml version="1.0" encoding="UTF-8"?>
<!--
  * analytics-context.xml
  *
  * resource configuration file for Google Analytics recommendations integration framework.
  *
  * All custom beans and services must be defined in this file.
  -->
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:c="http://www.springframework.org/schema/c"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/util
                        http://www.springframework.org/schema/util/spring-util-3.2.xsd
                        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
                        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<context:annotation-config/>

<!--
    START Globally used Google Analytics Query parameters
-->
<bean id="Config" class=“com.generators.analytics.Config">
    <property name="reportLocation" value="/data/runtime/web/assets/generated-list/google-analytics.json"/>
    <property name="runtimeLocation" value="/data/runtime/web/"/>
</bean>

<bean id="configFactory" class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean">
    <property name="serviceLocatorInterface" value=“com.generators.analytics.ConfigFactory" />
</bean>

<alias name="Config" alias="C" />

<bean id="Title" class=“com.generators.analytics.serialised.Title">
    <property name="config" ref="Config"/>
</bean>

<context:component-scan base-package=“com.generators.analytics" />

<!--
    END Globally used Google Analytics Query parameters
 -->

After running the test I get this in the log:

11:51:43.020 [main] DEBUG com.generators.analytics.serialised.Title - Class Title --- Method getConfig -- applicationContext is org.springframework.context.support.GenericApplicationContext@57f23557: startup date [Tue Jan 24 11:51:31 GMT 2017]; root of context hierarchy
11:51:43.020 [main] DEBUG com.generators.analytics.serialised.Title - Class Title --- Method getConfig -- config is com.generators.analytics.Config@13d9cbf5
11:51:43.020 [main] DEBUG com.generators.analytics.serialised.Title - Class Title --- Method getRuntimeLocation -- config is com.generators.analytics.Config@13d9cbf5
11:51:43.020 [main] DEBUG com.generators.analytics.serialised.Title - Class Title --- Method getRuntimeMethod -- runtime location is /data/runtime/web/

The question is, is there anything obvious that I do wrong to get the intended path /Users/me/dev/workarea/ ?

I guess I need to do refactoring to extract the location from the method ? I am not sure how to refactor this particular step.

    when(config.getRuntimeLocation()).thenReturn(“/Users/me/dev/workarea/");


Solution 1:[1]

I resolved this problem by creating a copy of the spring configuration file and changing

<bean id="Config" class="com.generators.analytics.Config">
    <property name="reportLocation" value="/Users/arnout/dev/soton-test/workarea/assets/generated-list/google-analytics.json"/>
    <property name="runtimeLocation" value="/Users/arnout/dev/soton-test/workarea/"/>
</bean>

and then changing

@ContextConfiguration(locations = {
    "classpath*:/WEB-INF/conf/livesite_customer/resources/test-analytics-resource-config.xml"
})
public class GeneratorTest {

I did some lateral thinking after I read up on Profiles in Spring configurations. In fact I did not need a profile, it was just as easy as taking another spring configuration in test mode.

Solution 2:[2]

How about using @MockBean for ApplicationContext instead of @Mock?

I believe your ApplicationContext class is created by @Autowired in you actual class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dherik
Solution 2 hiroyukik