public class StringCalculator{ public static int add(String numbers){ if(numbers == "") { return 0; } String[] numbersArr = numbers.split(","); if (numbersArr.length > 1) { int result = Integer.parseInt(numbersArr[0]) + Integer.parseInt(numbersArr[1]); return result; } return Integer.parseInt(numbers); } }
- public class StringCalculator{
- public static int add(String numbers){
- if(numbers == "") {
- return 0;
- }
return 1;- String[] numbersArr = numbers.split(",");
- if (numbersArr.length > 1) {
- int result = Integer.parseInt(numbersArr[0]) + Integer.parseInt(numbersArr[1]);
- return result;
- }
- return Integer.parseInt(numbers);
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class StringCalculatorTest { @Test void returnsZeroForEmptyString() { int result = StringCalculator.add(""); assertEquals(0, result); } @Test void returnsOneForInputOne() { int result = StringCalculator.add("1"); assertEquals(1, result); } @Test void returnsTwoForInputTwo() { int result = StringCalculator.add("2"); assertEquals(2, result); } @Test void returnsSumForInputOneTwo() { int result = StringCalculator.add("1,2"); assertEquals(3, result); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class StringCalculatorTest {
@Testvoid returnsZeroForEmptyString() {int result = StringCalculator.add("");assertEquals(0, result);}- @Test
- void returnsZeroForEmptyString() {
- int result = StringCalculator.add("");
- assertEquals(0, result);
- }
- @Test
- void returnsOneForInputOne() {
- int result = StringCalculator.add("1");
- assertEquals(1, result);
- }
- @Test
- void returnsTwoForInputTwo() {
- int result = StringCalculator.add("2");
- assertEquals(2, result);
- }
- @Test
- void returnsSumForInputOneTwo() {
- int result = StringCalculator.add("1,2");
- assertEquals(3, result);
- }
- }