LocalServiceTestHelper

com.google.appengine.tools.development.testing

Class LocalServiceTestHelper

  • java.lang.Object
    • com.google.appengine.tools.development.testing.LocalServiceTestHelper


  • public class LocalServiceTestHelper
    extends java.lang.Object
    Helper class for testing against local app engine services. Construct the helper with one LocalServiceTestConfig instance for each service that you wish to access as part of your test. Then call setUp() before each test executes and tearDown() after each test executes. No specific test-harness is assumed, but here's a JUnit 3 example that uses task queues and the datastore.
     public void MyTest extends TestCase {
    
       private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
         new LocalTaskQueueTestConfig(), new LocalDatastoreServiceTestConfig());
    
       @Override
       public void setUp() {
         super.setUp();
         helper.setUp();
       }
    
       @Override
       public void tearDown() {
         helper.tearDown();
         super.tearDown();
       }
     }
     
    • Constructor Detail

      • LocalServiceTestHelper

        public LocalServiceTestHelper(LocalServiceTestConfig... configs)
        Constructs a LocalServiceTestHelper with the provided configurations.
        Parameters:
        configs - for the local services that need to be set up and torn down.
    • Method Detail

      • setEnvAppId

        public LocalServiceTestHelper setEnvAppId(java.lang.String envAppId)
        The value to be returned by ApiProxy.getCurrentEnvironment().getAppId()
        Parameters:
        envAppId -
        Returns:
        this (for chaining)
      • setEnvModuleId

        public LocalServiceTestHelper setEnvModuleId(java.lang.String envModuleId)
        The value to be returned by ApiProxy.getCurrentEnvironment().getModuleId()
        Parameters:
        envModuleId -
        Returns:
        this (for chaining)
      • setEnvVersionId

        public LocalServiceTestHelper setEnvVersionId(java.lang.String envVersionId)
        The value to be returned by ApiProxy.getCurrentEnvironment().getVersionId()
        Parameters:
        envVersionId -
        Returns:
        this (for chaining)
      • setEnvInstance

        public LocalServiceTestHelper setEnvInstance(java.lang.String envInstance)
        The current instance id held by ApiProxy.getCurrentEnvironment()
        Parameters:
        envInstance - must an integer >= -1 (default instance)
        Returns:
        this (for chaining)
      • setEnvEmail

        public LocalServiceTestHelper setEnvEmail(java.lang.String envEmail)
        The value to be returned by ApiProxy.getCurrentEnvironment().getEmail()
        Parameters:
        envEmail -
        Returns:
        this (for chaining)
      • setEnvIsLoggedIn

        public LocalServiceTestHelper setEnvIsLoggedIn(boolean envIsLoggedIn)
        The value to be returned by ApiProxy.getCurrentEnvironment().isLoggedIn()
        Parameters:
        envIsLoggedIn -
        Returns:
        this (for chaining)
      • setEnvIsAdmin

        public LocalServiceTestHelper setEnvIsAdmin(boolean envIsAdmin)
        The value to be returned by ApiProxy.getCurrentEnvironment().isAdmin()
        Parameters:
        envIsAdmin -
        Returns:
        this (for chaining)
      • setEnvAuthDomain

        public LocalServiceTestHelper setEnvAuthDomain(java.lang.String envAuthDomain)
        The value to be returned by ApiProxy.getCurrentEnvironment().getAuthDomain()
        Parameters:
        envAuthDomain -
        Returns:
        this (for chaining)
      • setRemainingMillisTimer

        public LocalServiceTestHelper setRemainingMillisTimer(LocalServiceTestHelper.RequestMillisTimer timer)
        Sets the object that will return the value to be returned by ApiProxy.getCurrentEnvironment().getRemainingMillis()
        Parameters:
        timer - The timer that returns the amount of time left.
        Returns:
        this (for chaining)
      • setEnvRequestNamespace

        public LocalServiceTestHelper setEnvRequestNamespace(java.lang.String envRequestNamespace)
        The value to be returned by ApiProxy.getCurrentEnvironment().getRequestNamespace()
        Parameters:
        envRequestNamespace -
        Returns:
        this (for chaining)
      • setEnvAttributes

        public LocalServiceTestHelper setEnvAttributes(java.util.Map<java.lang.String,java.lang.Object> envAttributes)
        The value to be returned by ApiProxy.getCurrentEnvironment().getAttributes()
        Parameters:
        envAttributes -
        Returns:
        this (for chaining)
      • setClock

        public LocalServiceTestHelper setClock(Clock clock)
        Sets the clock with which all local services will be initialized. Note that once a local service is initialized its clock cannot be altered.
        Parameters:
        clock -
        Returns:
        this (for chaining)
      • setEnforceApiDeadlines

        public LocalServiceTestHelper setEnforceApiDeadlines(boolean val)
        Determines whether or not API calls should be subject to the same deadlines as in production. The default is false.
        Parameters:
        val -
        Returns:
        this (for chaining)
      • setSimulateProdLatencies

        public LocalServiceTestHelper setSimulateProdLatencies(boolean val)
        Determines whether or not local services should simulate production latencies. The default is false.
        Parameters:
        val -
        Returns:
        this (for chaining)
      • setTimeZone

        public LocalServiceTestHelper setTimeZone(java.util.TimeZone timeZone)
        Sets the time zone in which tests will execute. If not set we use the same timezone that we use in production and the dev appserver: UTC. Note that if your code has permission to modify the user.timezone system property, this will change the default timezone for the JVM. However, if your code does not have this permission, the timezone will only be altered for the current thread.
        Parameters:
        timeZone - the time zone
        Returns:
        this (for chaining)
      • setUp

        public final LocalServiceTestHelper setUp()
        Set up an environment in which tests that use local services can execute.
        Returns:
        this (for chaining)
      • newEnvironment

        protected com.google.apphosting.api.ApiProxy.Environment newEnvironment()
        Constructs the ApiProxy.Environment that will be installed. Subclass and override to provide your own implementation.
      • newLocalServerEnvironment

        protected LocalServerEnvironment newLocalServerEnvironment()
        Constructs the LocalServerEnvironment that will be installed. Subclass and override to provide your own implementation.
      • newLocalServerEnvironment

        protected LocalServerEnvironment newLocalServerEnvironment(java.lang.Integer apiServerPort)
      • tearDown

        public final void tearDown()
        Tear down the environment in which tests that use local services can execute.
      • endRequest

        public static void endRequest()
        Indicate the request has ended so that local services can do any post request work. This method is optional and is automatically done in tearDown(). You only need to call this method if you want to call tearDown() from your own tearDown / @After method but need to end the request to verify any behavior.
      • getApiProxyLocal

        public static ApiProxyLocal getApiProxyLocal()
        Convenience function for getting ahold of the currently registered ApiProxyLocal.
      • getLocalService

        public static LocalRpcService getLocalService(java.lang.String serviceName)
        Convenience function for getting ahold of a specific local service. For example, to get ahold of the LocalDatastoreService you would call getLocalService(LocalDatastoreService.PACKAGE).