Fixed the way in JUnit we address list indices

without this change for some reason we're using .get() to address an entry in the list via a index
with this change we're doing it in the proper JSON path format which is `$[index]`

fixes #85
This commit is contained in:
Marcin Grzejszczak
2016-12-27 14:18:40 +01:00
parent b7beb322b5
commit 07bf0cb9c7
3 changed files with 122 additions and 2 deletions

View File

@@ -93,7 +93,7 @@ abstract class JUnitMethodBodyBuilder extends RequestProcessingMethodBodyBuilder
@Override
protected String getPropertyInListString(String property, Integer listIndex) {
return "${property}.get($listIndex)" ?: ''
return "${property}[$listIndex]" ?: ''
}
@Override

View File

@@ -774,12 +774,72 @@ class JaxRsClientMethodBuilderSpec extends Specification implements WireMockStub
MethodBodyBuilder builder = new JaxRsClientJUnitMethodBodyBuilder(contractDsl, properties)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.then(blockBuilder)
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('assertThatRejectionReasonIsNull(parsedJson.read("$.get("rejectionReason").title"));')
}
@Issue('#85')
def "should execute custom method for more complex structures on the response side when using Spock"() {
given:
Contract contractDsl = Contract.make {
request {
method 'GET'
urlPath '/get'
}
response {
status 200
body([
[
name: $(consumer("userName 1"), producer(execute('assertThatUserNameIsNotNull($it)')))
],
[
name: $(consumer("userName 2"), producer(execute('assertThatUserNameIsNotNull($it)')))
]
])
}
}
MethodBodyBuilder builder = new JaxRsClientJUnitMethodBodyBuilder(contractDsl, properties)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('''assertThatUserNameIsNotNull(parsedJson.read("$[0].name")''')
test.contains('''assertThatUserNameIsNotNull(parsedJson.read("$[1].name")''')
}
@Issue('#85')
def "should execute custom method for more complex structures on the response side when using JUnit"() {
given:
Contract contractDsl = Contract.make {
request {
method 'GET'
urlPath '/get'
}
response {
status 200
body([
[
name: $(consumer("userName 1"), producer(execute('assertThatUserNameIsNotNull($it)')))
],
[
name: $(consumer("userName 2"), producer(execute('assertThatUserNameIsNotNull($it)')))
]
])
}
}
MethodBodyBuilder builder = new JaxRsClientJUnitMethodBodyBuilder(contractDsl, properties)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.then(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('''assertThatUserNameIsNotNull(parsedJson.read("$[0].name")''')
test.contains('''assertThatUserNameIsNotNull(parsedJson.read("$[1].name")''')
}
@Issue('#150')
def "should support body matching in response"() {
given:

View File

@@ -1721,6 +1721,66 @@ World.'''"""
SyntaxChecker.tryToCompileGroovy(blockBuilder.toString())
}
@Issue('#85')
def "should execute custom method for more complex structures on the response side when using Spock"() {
given:
Contract contractDsl = Contract.make {
request {
method 'GET'
urlPath '/get'
}
response {
status 200
body([
[
name: $(consumer("userName 1"), producer(execute('assertThatUserNameIsNotNull($it)')))
],
[
name: $(consumer("userName 2"), producer(execute('assertThatUserNameIsNotNull($it)')))
]
])
}
}
MethodBodyBuilder builder = new MockMvcSpockMethodRequestProcessingBodyBuilder(contractDsl, properties)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.then(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('''assertThatUserNameIsNotNull(parsedJson.read('$[0].name')''')
test.contains('''assertThatUserNameIsNotNull(parsedJson.read('$[1].name')''')
}
@Issue('#85')
def "should execute custom method for more complex structures on the response side when using JUnit"() {
given:
Contract contractDsl = Contract.make {
request {
method 'GET'
urlPath '/get'
}
response {
status 200
body([
[
name: $(consumer("userName 1"), producer(execute('assertThatUserNameIsNotNull($it)')))
],
[
name: $(consumer("userName 2"), producer(execute('assertThatUserNameIsNotNull($it)')))
]
])
}
}
MethodBodyBuilder builder = new MockMvcJUnitMethodBodyBuilder(contractDsl, properties)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.then(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('''assertThatUserNameIsNotNull(parsedJson.read("$[0].name")''')
test.contains('''assertThatUserNameIsNotNull(parsedJson.read("$[1].name")''')
}
@Issue('#111')
def "should execute custom method for request headers"() {
given: