Merge pull request #6964 from FDPro/bael-2932

dupirefr/dupire.francois+pro@gmail.com [BAEL-2932] Negate a Predicate Method Reference with Java 11
This commit is contained in:
Erik Pragt 2019-05-20 22:46:40 +10:00 committed by GitHub
commit 8c68020cfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 98 additions and 9 deletions

View File

@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
@ -12,7 +14,7 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
<relativePath>../..</relativePath>
</parent>
<dependencies>
@ -21,6 +23,12 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -41,6 +49,7 @@
<maven.compiler.source.version>11</maven.compiler.source.version>
<maven.compiler.target.version>11</maven.compiler.target.version>
<guava.version>27.1-jre</guava.version>
<assertj.version>3.11.1</assertj.version>
</properties>
</project>

View File

@ -0,0 +1,19 @@
package com.baeldung.predicate.not;
public class Person {
private static final int ADULT_AGE = 18;
private int age;
public Person(int age) {
this.age = age;
}
public boolean isAdult() {
return age >= ADULT_AGE;
}
public boolean isNotAdult() {
return !isAdult();
}
}

View File

@ -7,7 +7,7 @@ import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
public class AppUnitTest
extends TestCase
{
/**
@ -15,7 +15,7 @@ public class AppTest
*
* @param testName name of the test case
*/
public AppTest( String testName )
public AppUnitTest(String testName )
{
super( testName );
}
@ -25,7 +25,7 @@ public class AppTest
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
return new TestSuite( AppUnitTest.class );
}
/**

View File

@ -32,7 +32,7 @@ import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
public class HttpClientTest {
public class HttpClientUnitTest {
@Test
public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException {

View File

@ -19,7 +19,7 @@ import java.time.Duration;
import org.junit.Test;
public class HttpRequestTest {
public class HttpRequestUnitTest {
@Test
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {

View File

@ -14,7 +14,7 @@ import java.net.http.HttpResponse;
import org.junit.Test;
public class HttpResponseTest {
public class HttpResponseUnitTest {
@Test
public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException {

View File

@ -0,0 +1,61 @@
package com.baeldung.predicate.not;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.function.Predicate.not;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class PersonUnitTest {
private List<Person> people;
@BeforeEach
void preparePeople() {
people = Arrays.asList(
new Person(1),
new Person(18),
new Person(2)
);
}
@Test
void givenPeople_whenFilterIsAdult_thenOneResult() {
List<Person> adults = people.stream()
.filter(Person::isAdult)
.collect(Collectors.toList());
assertThat(adults).size().isEqualTo(1);
}
@Test
void givenPeople_whenFilterIsAdultNegated_thenTwoResults() {
List<Person> nonAdults = people.stream()
.filter(person -> !person.isAdult())
.collect(Collectors.toList());
assertThat(nonAdults).size().isEqualTo(2);
}
@Test
void givenPeople_whenFilterIsNotAdult_thenTwoResults() {
List<Person> nonAdults = people.stream()
.filter(Person::isNotAdult)
.collect(Collectors.toList());
assertThat(nonAdults).size().isEqualTo(2);
}
@Test
void givenPeople_whenFilterNotIsAdult_thenTwoResults() {
List<Person> nonAdults = people.stream()
.filter(not(Person::isAdult))
.collect(Collectors.toList());
assertThat(nonAdults).size().isEqualTo(2);
}
}