User Guide

Install

Go to File | Settings | Plugins | Browse Repositories... Enter Squaretest in the search bar at the top Select the Squaretest plugin, then click the Install button on the right.

Getting Started

Open a file that you want to create unit tests for Do one of the following

Press ctrl+alt+k on Windows/Linux or cmd+shift+L on OS X.

on Windows/Linux or on OS X. Choose Squaretest | Generate Test from the toolbar Follow the instructions on the Configure Squaretest Module Settings dialog.

The options are as follows. Test Language the language to create your tests in (either Java or Groovy)

the language to create your tests in (either Java or Groovy) Template the Apache Velocity template used to create your tests; the templates are named according to the test-runner and mocking framework they use.

the Apache Velocity template used to create your tests; the templates are named according to the test-runner and mocking framework they use. Test Sources Root the directory in which Squaretest will create your tests (and any package directories required) for this module.

the directory in which Squaretest will create your tests (and any package directories required) for this module. Promote Test Language and Template to Project Settings whether or not Squaretest will save the above Test Language and Template in the Project Settings or the Module Settings; the default setting works best for most users.

whether or not Squaretest will save the above and in the Project Settings or the Module Settings; the default setting works best for most users. Module Configuration whether or not Squaretest should try to configure the settings for your other modules automatically when they are needed or show this dialog every time; the default setting works best for most users. See Module Configuration for more details. Clicking OK will save these settings and create your test class. You can modify these settings later in the Squaretest Settings Menu described in the next section.

Settings

Go to File | Settings Mac OS X Users: IntelliJ IDEA | Preferences Click the Squaretest option shown near the bottom of the list on the left.

Application Settings

Open the Test File specifies which editor window Squaretest should open the test class in; options are: In the active editor window In the next editor window if one is available

Squaretest will open the test class in the editor window next to the active editor window if one is available; otherwise, it will open the test class in the active editor window. In the next editor window, creating one if needed

Squaretest will open the test class in the editor window next to the active editor window if one is available; otherwise, it will create a new editor window by splitting the active editor window vertically and open the test class in the new window.

specifies which editor window Squaretest should open the test class in; options are: Template Manager this shows a dialog allowing you to delete or rename any templates you've created.

Project Settings

Test Language the language to create your tests in (either Java or Groovy)

the language to create your tests in (either Java or Groovy) Template the Apache Velocity template used to create your tests.

the Apache Velocity template used to create your tests. Module Configuration

Automatically configure when possible

Squaretest will automatically configure the Module Settings the first time they are needed if the Project Settings contain a template and one of the following is true. The module contains exactly one test-sources root, and the test-sources root contains source files matching the template-language (Java or Groovy) in the Project Settings (the most common scenario). The module contains exactly one Groovy test-sources root and the Project Settings contain a Groovy-template. The module contains no test-sources roots and its dependent modules collectively contains exactly one test-sources root and that test-sources root contains source files matching the template-language in the Project Settings. Always ask to configure module settings

Squaretest will always prompt the user to configure settings the first time a test is generated for a module.



Module Settings

Test Language the language to create your tests in (inherited from the Project Settings by default)

the language to create your tests in (inherited from the Project Settings by default) Template the template used to create your tests (inherited from the Project Settings by default)

the template used to create your tests (inherited from the Project Settings by default) Test Sources Root the directory Squaretest will create your tests (and any package directories required) for this module

Keyboard Shortcut

Go to File | Settings | Keymap Expand the Plugins folder and then the Squaretest folder Double-click the Generate Test item and select Add Keyboard Shortcut. Set the desired shortcut, then click OK, then OK again.

Create Your Own Template

Select a template in either the Project Settings or the Module Settings. Make changes to the template code shown in the editor (see the image below)

Enter a name for your template in the textbox next to the Save Template As button. Click Save Template As, then OK

Delete or Rename Templates

Template Best Practices

1. Quick Settings

2. Test Class Rendering Code

3. Helper Macros

renderTestMethods($methods)

4. InitializeTemplateDataModel Macro

Formatting Tips

