Skip to content

Commit 67c9b1e

Browse files
daincredibleholgmaibin
authored andcommitted
BAEL-1376: Varargs Code Example (eugenp#3129)
* BAEL-1376: Varargs Code Example * BAEL-1376: Varargs Code Example Moving the "logic" into a test and removing the unnecessary class.
1 parent 064ea79 commit 67c9b1e

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.varargs;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
8+
public class FormatterTest {
9+
10+
private final static String FORMAT = "%s %s %s";
11+
12+
@Test
13+
public void givenNoArgument_thenEmptyAndTwoSpacesAreReturned() {
14+
String actualResult = format();
15+
16+
assertThat(actualResult, is("empty "));
17+
}
18+
19+
@Test
20+
public void givenOneArgument_thenResultHasTwoTrailingSpace() {
21+
String actualResult = format("baeldung");
22+
23+
assertThat(actualResult, is("baeldung "));
24+
}
25+
26+
@Test
27+
public void givenTwoArguments_thenOneTrailingSpaceExists() {
28+
String actualResult = format("baeldung", "rocks");
29+
30+
assertThat(actualResult, is("baeldung rocks "));
31+
}
32+
33+
@Test
34+
public void givenMoreThanThreeArguments_thenTheFirstThreeAreUsed() {
35+
String actualResult = formatWithVarArgs("baeldung", "rocks", "java", "and", "spring");
36+
37+
assertThat(actualResult, is("baeldung rocks java"));
38+
}
39+
40+
public String format() {
41+
return format("empty", "");
42+
}
43+
44+
public String format(String value) {
45+
return format(value, "");
46+
}
47+
48+
public String format(String val1, String val2) {
49+
return String.format(FORMAT, val1, val2, "");
50+
}
51+
52+
public String formatWithVarArgs(String... values) {
53+
if (values.length == 0) {
54+
return "no arguments given";
55+
}
56+
57+
return String.format(FORMAT, values);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)