Class SpecHelper

java.lang.Object
org.javalite.activeweb.SpecHelper
All Implemented Interfaces:
JSpecSupport
Direct Known Subclasses:
RequestSpecHelper, ViewSpec

public class SpecHelper extends Object implements JSpecSupport
This class is not used directly in applications.
Author:
Igor Polevoy
  • Constructor Details

    • SpecHelper

      public SpecHelper()
  • Method Details

    • atStart

      @BeforeEach public void atStart()
    • afterEnd

      @AfterEach public void afterEnd()
    • setTemplateLocation

      protected void setTemplateLocation(String location)
      Parameters:
      location - this is a relative location starting from the module root, intended for testing.
    • injector

      protected SpecHelper.DynamicBuilder injector()
      Convenience method: allows to set services without explicitly configuring a new module for mocking. All services are set as "eagerSingleton".

      Example:

       public void before(){
          injector().bind(Greeter.class).to(GreeterMock.class)
                    .bind(Redirector.class).to(RedirectorImpl.class).create();
       }
       

      An example where the first class is also an implementation:

       public void before(){
          injector().bind(Greeter.class).create();
       }
       
      The instance of a new injector will also be added to the current context and used to inject services into filters and controllers executing by this test.

      Each test method can potentially setup its own injector like this, and not interfere with previous settings.

      If you need more advanced settings, use createInjector(AbstractModule) or setInjector(Injector) methods.
      Returns:
      instance of dynamically created injector with interfaces and services already set.
    • setInjector

      protected void setInjector(com.google.inject.Injector injector)
      Use to set injector for current test.

      How to override some services for tests:

               Injector injector  = Guice.createInjector(Modules.override(new CommonModule()).with(new CommonModuleMock());
               setInjector(injector);
           

      Parameters:
      injector - injector to source dependencies form.
    • createInjector

      protected <T extends com.google.inject.AbstractModule> SpecHelper.ModuleBuilder createInjector(T module)
      This is a convenience method for setting Guice modules and service mocks.

      For instance, consider this code:

               Injector injector  = Guice.createInjector(Modules.override(new CommonModule()).with(new CommonModuleMock());
               setInjector(injector);
           
      The mock classes are specified inside the class CommonModuleMock, which means that you have to write the module class. This process is tedious and inflexible in a large project.

      The createInjector(..) method allows for a more elegant way of overriding real services with mocks:

               Injector injector = createInjector(new CommonModule())
                                                .override(EmailService.class).with(EmailServiceMock.class)
                                                .override(SmsService.class).with(SmsServiceMock.class).
                                                .create();
               setInjector(injector);
           
      As you can see, the is no longer need for writing a mock module.

      Parameters:
      module - - main module you want to set on a spec. This module may include services you need to override.
      Returns:
      instance of Injector with services in the main module overridden by provided mocks.
    • registerTag

      protected void registerTag(String name, FreeMarkerTag tag)
      Registers a single custom tag. You can call this method as many times as necessary to register multiple tags in tests. If you want to use all tags that you registered in app.config.AppBootstrap class, then you an option of using AppIntegrationSpec as a super class.
      Parameters:
      name - tag name where name is a part of the tag on page like so: <@name....
      tag - instance of tag to register.
    • session

      protected SessionFacade session()
      Allows access to session in test context.
      Returns:
      object allowing access to session in test context.
    • flash

      protected Object flash(String name)
      Returns a named flash value assigned to session by controller.
      Parameters:
      name - name of flash value.
      Returns:
      flash value assigned to session by controller.
    • flashExists

      protected Object flashExists(String name)
      Tests if flash by name exists.
      Parameters:
      name - name in question
      Returns:
      true if flash exists, false if not. Will return true even if flash by name exists, but its value is null.
    • flash

      protected <T> T flash(String name, Class<T> type)
      Returns a named flash value assigned to session by controller.
      Parameters:
      name - name of flash value.
      type - type to be returned
      Returns:
      flash value assigned to session by controller.
    • session

      protected void session(String name, Serializable value)
      Convenience method, sets an object on a session. Equivalent of:
       
           session().put(name, value)
       
       
      Parameters:
      name - name of object
      value - object itself.
    • sessionObject

      protected Object sessionObject(String name)
      Convenience method, returns object from session, equivalent of:
       
           session().get(name)
       
       
      Parameters:
      name - name of object,
      Returns:
      session object.
    • sessionString

      protected String sessionString(String name)
      Convenience method, returns object from session, equivalent of:
       
           String val = (String)session().get(name)
       
       
      Parameters:
      name - name of object
      Returns:
      value
    • sessionInteger

      protected Integer sessionInteger(String name)
      Convenience method, returns object from session, equivalent of:
       
           Integer val = (Integer)session().get(name)
       
       
      Parameters:
      name - name of object
      Returns:
      value
    • session

      protected <T> T session(String name, Class<T> type)
      Returns object from session that is already cast to expected type.
      Parameters:
      name - name of object in session
      type - expected type.
      Returns:
      object from session that is already cast to expected type.
    • sessionBoolean

      protected Boolean sessionBoolean(String name)
      Convenience method, returns object from session, equivalent of:
       
           Boolean val = (Boolean)session().get(name)
       
       
      Parameters:
      name - name of object
      Returns:
      value
    • sessionDouble

      protected Double sessionDouble(String name)
      Convenience method, returns object from session, equivalent of:
       
           Double val = (Double)session().get(name)
       
       
      Parameters:
      name - name of object
      Returns:
      value
    • sessionFloat

      protected Float sessionFloat(String name)
      Convenience method, returns object from session, equivalent of:
       
           Float val = (Float)session().get(name)
       
       
      Parameters:
      name - name of object
      Returns:
      value
    • sessionLong

      protected Long sessionLong(String name)
      Convenience method, returns object from session, equivalent of:
       
           Long val = (Long)session().get(name)
       
       
      Parameters:
      name - name of object
      Returns:
      value
    • sessionHas

      protected boolean sessionHas(String name)
      Returns true if session has named object, false if not.
      Parameters:
      name - name of object.
      Returns:
      true if session has named object, false if not.