Devemonvs.Friendly-Aid2 months ago
Refactored using TDD.
I created a single parameterized test to avoid redundant tests for the same output function without additional logic.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; class ReverseStringTest { ("testCases") public void it_should_return_the_expected_value_for_test_case(String inputName, String expectedOutputValue) { assertThat(reverseString.reverseString(inputName)) .as("It should return the expected value for %s", inputName) .isEqualTo(expectedOutputValue); } public static Stream<Arguments> testCases() { return Stream.of( Arguments.of("yeknom", "monkey"), Arguments.of("emoh", "home"), Arguments.of("sisoinoconaclovociliscipocsorcimartluonomuenp", "pneumonoultramicroscopicsilicovolcanoconiosis") ); } private final ReverseString reverseString = new ReverseString(); }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;- import org.junit.jupiter.params.ParameterizedTest;
- import org.junit.jupiter.params.provider.Arguments;
- import org.junit.jupiter.params.provider.MethodSource;
- import java.util.stream.Stream;
- import static org.assertj.core.api.Assertions.assertThat;
- class ReverseStringTest {
@Testpublic void testMonkey() {assertEquals("yeknom", ReverseString.reverseString( "monkey"));}@Testvoid testHome() {assertEquals("emoh", ReverseString.reverseString("home"));- @ParameterizedTest
- @MethodSource("testCases")
- public void it_should_return_the_expected_value_for_test_case(String inputName, String expectedOutputValue) {
- assertThat(reverseString.reverseString(inputName))
- .as("It should return the expected value for %s", inputName)
- .isEqualTo(expectedOutputValue);
- }
@Testvoid testPneumonoultramicroscopicsilicovolcanoconiosis() {assertEquals("sisoinoconaclovociliscipocsorcimartluonomuenp", ReverseString.reverseString("pneumonoultramicroscopicsilicovolcanoconiosis"));- public static Stream<Arguments> testCases() {
- return Stream.of(
- Arguments.of("yeknom", "monkey"),
- Arguments.of("emoh", "home"),
- Arguments.of("sisoinoconaclovociliscipocsorcimartluonomuenp", "pneumonoultramicroscopicsilicovolcanoconiosis")
- );
- }
}- private final ReverseString reverseString = new ReverseString();
- }
I created a single parameterized test to avoid redundant tests for the same output function without additional logic.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; class SolutionTest { ("testCases") public void it_should_return_the_expected_value_for_test_case(String inputName, String expectedOutputValue) { assertThat(greet.greet(inputName)) .as("It should return the expected value for %s", inputName) .isEqualTo(expectedOutputValue); } public static Stream<Arguments> testCases() { return Stream.of( Arguments.of("Reemu", "hello my name is Reemu"), Arguments.of("Pepo", "hello my name is Pepo"), Arguments.of("Delacroix", "hello my name is Delacroix"), Arguments.of("Toto", "hello my name is Toto") ); } private final Greet greet = new Greet(); }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;- import org.junit.jupiter.params.ParameterizedTest;
- import org.junit.jupiter.params.provider.Arguments;
- import org.junit.jupiter.params.provider.MethodSource;
- import java.util.stream.Stream;
- import static org.assertj.core.api.Assertions.assertThat;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
@Testvoid testSomething() {assertEquals("hello my name is Reemu", new Greet().greet("Reemu"));- @ParameterizedTest
- @MethodSource("testCases")
- public void it_should_return_the_expected_value_for_test_case(String inputName, String expectedOutputValue) {
- assertThat(greet.greet(inputName))
- .as("It should return the expected value for %s", inputName)
- .isEqualTo(expectedOutputValue);
- }
- public static Stream<Arguments> testCases() {
- return Stream.of(
- Arguments.of("Reemu", "hello my name is Reemu"),
- Arguments.of("Pepo", "hello my name is Pepo"),
- Arguments.of("Delacroix", "hello my name is Delacroix"),
- Arguments.of("Toto", "hello my name is Toto")
- );
- }
- private final Greet greet = new Greet();
- }