Skip to content

Instantly share code, notes, and snippets.

View eliasnogueira's full-sized avatar
🇧🇷

Elias Nogueira eliasnogueira

🇧🇷
View GitHub Profile
class MethodSourceCase2ExampleTest {
@DisplayName("Payment status")
@ParameterizedTest(name = "The payment with status {0} returns the message {1}")
@MethodSource("statusList")
void paymentStatus(Status status, String message) {
String auditedPayment = auditService.getPayments(status);
assertThat(status).isEqualTo(message);
}
@eliasnogueira
eliasnogueira / ValueSourceSimpleExample.java
Created June 15, 2023 20:00
Simple example that explain how the MethodSource annotation from JUnit 5 works
class MyTestClass {
@DisplayName("Date must be from the current year")
@ParameterizedTest(name = "Check the current year of {0}")
@MethodSource("dateList")
void myTest(LocalDate date) {
assertThat(date).hasYear(2023);
}
static Stream<Arguments> dateList() {
@eliasnogueira
eliasnogueira / good-testing-practices-open-source-talk.md
Created June 11, 2023 14:32
Good testing practices in an open-source project

Title

Good testing practices in an open-source project

Description

Nowadays the contribution guide in every open-source project is not sufficient to describe the approaches, techniques, and how people solve break changes.

We all know: they are the most democratic projects we can work on, but sometimes not follow basic practices to elevate the project quality.

@eliasnogueira
eliasnogueira / SoftAssertionJunit5Test.java
Last active March 26, 2023 11:30
SoftAssertions: JUnit 5
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class SoftAssertionJunit5Test {
@Test
void softAssertionUsingJUnit5() {
var person = Person.builder().name("John").phoneNumber(null).age(16).build();
@eliasnogueira
eliasnogueira / SoftAssertionTest.java
Last active March 25, 2023 14:00
SoftAssertion: AssertJ
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
class SoftAssertionTest {
@Test
void softAssertionUsingAssertJ() {
var person = Person.builder().name("John").phoneNumber(null).age(16).build();
SoftAssertions.assertSoftly(softly -> {
@eliasnogueira
eliasnogueira / HardAssertionTest.java
Last active March 25, 2023 14:01
HardAssertion: JUnit 5
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
class HardAssertionTest {
@Test
void hardAssertion() {
var person = Person.builder().name("John").phoneNumber(null).age(16).build();
assertThat(person.getName()).isNotBlank();
@eliasnogueira
eliasnogueira / SoftAssertTestNGTest.java
Created March 25, 2023 12:53
SoftAssertions: TestNG
public class SoftAssertTestNGTest {
@Test
public void testNGSoftAssertion() {
var person = Person.builder().name("John").phoneNumber(null).age(16).build();
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(person.getName(), "John");
softAssert.assertNotNull(person.getPhoneNumber(), "Phone number cannot be null");
softAssert.assertEquals(person.getAge(), 25, "Age should be equal");
@eliasnogueira
eliasnogueira / AgeTest.java
Last active March 23, 2023 19:53
JUnit 5 - When to use @valuesource: test example
class AgeTest {
@DisplayName("Valid ages")
@ParameterizedTest(name = "{0} is a valid age for the requirement 18 to 30")
@ValueSource(ints = {18, 19, 29, 30})
void validAges(int age) {
Assertions.assertThat(isAgeValid(age)).isTrue();
}
@eliasnogueira
eliasnogueira / Example.java
Created March 23, 2023 17:35
JUnit 5 - When to use @valuesource: code example
public class Example {
private Boolean isAgeValid(int age) {
return age >= 18 && age <= 30 ? Boolean.TRUE : Boolean.FALSE;
}
}