ComeçarComece de graça

Digits of a number fail

Now that you've written a successful test, write a test that will fail when it encounters unexpected behavior at an edge case.

Software engineers often intentionally write failing tests to demonstrate the existence of a bug.

Este exercício faz parte do curso

Introduction to Testing in Java

Ver curso

Instruções do exercício

  • Enter the correct annotation that will enable JUnit to execute this test.
  • Call the method under test.
  • Use the correct JUnit assertion to assert expected behavior - the test failing.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

import org.junit.jupiter.api.Test;

import static com.datacamp.util.testing.CustomJUnitTestLauncher.launchTestsAndPrint;
import static org.junit.jupiter.api.Assertions.*;

public class LastDigitWithTests {
    public static void main(String[] args) {
        launchTestsAndPrint(LastDigitTest.class);
    }
}

class LastDigit {
    public static int lastDigit(int a) {
        return a % 10;
    }
}

class LastDigitTest {
    // Use the correct annotation to mark this as a JUnit test
    ____
    void testLastDigit() {
        int number = -2025;
        int expected = 5;

        // Call the method under test
        int actual = LastDigit.____;

        // Use the correct JUnit assertion for the scenario
        ____;
    }
}
Editar e executar o código