BAEL-3511 : Default parameters in Java (#8317)
* Added Assertj dependency * Default parameter example POJO and test class
This commit is contained in:
parent
aa38ab45a4
commit
69f8d56f3e
|
@ -34,6 +34,12 @@
|
||||||
<artifactId>jmh-core</artifactId>
|
<artifactId>jmh-core</artifactId>
|
||||||
<version>${jmh-core.version}</version>
|
<version>${jmh-core.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -49,6 +55,7 @@
|
||||||
<properties>
|
<properties>
|
||||||
<jmh-core.version>1.19</jmh-core.version>
|
<jmh-core.version>1.19</jmh-core.version>
|
||||||
<jmh-generator.version>1.19</jmh-generator.version>
|
<jmh-generator.version>1.19</jmh-generator.version>
|
||||||
|
<assertj.version>3.12.2</assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue