Class ControllerSpec

All Implemented Interfaces:
JSpecSupport
Direct Known Subclasses:
DBControllerSpec

public class ControllerSpec extends RequestSpecHelper
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
  • Constructor Details

    • ControllerSpec

      public ControllerSpec()
  • Method Details

    • atStart

      @BeforeEach public void atStart()
      Overrides:
      atStart in class SpecHelper
    • request

      protected RequestBuilder 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

      protected final String getControllerPath()
      Returns a controller path - this includes packages if there are any after "app.controllers".
      Returns:
      controller path
    • getControllerClassName

      protected final String getControllerClassName()