GithubHelp home page GithubHelp logo

danhagberg / pojot Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 150 KB

Unit test helper to automatically test basic POJO classes. Helps provide code coverage for trivial methods.

License: Apache License 2.0

Java 100.00%

pojot's People

Contributors

danhagberg avatar fossabot avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

fossabot

pojot's Issues

Provide additional helper methods to define equals/hashcode testing.

Currently, equals/hashcode testing assumes that if no fields are provided, then the default methods are used and if fields are provided, then an equals/hashcode method has been provided and uses those fields.

Due to how some IDEs generate an equals/hashcode to be all fields, it is cumbersome to have to list all fields and to then update that list if the class changes.
Two new methods should be provided that perform the following:

  • Indicate that all fields should be included.
  • Allow exclusion of list of fields. This would assume that all fields, with the exception of those listed would be included.

Boolean fields not behaving as expected in equals and hash tests

Seeing the exception below for a simple Pojo with a 1 String and 1 Boolean field.
java.lang.AssertionError: [Equals method test failed for TenantOptin. Non-equal values in value did not cause inequality for instances.] expected [true] but found [false]

The class itself is simple:

public class TenantOptin  {

  private String name = null;
  private Boolean value = null;

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  public Boolean getValue() {
    public Boolean getValue() {
    return value;
  }
  public void setValue(Boolean value) {
    this.value = value;
  }

 @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    TenantOptin tenantOptin = (TenantOptin) o;
    return Objects.equals(name, tenantOptin.name) &&
        Objects.equals(value, tenantOptin.value);
  }
 @Override
  public int hashCode() {
    return Objects.hash(name, value);
  }
}


Enhancement: Add ability to include properties of abstract super classes.

Currently, the properties are tested only if declared by the class under test. Superclass properties are not included as they would be included in a separate test. With an abstract class, this is not possible as you cannot create an instance of the class.

Enhance property discovery to allow inclusion of the abstract classes properties and methods, if enabled to do so. The discovery should default to false and be enabled via a setting on the TestAid object.

Add ability to test with Collections classes as properties

Currently, collections classes are normally defined as interfaces and the ability to test requires an empty constructor be available. As these are interfaces, they cannot be instantiated.

Add the ability to recognize these types and return a Collections.empty[Type]

Testing a Pojo with enum fields results in a ClassNotFoundException

I have a simple Pojo that includes a field that is an enumeration. Running TestAid with this class results in a NoSuchMethodException.

Here is the sample class and a TestNG driver

import static org.testng.Assert.assertTrue;

import java.util.List;
import java.util.Objects;

import org.testng.annotations.Test;

import net.digitaltsunami.pojot.TestAid;

public class PojotTest {

    public enum TestEnum {
        ENUM1(), ENUM2()
    }

    public class TestEnumConsumer {

        private String name = null;
        private TestEnum enumField = null;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public TestEnum getEnumField() {
            return enumField;
        }

        public void setEnumField(TestEnum enumField) {
            this.enumField = enumField;
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, enumField);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            TestEnumConsumer testEnumConsumer = (TestEnumConsumer) o;
            return Objects.equals(name, testEnumConsumer.name) && Objects.equals(enumField, testEnumConsumer.enumField);
        }

    }

    @Test
    public void testEnum() throws Exception {
        List<String> errors = new TestAid<TestEnumConsumer>(TestEnumConsumer.class)
                .includeInEquals( "name", "enumField")
                .validate();
        assertTrue(errors.isEmpty(), errors.toString());

    }

}

The full stack of the test failure is:

net.digitaltsunami.pojot.TestAidException: Error during Getter testing
    at net.digitaltsunami.pojot.GetterTestRunner.runTests(GetterTestRunner.java:48)
    at net.digitaltsunami.pojot.TestAid.runGetterTests(TestAid.java:241)
    at net.digitaltsunami.pojot.TestAid.validate(TestAid.java:223)
    at com.concur.imaging.shared.beans.domain.PojotTest.testEnum(PojotTest.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
    at org.testng.SuiteRunner.run(SuiteRunner.java:254)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:207)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:178)
Caused by: java.lang.InstantiationException: com.concur.imaging.shared.beans.domain.PojotTest$TestEnumConsumer
    at java.lang.Class.newInstance(Class.java:427)
    at net.digitaltsunami.pojot.GetterTestRunner.runTests(GetterTestRunner.java:44)
    ... 27 more
Caused by: java.lang.NoSuchMethodException: com.concur.imaging.shared.beans.domain.PojotTest$TestEnumConsumer.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 28 more

Setters that return a value not tested

The bean introspection does not return setter methods if the setter method returns a value. This can be used to add a fluent style to setting the object properties.

Need to either document that these setters will not be tested or use another method for getting the setters.

Getter and Setter detection includes non property methods

Test detection is currently including gutter and setter methods due to them starting with the string "get" or "set". This is true even if there is no property directly tied to the methods. Pojot only tests property setters and getters and should exclude methods that are not tied to a property in the class under test.

Example:

public class SetterGetterForNonPropClass {
    private String testString1;

    public String getTestString1() {
        return testString1;
    }

    public void setTestString1(String testString1) {
        this.testString1 = testString1;
    }

    public void setNonProp(String value) {
        testString1 = value;
    }

This will result in the error:
net.digitaltsunami.pojot.TestAidException: Exception during setter test: net.digitaltsunami.pojot.testsubject.SetterGetterForNonPropClass.setNonProp
...
Caused by: java.lang.NoSuchFieldException: nonProp
at java.lang.Class.getDeclaredField(Class.java:2057)
at net.digitaltsunami.pojot.SetterTestRunner.testSetter(SetterTestRunner.java:77)

Modify tests to use different values for each property test

Current testing will not fail if a two getters/setters return the same property if both of are the same type or can be cast to the same type (e.g., int to long).

To remedy this, a different value for each property should be used for all properties. This is needed only for the getValue() method as the getLarge and getSmall methods may use max and min values.

Enhance equals/hash code test to verify non-specified fields have no effect

Currently, the equals and hash code testing verifies that the specified fields affect the equality or resulting hash code value of the class. But right now, if another field that was not specified affected the calculation, this would not be caught.

The testing should iterate over all non-specified fields and verify that changing the value has no effect on the values returned by the equals() and hashCode() methods.

Add ability to test with inner classes and enums

Currently, inner classes and enums cannot be tested as the class cannot be found. This is due to the the name being returned as EnclosingClass.InnerClass, but the class name is EnclosingClass$InnerClass. To allow classes with these inner classes to be tested, add the ability to load these inner classes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.