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