assertj-assertions-generator icon indicating copy to clipboard operation
assertj-assertions-generator copied to clipboard

For boolean properties, support "hasPropertyName(boolean expectedValue)"

Open dancerjohn opened this issue 7 years ago • 1 comments

Currently if a class has a boolean field / property (say hideLegend, the assertion generator produces two methods: isHideLegend() and isNotHideLegend. It would be really helpful to have an additional method hasHideLegend(boolean expectedValue). To keep with the BooleanAssert pattern, this might become hasHideLegendEqualTo(boolean) and hasHideLegendNotEqualTo(boolean). This is demonstrated by the example below:

private void testHelperMethod(MyAssertionClass instanceUnderTest, SomeInput input){
    boolean expectedHasLegend = // some code that uses input to determine expected legend;
    MyAssertionClassAssert assert = Assertions.assertThat(instanceUnderTest);

    if (expectedHadLengeng)
       assert.isHideLegend();
    else 
       assert.isNotHideLegend();
}

The above could become:

private void testHelperMethod(MyAssertionClass instanceUnderTest, SomeInput input){
    boolean expectedHasLegend = // some code that uses input to determine expected legend;
    Assertions.assertThat(instanceUnderTest)
      hasHideLegend(expectedHasLegend );
}

dancerjohn avatar May 17 '18 16:05 dancerjohn

This possible by changing the templates used to generate the assertions, see http://joel-costigliola.github.io/assertj/assertj-assertions-generator-maven-plugin.html#custom-templates.

In your case you would need to change these ones:

  <booleanAssertion>my_boolean_assert_template.txt</booleanAssertion>
  <booleanWrapperAssertion>my_Boolean_wrapper_assert_template.txt</booleanWrapperAssertion>

The easiest way to define your templates is to start from the existing ones available here: https://github.com/joel-costigliola/assertj-assertions-generator/tree/master/src/main/resources/templates

joel-costigliola avatar May 20 '18 10:05 joel-costigliola