Package org.javalite.activeweb
Class SpecHelper
java.lang.Object
org.javalite.activeweb.SpecHelper
- All Implemented Interfaces:
JSpecSupport
- Direct Known Subclasses:
RequestSpecHelper
,ViewSpec
This class is not used directly in applications.
- Author:
- Igor Polevoy
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
class
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
afterEnd()
void
atStart()
protected <T extends com.google.inject.AbstractModule>
SpecHelper.ModuleBuildercreateInjector(T module)
This is a convenience method for setting Guice modules and service mocks.protected Object
Returns a named flash value assigned to session by controller.protected <T> T
Returns a named flash value assigned to session by controller.protected Object
flashExists(String name)
Tests if flash by name exists.protected SpecHelper.DynamicBuilder
injector()
Convenience method: allows to set services without explicitly configuring a new module for mocking.protected void
registerTag(String name, FreeMarkerTag tag)
Registers a single custom tag.protected SessionFacade
session()
Allows access to session in test context.protected void
session(String name, Serializable value)
Convenience method, sets an object on a session.protected <T> T
Returns object from session that is already cast to expected type.protected Boolean
sessionBoolean(String name)
Convenience method, returns object from session, equivalent of:protected Double
sessionDouble(String name)
Convenience method, returns object from session, equivalent of:protected Float
sessionFloat(String name)
Convenience method, returns object from session, equivalent of:protected boolean
sessionHas(String name)
Returns true if session has named object, false if not.protected Integer
sessionInteger(String name)
Convenience method, returns object from session, equivalent of:protected Long
sessionLong(String name)
Convenience method, returns object from session, equivalent of:protected Object
sessionObject(String name)
Convenience method, returns object from session, equivalent of:protected String
sessionString(String name)
Convenience method, returns object from session, equivalent of:protected void
setInjector(com.google.inject.Injector injector)
Use to set injector for current test.protected void
setTemplateLocation(String location)
-
Constructor Details
-
SpecHelper
public SpecHelper()
-
-
Method Details
-
atStart
@BeforeEach public void atStart() -
afterEnd
@AfterEach public void afterEnd() -
setTemplateLocation
- Parameters:
location
- this is a relative location starting from the module root, intended for testing.
-
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, usecreateInjector(AbstractModule)
orsetInjector(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 classCommonModuleMock
, 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
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 inapp.config.AppBootstrap
class, then you an option of usingAppIntegrationSpec
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
Allows access to session in test context.- Returns:
- object allowing access to session in test context.
-
flash
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
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 isnull
.
-
flash
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
Convenience method, sets an object on a session. Equivalent of:session().put(name, value)
- Parameters:
name
- name of objectvalue
- object itself.
-
sessionObject
Convenience method, returns object from session, equivalent of:session().get(name)
- Parameters:
name
- name of object,- Returns:
- session object.
-
sessionString
Convenience method, returns object from session, equivalent of:String val = (String)session().get(name)
- Parameters:
name
- name of object- Returns:
- value
-
sessionInteger
Convenience method, returns object from session, equivalent of:Integer val = (Integer)session().get(name)
- Parameters:
name
- name of object- Returns:
- value
-
session
Returns object from session that is already cast to expected type.- Parameters:
name
- name of object in sessiontype
- expected type.- Returns:
- object from session that is already cast to expected type.
-
sessionBoolean
Convenience method, returns object from session, equivalent of:Boolean val = (Boolean)session().get(name)
- Parameters:
name
- name of object- Returns:
- value
-
sessionDouble
Convenience method, returns object from session, equivalent of:Double val = (Double)session().get(name)
- Parameters:
name
- name of object- Returns:
- value
-
sessionFloat
Convenience method, returns object from session, equivalent of:Float val = (Float)session().get(name)
- Parameters:
name
- name of object- Returns:
- value
-
sessionLong
Convenience method, returns object from session, equivalent of:Long val = (Long)session().get(name)
- Parameters:
name
- name of object- Returns:
- value
-
sessionHas
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.
-