public class SpecHelper extends Object implements JSpecSupport
Modifier and Type | Class and Description |
---|---|
protected class |
SpecHelper.DynamicBuilder |
class |
SpecHelper.ModuleBuilder |
Constructor and Description |
---|
SpecHelper() |
Modifier and Type | Method and Description |
---|---|
void |
afterEnd() |
void |
atStart() |
protected <T extends com.google.inject.AbstractModule> |
createInjector(T module)
This is a convenience method for setting Guice modules and service mocks.
|
protected Object |
flash(String name)
Returns a named flash value assigned to session by controller.
|
protected <T> T |
flash(String name,
Class<T> type)
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 SessionTestFacade |
session()
Allows access to session in test context.
|
protected <T> T |
session(String name,
Class<T> type)
Returns object from session that is already cast to expected type.
|
protected void |
session(String name,
Serializable value)
Convenience method, sets an object on a session.
|
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) |
@BeforeEach public void atStart()
@AfterEach public void afterEnd()
protected void setTemplateLocation(String location)
location
- this is a relative location starting from the module root, intended for testing.protected SpecHelper.DynamicBuilder injector()
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)
or setInjector(Injector)
methods.protected void setInjector(com.google.inject.Injector injector)
How to override some services for tests:
Injector injector = Guice.createInjector(Modules.override(new CommonModule()).with(new CommonModuleMock()); setInjector(injector);
injector
- injector to source dependencies form.protected <T extends com.google.inject.AbstractModule> SpecHelper.ModuleBuilder createInjector(T module)
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.
module
- - main module you want to set on a spec. This module may include services you need to override.protected void registerTag(String name, FreeMarkerTag tag)
app.config.AppBootstrap
class, then you an
option of using AppIntegrationSpec
as a super class.name
- tag name where name is a part of the tag on page like so: <@name...
.tag
- instance of tag to register.protected SessionTestFacade session()
protected Object flash(String name)
name
- name of flash value.protected Object flashExists(String name)
name
- name in questiontrue
even if flash by name exists,
but its value is null
.protected <T> T flash(String name, Class<T> type)
name
- name of flash value.type
- type to be returnedprotected void session(String name, Serializable value)
session().put(name, value)
name
- name of objectvalue
- object itself.protected Object sessionObject(String name)
session().get(name)
name
- name of object,protected String sessionString(String name)
String val = (String)session().get(name)
name
- name of objectprotected Integer sessionInteger(String name)
Integer val = (Integer)session().get(name)
name
- name of objectprotected <T> T session(String name, Class<T> type)
name
- name of object in sessiontype
- expected type.protected Boolean sessionBoolean(String name)
Boolean val = (Boolean)session().get(name)
name
- name of objectprotected Double sessionDouble(String name)
Double val = (Double)session().get(name)
name
- name of objectprotected Float sessionFloat(String name)
Float val = (Float)session().get(name)
name
- name of objectprotected Long sessionLong(String name)
Long val = (Long)session().get(name)
name
- name of objectprotected boolean sessionHas(String name)
name
- name of object.Copyright © 2020 JavaLite. All rights reserved.