Coder Social home page Coder Social logo

testutils's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

finaldoom

testutils's Issues

Non "bean like" constructors support

Hi,

I don't know if I just didn't find how to do it, but I can't test a bean that has a constructor where the arguments are not also properties.

Example:

public class MyBeanClass
{
    private String a, b, c;
    public MyBeanClass(AnotherClass other)
    {
        this.a = other.getA();
        this.b = other.getB();
        this.c = other.getC();
    }
}

There should be some way to tell the tester to just ignore that constructor, or is there?

Example on http://outsidemybox.github.io/testUtils/ is just wrong

On http://outsidemybox.github.io/testUtils/ there is an example with a class MyBean, it has 3 properties, of those 2 are used in the equals method, and all three are used in the hashcode method.
This should be the other way around, see javadoc : http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()
"If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result."
Writing such examples can prove a bad influence on inexperienced programmers.

BeanLikeTester doesn't work with properties that are initialized with an array

Consider the following class:

public class MyClass {
  private String[] foo = new String[] { "a", "b" };
  public String[] getFoo() {
    return foo;
  }
  public void setFoo(String[] foo) {
    this.foo = foo;
  }
}

I cannot testDefaultValues because it uses equals(), even for array-type members:

private static boolean areValuesDifferent(Object value1, Object value2) {
  final boolean conditionToFail1 = (value1 != null) && ((value2 == null) || !value1.equals(value2));
  final boolean conditionToFail2 = (value2 != null) && ((value1 == null) || !value2.equals(value1));
  return conditionToFail1 || conditionToFail2;
}

I propose to have a special case in areValuesDifferent for array-type values and use Arrays.equals().

testEqualsAndHash does not follow standard hashCode / equals contract

I have been using your bean tester but have been unable to ever use the "testEqualsAndHash" functionality because the rules it enforces are just wrong.

The rules between hashcode and equals are:

  1. If two objects are equal, then they must have the same hash code.
  2. If two objects have the same hash code, they may or may not be equal.

Here is a simple POJO bean like class I want to test:

`
public class CatalogueCacheEntry {
private Integer catalogueId;
private Integer contextId;

public Integer getCatalogueId() {
	return catalogueId;
}

public void setCatalogueId(Integer catalogueId) {
	this.catalogueId = catalogueId;
}

public Integer getContextId() {
	return contextId;
}

public void setContextId(Integer contextId) {
	this.contextId = contextId;
}

@Override
public boolean equals(Object o) {
	if (this == o) {
		return true;
	}

	if (!(o instanceof CatalogueCacheEntry)) {
		return false;
	}

	final CatalogueCacheEntry that = (CatalogueCacheEntry) o;
	final EqualsBuilder builder = new EqualsBuilder();

	builder.append(catalogueId, that.catalogueId);

	return builder.isEquals();
}

@Override
public int hashCode() {
	return new HashCodeBuilder().append(catalogueId).toHashCode();
}

}
`

If the catalogueIds are equal then the objects are equal as far as I care - I do not want to check the second field. As such the equals method only checks the catalogueId and as you can see the hashCode also only considers the catalogueId so the rules above are met.

If I run your "testEqualsAndHash" (from v0.1.3) I get the following result:

org.outsideMyBox.testUtils.BeanLikeTesterException: The hashcodes of different objects should be different for the tests, please change the values or check that hashcode() is correct object1:CatalogueCacheEntry[catalogueId=<null>,contextId=<null>] hashcode:629 object2:CatalogueCacheEntry[catalogueId=<null>,contextId=2222] hashcode:629 at org.outsideMyBox.testUtils.BeanLikeTester.testEqualsAndHash(BeanLikeTester.java:554) ...

This is wrong in two ways:

  1. The objects ARE equal, if you did obj1.equals(obj2) it would be true. So saying they are "different objects" is incorrect.
  2. Different object CAN have the same hashcode so even if they WERE different (which they aren't) it would still be valid!

It appears that you are forcing every field of a bean to be compared in the equals and hashcode and that is often not desireable.

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.