Don't worry about organizing the import-statements These will be organized according to your IDE code style settings in the code-cleanup.

Use the fully qualified, canonical text when declaring members and variables; e.g. Variable.type.canonicalText. These will be shortened automatically in the code-cleanup. Using fully qualified, canonical text simplifies declaring variables with generic types.

Don't worry about having extra newlines (more than 1 empty-line) between sections of the test class; those are removed in the code-cleanup.

Don't worry about over-indenting the Java code or Groovy code in your templates The test class will be formatted according to your IDE code style settings in the code-cleanup.



Advanced Template Editing

IntelliJ IDEA Ultimate

Other Editors

Windows <SYSTEM DRIVE>\Users\<USER ACCOUNT NAME>\.<PRODUCT><VERSION>\config\fileTemplates Mac OS X ~/Library/Preferences/.<PRODUCT><VERSION>/fileTemplates Linux ~/.<PRODUCT><VERSION>/fileTemplates

You can install the Squaretest plugin from the IntelliJ plugin repository by following the instructions below.You can try Squaretest for free for 30 days.The best way to get started with Squaretest is to use it to create a test by following the instructions below.The Squaretest settings can be changed by doing the following.The application, project and module-level settings are as follows.These settings apply to the Squaretest application; i.e. all projects. The options are as follows.The Project Settings apply to an IntelliJ Project. They are as follows.The Module Settings apply to a specific module. They are as follows.The Squaretest keyboard shortcut can be changed by doing the following.You can create your own template to use to generate tests by doing the following.You can delete or rename the templates you've created by clickingin the Application Settings The Velocity templates included with Squaretest are organized into 4 parts. Most developers will only need to modify the first 2 parts to update the template to match their code style and conventions. The parts are described below in the order in which they appear in the template code.The first section of the template contains Quick Settings, or variables that configure common code style settings and other aspects of the test class that are applied throughout the template code. See Quick Settings for details about each setting.This is the code that renders the test class, including the package declaration, import statements and class definition. You can modify this code to match the conventions used in your tests; e.g. add @RunWith annotations, make the test class extend a common base class in your project or module, add comments between the mock variable declarations and the non-mock variable declarations, etc. See Template Data Model for the list of variables available to the test class rendering code.This contains macros used to render parts of the test class; e.g.. These macros are invoked in the Test Class Rendering Code and can be modified to change how parts of the test class are created.The initializeTemplateDataModel macro configures the data model based on the archetype of the source class and the Quick Settings. This macro is called immediately after the Quick Settings section and sets global variables describing how the test class should be rendered. The global variables are set based on the source class's constructors, fields, methods, annotations and other factors. The variables set by the macro are described here and may be used in the Test Class Rendering Code and the Helper Macros Power users can modify this macro and its helper macros (defined below the macro) to add or change variables in the data model to accommodate their use cases.Users should follow the convention of using the initializeTemplateDataModel macro to add or modify variables in the data model, then update the Test Class Rendering Code to check for the new variables and change the output accordingly. This keeps the logic to configure the data model separate from the test class rendering code and allows both to grow in complexity without becoming unmaintainable.Squaretest is designed to allow users to make changes to the template without worrying about formatting. To accomplish this, Squaretest automatically performs several code-cleanup tasks on the generated test class, described here . In general, users should follow the guidelines below.Users making significant changes to the templates may wish to use a different editor. The following editors work well.The IntelliJ IDEA Ultimate IDE includes a good Velocity editor plugin and comes with a free trial. Squaretest also implements the velocity.globalVariableProvider extension point in the Velocity plugin to provide limited code-completion suggestions and javadocs for some of the template variables. To use the editor, simply use the Squaretest plugin in IntelliJ IDEA Ultimate. Squaretest will automatically use the editor provided by the Velocity plugin in the Settings menu and Configure Squaretest Module Settings dialog.Squaretest stores the user-created templates in the IntelliJ Template Manager, which stores them in files on the file system. You can edit these files using your editor of choice. Please. Also note that you will need to restart IntelliJ IDEA after changing the files. The template files are stored in the following locations.