Finding Elements in Collections in Groovy: refactor to use spock testing framework
This commit is contained in:
parent
3512ca847f
commit
76fb5b02e0
@ -1,58 +1,57 @@
|
|||||||
package com.baeldung.find
|
package com.baeldung.find
|
||||||
|
|
||||||
import com.baeldung.find.Person
|
import spock.lang.Specification
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
import static org.junit.Assert.*
|
class ListFindUnitTest extends Specification {
|
||||||
|
|
||||||
class ListFindUnitTest {
|
final personList = [
|
||||||
|
new Person("Regina", "Fitzpatrick", 25),
|
||||||
private final personList = [
|
new Person("Abagail", "Ballard", 26),
|
||||||
new Person("Regina", "Fitzpatrick", 25),
|
new Person("Lucian", "Walter", 30),
|
||||||
new Person("Abagail", "Ballard", 26),
|
|
||||||
new Person("Lucian", "Walter", 30),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
@Test
|
def "whenListContainsElement_thenCheckReturnsTrue"() {
|
||||||
void whenListContainsElement_thenCheckReturnsTrue() {
|
given:
|
||||||
def list = ['a', 'b', 'c']
|
def list = ['a', 'b', 'c']
|
||||||
|
|
||||||
assertTrue(list.indexOf('a') > -1)
|
expect:
|
||||||
assertTrue(list.contains('a'))
|
list.indexOf('a') > -1
|
||||||
|
list.contains('a')
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "whenListContainsElement_thenCheckWithMembershipOperatorReturnsTrue"() {
|
||||||
void whenListContainsElement_thenCheckWithMembershipOperatorReturnsTrue() {
|
given:
|
||||||
def list = ['a', 'b', 'c']
|
def list = ['a', 'b', 'c']
|
||||||
|
|
||||||
assertTrue('a' in list)
|
expect:
|
||||||
|
'a' in list
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "givenListOfPerson_whenUsingStreamMatching_thenShouldEvaluateList"() {
|
||||||
void givenListOfPerson_whenUsingStreamMatching_thenShouldEvaluateList() {
|
expect:
|
||||||
assertTrue(personList.stream().anyMatch {it.age > 20})
|
personList.stream().anyMatch { it.age > 20 }
|
||||||
assertFalse(personList.stream().allMatch {it.age < 30})
|
!personList.stream().allMatch { it.age < 30 }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "givenListOfPerson_whenUsingCollectionMatching_thenShouldEvaluateList"() {
|
||||||
void givenListOfPerson_whenUsingCollectionMatching_thenShouldEvaluateList() {
|
expect:
|
||||||
assertTrue(personList.any {it.age > 20})
|
personList.any { it.age > 20 }
|
||||||
assertFalse(personList.every {it.age < 30})
|
!personList.every { it.age < 30 }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "givenListOfPerson_whenUsingStreamFind_thenShouldReturnMatchingElements"() {
|
||||||
void givenListOfPerson_whenUsingStreamFind_thenShouldReturnMatchingElements() {
|
expect:
|
||||||
assertTrue(personList.stream().filter {it.age > 20}.findAny().isPresent())
|
personList.stream().filter { it.age > 20 }.findAny().isPresent()
|
||||||
assertFalse(personList.stream().filter {it.age > 30}.findAny().isPresent())
|
!personList.stream().filter { it.age > 30 }.findAny().isPresent()
|
||||||
assertTrue(personList.stream().filter {it.age > 20}.findAll().size() == 3)
|
personList.stream().filter { it.age > 20 }.findAll().size() == 3
|
||||||
assertTrue(personList.stream().filter {it.age > 30}.findAll().isEmpty())
|
personList.stream().filter { it.age > 30 }.findAll().isEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "givenListOfPerson_whenUsingCollectionFind_thenShouldReturnMatchingElements"() {
|
||||||
void givenListOfPerson_whenUsingCollectionFind_thenShouldReturnMatchingElements() {
|
expect:
|
||||||
assertNotNull(personList.find {it.age > 20})
|
personList.find { it.age > 20 } == new Person("Regina", "Fitzpatrick", 25)
|
||||||
assertNull(personList.find {it.age > 30})
|
personList.find { it.age > 30 } == null
|
||||||
assertTrue(personList.findAll {it.age > 20}.size() == 3)
|
personList.findAll { it.age > 20 }.size() == 3
|
||||||
assertTrue(personList.findAll {it.age > 30}.isEmpty())
|
personList.findAll { it.age > 30 }.isEmpty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,76 +1,81 @@
|
|||||||
package com.baeldung.find
|
package com.baeldung.find
|
||||||
|
|
||||||
import com.baeldung.find.Person
|
import spock.lang.Specification
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
import static org.junit.Assert.*
|
class MapFindUnitTest extends Specification {
|
||||||
|
|
||||||
class MapFindUnitTest {
|
final personMap = [
|
||||||
|
Regina: new Person("Regina", "Fitzpatrick", 25),
|
||||||
private final personMap = [
|
Abagail: new Person("Abagail", "Ballard", 26),
|
||||||
Regina : new Person("Regina", "Fitzpatrick", 25),
|
Lucian: new Person("Lucian", "Walter", 30)
|
||||||
Abagail: new Person("Abagail", "Ballard", 26),
|
|
||||||
Lucian : new Person("Lucian", "Walter", 30)
|
|
||||||
]
|
]
|
||||||
|
|
||||||
@Test
|
def "whenMapContainsKeyElement_thenCheckReturnsTrue"() {
|
||||||
void whenMapContainsKeyElement_thenCheckReturnsTrue() {
|
given:
|
||||||
def map = [a: 'd', b: 'e', c: 'f']
|
def map = [a: 'd', b: 'e', c: 'f']
|
||||||
|
|
||||||
assertTrue(map.containsKey('a'))
|
expect:
|
||||||
assertFalse(map.containsKey('e'))
|
map.containsKey('a')
|
||||||
assertTrue(map.containsValue('e'))
|
!map.containsKey('e')
|
||||||
|
map.containsValue('e')
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "whenMapContainsKeyElement_thenCheckByMembershipReturnsTrue"() {
|
||||||
void whenMapContainsKeyElement_thenCheckByMembershipReturnsTrue() {
|
given:
|
||||||
def map = [a: 'd', b: 'e', c: 'f']
|
def map = [a: 'd', b: 'e', c: 'f']
|
||||||
|
|
||||||
assertTrue('a' in map)
|
expect:
|
||||||
assertFalse('f' in map)
|
'a' in map
|
||||||
|
'f' !in map
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "whenMapContainsFalseBooleanValues_thenCheckReturnsFalse"() {
|
||||||
void whenMapContainsFalseBooleanValues_thenCheckReturnsFalse() {
|
given:
|
||||||
def map = [a: true, b: false, c: null]
|
def map = [a: true, b: false, c: null]
|
||||||
|
|
||||||
assertTrue(map.containsKey('b'))
|
expect:
|
||||||
assertTrue('a' in map)
|
map.containsKey('b')
|
||||||
assertFalse('b' in map)
|
'a' in map
|
||||||
assertFalse('c' in map)
|
'b' !in map // get value of key 'b' and does the assertion
|
||||||
|
'c' !in map
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "givenMapOfPerson_whenUsingStreamMatching_thenShouldEvaluateMap"() {
|
||||||
void givenMapOfPerson_whenUsingStreamMatching_thenShouldEvaluateMap() {
|
expect:
|
||||||
assertTrue(personMap.keySet().stream().anyMatch {it == "Regina"})
|
personMap.keySet().stream()
|
||||||
assertFalse(personMap.keySet().stream().allMatch {it == "Albert"})
|
.anyMatch { it == "Regina" }
|
||||||
assertFalse(personMap.values().stream().allMatch {it.age < 30})
|
!personMap.keySet().stream()
|
||||||
assertTrue(personMap.entrySet().stream().anyMatch {it.key == "Abagail" && it.value.lastname == "Ballard"})
|
.allMatch { it == "Albert" }
|
||||||
|
!personMap.values().stream()
|
||||||
|
.allMatch { it.age < 30 }
|
||||||
|
personMap.entrySet().stream()
|
||||||
|
.anyMatch { it.key == "Abagail" && it.value.lastname == "Ballard" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "givenMapOfPerson_whenUsingCollectionMatching_thenShouldEvaluateMap"() {
|
||||||
void givenMapOfPerson_whenUsingCollectionMatching_thenShouldEvaluateMap() {
|
expect:
|
||||||
assertTrue(personMap.keySet().any {it == "Regina"})
|
personMap.keySet().any { it == "Regina" }
|
||||||
assertFalse(personMap.keySet().every {it == "Albert"})
|
!personMap.keySet().every { it == "Albert" }
|
||||||
assertFalse(personMap.values().every {it.age < 30})
|
!personMap.values().every { it.age < 30 }
|
||||||
assertTrue(personMap.any {firstname, person -> firstname == "Abagail" && person.lastname == "Ballard"})
|
personMap.any { firstname, person -> firstname == "Abagail" && person.lastname == "Ballard" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "givenMapOfPerson_whenUsingCollectionFind_thenShouldReturnElements"() {
|
||||||
void givenMapOfPerson_whenUsingCollectionFind_thenShouldReturnElements() {
|
expect:
|
||||||
assertNotNull(personMap.find {it.key == "Abagail" && it.value.lastname == "Ballard"})
|
personMap.find { it.key == "Abagail" && it.value.lastname == "Ballard" }
|
||||||
assertTrue(personMap.findAll {it.value.age > 20}.size() == 3)
|
personMap.findAll { it.value.age > 20 }.size() == 3
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
def "givenMapOfPerson_whenUsingStreamFind_thenShouldReturnElements"() {
|
||||||
void givenMapOfPerson_whenUsingStreamFind_thenShouldReturnElements() {
|
expect:
|
||||||
assertTrue(
|
personMap.entrySet().stream()
|
||||||
personMap.entrySet().stream()
|
.filter { it.key == "Abagail" && it.value.lastname == "Ballard" }
|
||||||
.filter {it.key == "Abagail" && it.value.lastname == "Ballard"}
|
.findAny()
|
||||||
.findAny().isPresent())
|
.isPresent()
|
||||||
assertTrue(
|
|
||||||
personMap.entrySet().stream()
|
personMap.entrySet().stream()
|
||||||
.filter {it.value.age > 20}
|
.filter { it.value.age > 20 }
|
||||||
.findAll().size() == 3)
|
.findAll()
|
||||||
|
.size() == 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,10 @@
|
|||||||
package com.baeldung.find
|
package com.baeldung.find
|
||||||
|
|
||||||
|
import groovy.transform.Canonical
|
||||||
|
|
||||||
|
@Canonical
|
||||||
class Person {
|
class Person {
|
||||||
private String firstname
|
String firstname
|
||||||
private String lastname
|
String lastname
|
||||||
private Integer age
|
Integer age
|
||||||
|
|
||||||
Person(String firstname, String lastname, Integer age) {
|
|
||||||
this.firstname = firstname
|
|
||||||
this.lastname = lastname
|
|
||||||
this.age = age
|
|
||||||
}
|
|
||||||
|
|
||||||
String getFirstname() {
|
|
||||||
return firstname
|
|
||||||
}
|
|
||||||
|
|
||||||
void setFirstname(String firstname) {
|
|
||||||
this.firstname = firstname
|
|
||||||
}
|
|
||||||
|
|
||||||
String getLastname() {
|
|
||||||
return lastname
|
|
||||||
}
|
|
||||||
|
|
||||||
void setLastname(String lastname) {
|
|
||||||
this.lastname = lastname
|
|
||||||
}
|
|
||||||
|
|
||||||
Integer getAge() {
|
|
||||||
return age
|
|
||||||
}
|
|
||||||
|
|
||||||
void setAge(Integer age) {
|
|
||||||
this.age = age
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
package com.baeldung.find
|
package com.baeldung.find
|
||||||
|
|
||||||
import org.junit.Test
|
import spock.lang.Specification
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue
|
class SetFindUnitTest extends Specification {
|
||||||
|
|
||||||
class SetFindUnitTest {
|
def "whenSetContainsElement_thenCheckReturnsTrue"() {
|
||||||
|
given:
|
||||||
@Test
|
|
||||||
void whenSetContainsElement_thenCheckReturnsTrue() {
|
|
||||||
def set = ['a', 'b', 'c'] as Set
|
def set = ['a', 'b', 'c'] as Set
|
||||||
|
|
||||||
assertTrue(set.contains('a'))
|
expect:
|
||||||
assertTrue('a' in set)
|
set.contains('a')
|
||||||
|
'a' in set
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user