<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
Как после модификации кода быть уверенным, что он все ещё работает правильно?
JUnit
Применение новых аннотаций
Использование пользовательского имени для класса и метода теста
Возможность объединения нескольких assert в группу assert
Возможность запуска в группе assert независимых assert
Использование части кода теста для проверки на генерацию объекта класса исключения
Использование тайм-аута при выполнении теста
Использование предположений в методе
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
StringService
public interface StringService {
boolean isEmpty(String str);
String trim(String str);
boolean isPalindrome(String str) throws WrongArgumentException;
String[] split(String test, String separator);
}
StringServiceImpl
public class StringServiceImpl implements StringService {
public boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
public String trim(String str) {
return str.trim();
}
public boolean isPalindrome(String str) throws WrongArgumentException {
if (str == null) {
throw new WrongArgumentException("String can't be 'null'");
}
return str.equals(new StringBuilder(str).reverse().toString());
}
public String[] split(String text, String separator) {
if (separator.contains("|")) {
separator = separator.replace("|", "\\|");
}
return text.split(separator);
}
}
StringServiceTest
public class StringServiceTest {
private static StringService stringService;
@BeforeAll
static void init() {
stringService = new StringServiceImpl();
}
// tests
}
@Test
@Test
void isPalindrome() throws WrongArgumentException {
Assertions.assertTrue(stringService.isPalindrome("121"));
}
@Test
as set@Test
void trimTest() {
Assertions.assertEquals("", stringService.trim(" \n\t "));
Assertions.assertEquals("1", stringService.trim(" 1\t"));
Assertions.assertEquals("c", stringService.trim(" c\n\n "));
Assertions.assertEquals("e", stringService.trim("\n\ne "));
}
@Test
as group@Test
public void splitTest() {
String[] splitText = stringService.split("a|b", "|");
Assertions.assertAll("Group Test: split()",
() -> Assertions.assertEquals(new String[]{"a", "b"}.length, splitText.length),
() -> Assertions.assertEquals("a", splitText[0]),
() -> Assertions.assertEquals("b", splitText[1]));
}
@ParameterizedTest
@MethodSource("isEmptyArgumentsProvider")
@ParameterizedTest
void isEmptyTest(boolean expected, String str) {
Assertions.assertEquals(expected, stringService.isEmpty(str));
}
static Stream<Arguments> isEmptyArgumentsProvider() {
return Stream.of(
Arguments.arguments(true, "" ),
Arguments.arguments(true, null),
Arguments.arguments(true, " "),
Arguments.arguments(true, "\t"),
Arguments.arguments(true, "\n")
);
}
@Test
when throws Exception@Test
void isPalindromeThrowExceptionTest() {
Executable executable = () -> stringService.isPalindrome(null);
Assertions.assertThrows(WrongArgumentException.class, executable);
}