[announce] Tools to generate custom assertions

145 views
Skip to first unread message

Joel Costigliola

unread,
Oct 16, 2012, 11:36:08 AM10/16/12
to easyt...@googlegroups.com
Hi Guys,

It was possible to make your own assertions but it was not that straightforward so we decided to provide tools to generate Fest assertions.
Use case : Let's say that you have a Player class with name and team attributes, the generator is able to create a PlayerAssert assertions class with hasName and hasTeam assertions, to write code like :

  assertThat(mvp).hasName("Lebron James").hasTeam("Miami Heat");

Note that the tools is able to generate all the assertions for classes in a specified package.

There are several ways to launch the assertions Generator :
- a Maven plugin
- a command line tool (unix shell or windows .bat)
- soon available an eclipse plugin (in few weeks - stay tuned)

The Maven plugin is the easiest way to generate assertions for the time being, so I'm gonna describe how to use it (it only lacks the possibility to change the assertions template ... for the time being).

To use the maven plugin, you need to :
- add FEST Assertions as a dependency in pom.xml e.g (version 2.x is required)
- specify the packages containing the classes you want to generate custom assertions for (typically set the the package of Player class if you want Player assertions)
- optionally, set the directory where assertions files will be generated (default to target/generated-test-sources/assertions)
- call mvn generate-test-sources or simply mvn test

Fest assert dependency :

<dependency>
    <groupId>org.easytesting</groupId>
    <artifactId>fest-assert-core</artifactId>
    <version>2.0M8</version>
    <scope>test</scope>
</dependency>

To generate custom assertions, add the following plugin to your pom.xml build/plugins section :

<plugin>
    <groupId>org.easytesting</groupId>
    <artifactId>maven-fest-assertion-generator-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate-assertions</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <packages>
            <param>your.first.package</param>
            <param>your.second.package</param>
            ...
        </packages>
    </configuration>
</plugin>

packages configuration element is required in order to generate assertions for classes located in the specified packages.

You can also specify non-standard destination directory for assertion files using targetDir configuration element e.g.

<configuration>
    <packages>
        <param>your.first.package</param>
        <param>your.second.package</param>
        ...
    </packages>
    <targetDir>YOUR_NON_STANDARD_DIR</targetDir>
</configuration>


Have good assertions and give us some feedback to improve the tools !
Cheers,

Joel

Øyvind A.

unread,
Oct 15, 2013, 7:32:10 AM10/15/13
to easyt...@googlegroups.com
This is great! Love the simplicity of the eclipse plugin. It keeps me in the flow:)

One issue thoug..
The generator generates has-methods, but I get a NullPointerException if I want to check that something is null.

Case: person.setName(null);

assertThat(person).hasName(null); //This throws a NPE

because of getName() returns null:

        if (!actual.getName().equals(name)) { //actual.getName() returns null
            throw new AssertionError(errorMessage);
        }

What do you think about having the generator also generate methods that check for null-values? Alternatively generates a null-safe has-method?
what would be a suitable name of such a method?
assertThat(person).hasNameNull()
assertThat(person).nameIsNull();


Kind regards,
Øyvind

Joel Costigliola

unread,
Oct 15, 2013, 7:59:40 AM10/15/13
to easyt...@googlegroups.com
Hey Øyvind,

Some (good or bad ?) news for you :
  • I don't maintain fest assertion generator anymore as I have forked fest to create assertj and assertj assertion generator
  • The problem is fixed in assertj assertion generator 1.1.0 which has been released a month ago :)
  • There is no eclipse plugin yet for assertj :(  - I had a weird eclipse bug when migrating to assertj that I haven't managed to fix yet - but the maven plugin works fine.
  • There will be soon an Idea plugin.

I should try to give another shot at fixing the eclipse plugin, I hope to have it fixed by the end of the year (any help would be welcome).

Kind regards,

Joel Costigliola




--
You received this message because you are subscribed to the Google Groups "easytesting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to easytesting...@googlegroups.com.

To post to this group, send email to easyt...@googlegroups.com.
Visit this group at http://groups.google.com/group/easytesting.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages