Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find out the most user friendly pojo tester api for testing #68

Closed
sta-szek opened this issue Sep 1, 2016 · 6 comments
Closed

Find out the most user friendly pojo tester api for testing #68

sta-szek opened this issue Sep 1, 2016 · 6 comments

Comments

@sta-szek
Copy link
Owner

sta-szek commented Sep 1, 2016

What is the most user friendly / given-when-then convention compatible way to invoke pojo tests?

// given
PojoTester tester = new PojoTester();
PojoTester tester = new PojoTester(fieldValueChanger);
PojoTester tester = new PojoTester().using(fieldValueChanger);

Class coffee = Coffee.class;
Class cake = Cake.class;

Class[] classes = new Class[]{coffee, cake};

// when

// then
tester.test(coffee);
tester.testAll(coffee, cake,);
tester.testAll(classes);

OR


// given
Class coffee = Coffee.class;
Class cake = Cake.class;

Class[] classes = new Class[]{coffee, cake};

// when

// then
assertThat(class).using(fieldValueChanger).hasWellImplemented...();
assertThat(class1, class2).using(fieldValueChanger).haveWellImplemented...();

EXPLANATION

Currently test and testAll methods throw exception when pojo has invalid equals, hashcode or toString method implementation.

NESTED CLASSES

You can also define nested classes to change in your main class e.g.

A {
  B b;
  C c.
}
Class baseClass = A.class;
Class[] nestedClasses = new Class[]{B.class, C.class};

test(baseClass, nestedClasses )

KIND OF TESTS

There is no option to choose which tests should be run (equals, hashcode or toString).
How do you want to provide it? E.g.

new PojoTester(EQUALS | HASHCODE | TO_STRING)
or
new PojoTester().addTesters(new EqualsTester(), ...)
or
assertThat(class).using(new HashCodeTester()).hasWellImplemented...();

CHANGING FIELDS

At this moment you can provide you own fieldsValuesChanger that should change field of specific type e.g. Coffee.
In order to do this you have to extend AbstractFieldsValuesChanger class and implement some methods.

If you do not specify your own fields values changer and pojo tester could not change field value then it sets it either to null or class.instantiate()

@jerckov
Copy link

jerckov commented Sep 2, 2016

Maybe something like this will be cool:

@Test
public void shouldCheckPojo() {
   // given
   Class cofe = Cofe.class;

   // when
   final PojoResult result = testPojo(cofe);

   // then
   assertPojo(result).hasWellImplemented();
 }

Although it's splitted to two lines but BDD structured is preserved.

@m-dabrowa
Copy link

I like the second approach

assertThat(class).using(fieldValueChanger).hasWellImplemented...();

But @jerckov has right. You wrote about given-when-then and you had empty section when. He introduced PojoResult and this seems a little strange to me. Maybe BDD is not applicable for PojoTester.

If you want to chose than the second proposition is better.

@losoft
Copy link

losoft commented Sep 5, 2016

I think the second option is more natural for me.
And something similar to:

// given
final EqualsTester equalsTester = new CustomEqualsTester();

// when
final Configuration configuration = new DefaultConfiguration().withCustom(equalsTester)
                                                              .skip(Configuration.ToStringTester)

// then
assertThat(class).using(configuration).hasWellImplemented...();

@sta-szek
Copy link
Owner Author

sta-szek commented Sep 6, 2016

Gitter

@SemionPar
Copy link
Contributor

assertThat(class) convention is surely the most appealing one.

Have you considered annotation based approach? You could annotate the pojos you want to test with @PojoTester (options = ), then use custom test runner to run tests for all pojos in a single test class, using @Test methods to provide sets of configuration. If you are dealing with primarily dumb pojos this could spare you some time writing and maintaining all those test classes.

@sta-szek
Copy link
Owner Author

sta-szek commented Sep 7, 2016

assertThat(...)... almost complete, just resolve some problems with IllegalAccessException.
Annotations sounds delicious - but this makes your production code test framework dependend.
This means you have to have fat-jar pojo-tester in production jar... 😞
But for people who do not care about that it could be a nice feature, so you can report feature issue 😄

BTW
Solution for this problem is to write gradle plugin, that will delete all pojo-tester annotations from your source code before compiling.

sta-szek pushed a commit that referenced this issue Sep 8, 2016
sta-szek pushed a commit that referenced this issue Sep 8, 2016
sta-szek pushed a commit that referenced this issue Sep 8, 2016
sta-szek pushed a commit that referenced this issue Sep 8, 2016
sta-szek pushed a commit that referenced this issue Sep 9, 2016
sta-szek pushed a commit that referenced this issue Sep 9, 2016
sta-szek pushed a commit that referenced this issue Sep 9, 2016
sta-szek pushed a commit that referenced this issue Sep 11, 2016
…dly-pojo-tester-api-for-testing

#68 - API implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants