Package org.javalite.activeweb
Class ControllerSpec
java.lang.Object
org.javalite.activeweb.SpecHelper
org.javalite.activeweb.RequestSpecHelper
org.javalite.activeweb.ControllerSpec
- All Implemented Interfaces:
JSpecSupport
- Direct Known Subclasses:
DBControllerSpec
Super class for controller tests. This class is used by unit tests that test a single controller. Controllers are
tested by simulating a web request to a controller (no physical network is involved, and no container initialized).
Subclasses must follow a simple naming convention: subclass name must be
made of two words: controller short class name and word "Spec". Example, of there is a controller:
public class GreeterController extends AppController{ ... }then the test will look like this:
public class GreeterControllerSpec extends ControllerSpec{ ... }ActiveWeb controller specs allow for true TDD, since they do not have a compiler dependency on controllers. You can describe full behavior of your controller before a controller class even exists. Simplest example:
public GreeterControllerSpec extends ControllerSpec{ @Test public void shouldRespondWithGreetingMessage(){ request().get("index"); a(responseCode()).shouldBeEqual(200); a(assigns().get("message")).shouldBeEqual("Hello, earthlings!"); } }In a code snippet above, a request with HTTP GET method is simulated to the GreeterController, index() action. Controller is expected to assign an object called "message" with value "Hello, earthlings!" to a view. This class will not open a connection to a test DB. If you need a connection, use
DBControllerSpec
.- Author:
- Igor Polevoy
-
Nested Class Summary
Nested classes/interfaces inherited from class org.javalite.activeweb.SpecHelper
SpecHelper.DynamicBuilder, SpecHelper.ModuleBuilder
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
atStart()
protected String
protected String
Returns a controller path - this includes packages if there are any after "app.controllers".protected RequestBuilder
request()
Use this DSL-ish method to send requests to controllers from specs.Methods inherited from class org.javalite.activeweb.RequestSpecHelper
assigns, atStart00, bytesContent, contentType, cookie, cookieValue, count, getCookies, header, headerNames, headers, layout, redirected, redirectValue, responseContent, statusCode, template, text, val, val, valBoolean, valDouble, valFloat, valInteger, valLong, vals, valString
Methods inherited from class org.javalite.activeweb.SpecHelper
afterEnd, createInjector, flash, flash, flashExists, injector, registerTag, session, session, session, sessionBoolean, sessionDouble, sessionFloat, sessionHas, sessionInteger, sessionLong, sessionObject, sessionString, setInjector, setTemplateLocation
-
Constructor Details
-
ControllerSpec
public ControllerSpec()
-
-
Method Details
-
atStart
@BeforeEach public void atStart()- Overrides:
atStart
in classSpecHelper
-
request
Use this DSL-ish method to send requests to controllers from specs. Attention: this method always returns a new object, please string methods one after another - fluent interfaces approach.- Returns:
- instance of
RequestBuilder
with convenience methods.
-
getControllerPath
Returns a controller path - this includes packages if there are any after "app.controllers".- Returns:
- controller path
-
getControllerClassName
-