outsidemybox / testutils Goto Github PK
View Code? Open in Web Editor NEWEasily and quickly test and improve the test coverage of 'bean like' objects.
Home Page: http://outsidemybox.github.com/testUtils/
License: Other
Easily and quickly test and improve the test coverage of 'bean like' objects.
Home Page: http://outsidemybox.github.com/testUtils/
License: Other
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?
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.
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()
.
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:
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:
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.