Skip to content

Commit

Permalink
Fix MissingFieldException in DiffedObjectAsBeanRenderer (fixes spockf…
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed Jul 8, 2018
1 parent 5b48fa8 commit f408b8d
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 3 deletions.
Expand Up @@ -26,8 +26,16 @@ public String render(Object object) {
for (Class<?> clazz = object.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
for (Field field : clazz.getDeclaredFields()) {
if (field.isSynthetic()) continue;
String value = GroovyRuntimeUtil.toString(GroovyRuntimeUtil.getAttribute(object, field.getName()));
builder.addLine(field.getName() + ": " + value);
boolean accessible = field.isAccessible();
try {
field.setAccessible(true);
String value = GroovyRuntimeUtil.toString(field.get(object));
builder.addLine(field.getName() + ": " + value);
} catch (IllegalAccessException e) {
builder.addLine(field.getName() + ": [threw an exception on access: "+e.getMessage()+"]");
} finally {
field.setAccessible(accessible);
}
}
}

Expand Down
Expand Up @@ -2,10 +2,10 @@ package org.spockframework.smoke.condition

import org.spockframework.EmbeddedSpecification
import org.spockframework.runtime.SpockComparisonFailure

import spock.lang.Issue

class DiffedObjectRendering extends EmbeddedSpecification {

@Issue("http://issues.spockframework.org/detail?id=170")
def "can handle null values"() {
when:
Expand All @@ -16,4 +16,165 @@ class DiffedObjectRendering extends EmbeddedSpecification {
failure.actual == "1\n"
failure.expected == "null\n"
}

@Issue("https://github.com/spockframework/spock/issues/659")
def 'it should render a bean properly'() {
when:
runner.runFeatureBody '''
setup:
def b1 = new org.spockframework.smoke.condition.DiffedObjectRendering.MyBean([integer: 1, string: "fun"])
def b2 = new org.spockframework.smoke.condition.DiffedObjectRendering.MyBean([integer: 2, string: "fun2"])
expect:
b1 == b2
'''

then:
SpockComparisonFailure failure = thrown()
failure.actual == "integer: 1\nstring: fun\n"
failure.expected == "integer: 2\nstring: fun2\n"
}

@Issue("https://github.com/spockframework/spock/issues/659")
def 'it should render a bean sub-class properly'() {
when:
runner.runFeatureBody '''
setup:
def b1 = new org.spockframework.smoke.condition.DiffedObjectRendering.MySubBean([integer: 1, string: "fun"])
def b2 = new org.spockframework.smoke.condition.DiffedObjectRendering.MySubBean([integer: 2, string: "fun2"])
expect:
b1 == b2
'''

then:
SpockComparisonFailure failure = thrown()
failure.actual == "integer: 1\nstring: fun\n"
failure.expected == "integer: 2\nstring: fun2\n"
}

@Issue("https://github.com/spockframework/spock/issues/659")
def 'it should render a bean with an interface properly'() {
when:
runner.runFeatureBody '''
setup:
def b1 = new org.spockframework.smoke.condition.DiffedObjectRendering.MyBean2([long: 1, string: "fun"])
def b2 = new org.spockframework.smoke.condition.DiffedObjectRendering.MyBean2([long: 2, string: "fun2"])
expect:
b1 == b2
'''

then:
SpockComparisonFailure failure = thrown()
failure.actual == "long1: 1\nstring: fun\n"
failure.expected == "long1: 2\nstring: fun2\n"
}

@Issue("https://github.com/spockframework/spock/issues/659")
def 'it should render a bean sub-class with an interface properly'() {
when:
runner.runFeatureBody '''
setup:
def b1 = new org.spockframework.smoke.condition.DiffedObjectRendering.MySubBean2([long: 1, string: "fun"])
def b2 = new org.spockframework.smoke.condition.DiffedObjectRendering.MySubBean2([long: 2, string: "fun2"])
expect:
b1 == b2
'''

then:
SpockComparisonFailure failure = thrown()
failure.actual == "long1: 1\nstring: fun\n"
failure.expected == "long1: 2\nstring: fun2\n"
}

interface RenderBean {
long getLong()
}

static class MyBean {
private int integer
private String string

int getInteger() {
return integer
}

void setInteger(int integer) {
this.integer = integer
}

String getString() {
return string
}

void setString(String string) {
this.string = string
}

@Override
boolean equals(Object o) {
if (this.is(o))
return true
if (o == null || getClass() != o.getClass())
return false
MyBean myBean = (MyBean)o
return integer == myBean.integer &&
Objects.equals(string, myBean.string)
}

@Override
int hashCode() {
return Objects.hash(integer, string)
}

}

static class MyBean2 implements RenderBean {
private long long1
private String string

long getLong() {
return long1
}

void setLong(long long1) {
this.long1 = long1
}

String getString() {
return string
}

void setString(String string) {
this.string = string
}

@Override
boolean equals(Object o) {
if (this.is(o))
return true
if (o == null || getClass() != o.getClass())
return false
MyBean2 myBean = (MyBean2)o
return long1 == myBean.long1 &&
Objects.equals(string, myBean.string)
}

@Override
int hashCode() {
return Objects.hash(long1, string)
}

}


static class MySubBean extends MyBean {
}

static class MySubBean2 extends MyBean2 {
}

}

0 comments on commit f408b8d

Please sign in to comment.