0% found this document useful (0 votes)
10 views7 pages

Week 14

Unit testing is a methodology that tests individual components of software applications in isolation to ensure they perform as expected and to identify defects early. It utilizes mock objects for isolation and is automated for frequent execution, enhancing code correctness and maintainability. The document also covers unit testing in JUnit5, including simple tests, coverage analysis, and various assert methods for validating conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Week 14

Unit testing is a methodology that tests individual components of software applications in isolation to ensure they perform as expected and to identify defects early. It utilizes mock objects for isolation and is automated for frequent execution, enhancing code correctness and maintainability. The document also covers unit testing in JUnit5, including simple tests, coverage analysis, and various assert methods for validating conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Software construction and

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

● assertEquals(boolean condition): Asserts that both parameters given are equal.

● assertNotEquals(boolean condition): Asserts that both parameters are not equal.

● assertTrue(boolean condition): Asserts that the given condition is true.

● assertFalse(boolean condition): Asserts that the given condition is false.

● assertNull(Object object): Asserts that the given object reference is null.

● assertNotNull(Object object): Asserts that the given object reference is not null.

● assertThrows(expectedException, executable): Asserts that the specified executable (lambda or

method reference) throws the expected exception.


Assert Throws Example

assertThrows(IllegalArgumentException.class,
() -> {
grader.determineLetterGrade(-1);
});

You might also like