BAEL-3511 : Default parameters in Java (#8317)

* Added Assertj dependency

* Default parameter example POJO and test class
This commit is contained in:
Sampada 2019-12-06 12:43:38 +05:30 committed by maibin
parent aa38ab45a4
commit 69f8d56f3e
3 changed files with 142 additions and 54 deletions

View File

@ -1,54 +1,61 @@
<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>
<artifactId>core-java-lang-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-bytecode</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-lang-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<jmh-core.version>1.19</jmh-core.version>
<jmh-generator.version>1.19</jmh-generator.version>
</properties>
</project>
<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>
<artifactId>core-java-lang-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-bytecode</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-lang-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<jmh-core.version>1.19</jmh-core.version>
<jmh-generator.version>1.19</jmh-generator.version>
<assertj.version>3.12.2</assertj.version>
</properties>
</project>

View File

@ -0,0 +1,61 @@
package com.baeldung.defaultparams;
public class Tea {
static final int DEFAULT_TEA_POWDER = 1; // add 1 tbsp tea powder by default
private String name; // mandatory
private int milk; // ml
private boolean herbs; // add herbs or don't
private int sugar; // tbsp
private int teaPowder; // tbsp
public Tea(String name, int milk, boolean herbs, int sugar, int teaPowder) {
this.name = name;
this.milk = milk;
this.herbs = herbs;
this.sugar = sugar;
this.teaPowder = teaPowder;
}
// when tea powder is not provided by client, use default value
public Tea(String name, int milk, boolean herbs, int sugar) {
this(name, milk, herbs, sugar, DEFAULT_TEA_POWDER);
}
// when sugar is not provided by client, don't add it
public Tea(String name, int milk, boolean herbs) {
this(name, milk, herbs, 0);
}
// when herbs is not provided by client, don't add it
public Tea(String name, int milk) {
this(name, milk, false);
}
// when milk is not provided by client, don't add it
public Tea(String name) {
this(name, 0);
}
public String getName() {
return name;
}
public int getMilk() {
return milk;
}
public boolean isHerbs() {
return herbs;
}
public int getSugar() {
return sugar;
}
public int getTeaPowder() {
return teaPowder;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.defaultparams;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class TeaUnitTest {
@Test
public void whenTeaWithOnlyName_thenCreateDefaultTea() {
Tea blackTea = new Tea("Black Tea");
assertThat(blackTea.getName()).isEqualTo("Black Tea");
assertThat(blackTea.getMilk()).isEqualTo(0);
assertThat(blackTea.isHerbs()).isFalse();
assertThat(blackTea.getSugar()).isEqualTo(0);
assertThat(blackTea.getTeaPowder()).isEqualTo(Tea.DEFAULT_TEA_POWDER);
}
}