Week 14
Week 14
development Practical - I
Week 14
Unit Testing
● Unit testing is a software testing methodology that focuses on testing individual units
or components of a software application in isolation.
● Each unit, typically a function or method, is tested independently of the rest of the
application.
● The purpose is to verify that each unit performs as expected and to identify and fix any
defects early in the development process.
● Isolation is achieved using mock objects or stubs to simulate the behavior of
dependencies.
● Unit tests are automated, allowing for easy and frequent execution through testing
frameworks or tools.
● It helps ensure code correctness, improves software maintainability, and facilitates
continuous integration practices.
● By catching issues early, it reduces overall development costs and enhances the
reliability of the software system as a whole.
Unit Test in Junit5
Simple Unit Test
@Test
void twoPlusTwoShouldEqualFour(){
var calulator = new SimpleCalculator();
assertEquals(4,calulator.add(2,2));
}
Run unit test with coverage
"Run with coverage" in JUnit refers to executing unit tests while collecting
code coverage data. JUnit, along with coverage tools like JaCoCo, tracks
lines, branches, and statements covered during testing. A code coverage
report shows the percentage of code exercised by tests, helping identify
untested code areas. High coverage indicates thorough testing, but it
doesn't guarantee defect-free code. Developers can improve test suites
based on coverage insights, ensuring more robust and reliable applications.
Assert Methods in java
● assertNotNull(Object object): Asserts that the given object reference is not null.
assertThrows(IllegalArgumentException.class,
() -> {
grader.determineLetterGrade(-1);
});