A Java exercise that generates the multiplication table for a given integer. This exercise was developed focusing on the application of core software engineering principles like Test-Driven Development (TDD), Git best practices, and Java code coverage (JaCoCo).
- Java 21: The primary programming language used for this exercise.
- Maven: A build automation tool used to manage dependencies and lifecycle.
- JUnit 5: The testing framework used for writing and running unit tests.
- Hamcrest: A library of matcher objects that can be combined to create flexible and readable assertions in tests.
- Git & GitHub: Version control system and hosting platform used to manage the exercise's codebase.
- Visual Studio Code and IntelliJ IDEA: These are the integrated development environments (IDEs) used for development.
This exercise includes:
- A class responsible for creating the multiplication table for a number.
printTablemethod that generates and prints the multiplication table from 1 to 10 for a giveninteger number.- A unit test suite with at least 80% coverage, ensuring all code paths are thoroughly validated.
-
Test-Driven Development (TDD): A development workflow focused on writing a failing test first (Red), writing the minimal code to pass the test (Green), and then improving the code (Refactor).
-
Git Branching: The use of
feature,development, andreleasebranches to manage changes and maintain a stable main branch. -
Separation of Concerns: Refactoring code to separate distinct responsibilities, such as generating a string from the action of printing it. This makes the code more modular and testable.
-
StringBuilder: A more efficient and performant alternative to standard string concatenation within loops.
-
Platform Independence: Using
System.lineSeparator()instead of hardcoding\nto ensure the application behaves correctly across different operating systems. -
Parameterized Tests: Using JUnit 5's
@ParameterizedTestand@MethodSourceto run the same test with different data inputs, which reduces code duplication and improves test readability. -
Java Reflection: A powerful tool used to inspect and manipulate classes, methods, and fields at runtime. In this exercise, it was used to test a private method.
-
Code Coverage: Using tools like JaCoCo to measure the percentage of code that is executed by tests, which helps ensure high-quality and reliable code.
Here is a class diagram for the MultiplicationTable class and its test suite.
classDiagram
direction LR
class MultiplicationTable{
+void printTable(int number)
-String generateTableString(int number)
}
class MultiplicationTableTest{
-ByteArrayOutputStream outputStreamCaptor
+void setUp()
+Stream provideMultiplicationTableData()
+void testPrintTable(int number, String expectedOutput)
}
MultiplicationTableTest --> MultiplicationTable : tests
This project is built with Maven, which handles all dependencies. To run the tests and generate the code coverage report, simply execute the following command in your terminal:
mvn clean verifyThis will compile the code, run all unit tests, and produce a JaCoCo coverage report in the target/site/jacoco directory, named index.html.
This section contains a screenshot of the test coverage report, demonstrating that every method has been thoroughly tested.
This project is part of the Full Stack Web Development training program in Asturias, offered by Factoría F5.
The curriculum covers a wide range of topics, from basic programming languages and UX principles to advanced project development techniques. It includes front-end and back-end technologies, agile methodologies, and tools for user experience design and database development. The program also focuses on essential soft skills such as communication, problem-solving, teamwork, adaptability, and time management.
For any questions or inquiries, please do not hesitate to contact me!
Happy coding! 🌱 🐒
