The quality of software can be enhanced by writing automated testing. JUnit plays a significant role in testing the software. In this article, let us see a few samples of magic numbers like Armstrong, Unique, and Perfect Numbers and their corresponding JUnit as maven project.
Maven Example Project Structure:
Let’s start with pom.xml file
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < groupId >com.gfg.numbersamples</ groupId > < artifactId >NumberUtilityServices</ artifactId > < packaging >jar</ packaging > < version >1.0-SNAPSHOT</ version > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >1.8</ maven.compiler.source > < maven.compiler.target >1.8</ maven.compiler.target > < junit.version >5.3.1</ junit.version > < pitest.version >1.4.3</ pitest.version > </ properties > < dependencies > <!-- junit 5, unit test --> < dependency > < groupId >org.junit.jupiter</ groupId > < artifactId >junit-jupiter-engine</ artifactId > < version >${junit.version}</ version > < scope >test</ scope > </ dependency > </ dependencies > < build > < finalName >NumberUtilityServices</ finalName > < plugins > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-surefire-plugin</ artifactId > < version >3.0.0-M1</ version > </ plugin > < plugin > < groupId >org.pitest</ groupId > < artifactId >pitest-maven</ artifactId > < version >${pitest.version}</ version > < executions > < execution > < id >pit-report</ id > < phase >test</ phase > < goals > < goal >mutationCoverage</ goal > </ goals > </ execution > </ executions > <!-- Need this to support JUnit 5 --> < dependencies > < dependency > < groupId >org.pitest</ groupId > < artifactId >pitest-junit5-plugin</ artifactId > < version >0.8</ version > </ dependency > </ dependencies > < configuration > < targetClasses > < param >com.gfg.numbersamples.*numbersamples*</ param > </ targetClasses > < targetTests > < param >com.gfg.numbersamples.*</ param > </ targetTests > </ configuration > </ plugin > </ plugins > </ build > </ project > |
We need to prepare a Java file where all the business logic is to be written. Now let us write for 3 different services
Java
public class NumberSampleService { // Armstrong Number checking // 153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong // number. public boolean checkArmstrongNumber( int inputNumber) { int checkingNumber, remainder, finalResult = 0 ; checkingNumber = inputNumber; while (checkingNumber != 0 ) { remainder = checkingNumber % 10 ; finalResult += Math.pow(remainder, 3 ); checkingNumber /= 10 ; } if (finalResult == inputNumber) return true ; else return false ; } public boolean checkUniqueNumber( int inputNumber) { int number1, number2, uniqueCount = 0 , reminder1, reminder2; // num1 and num2 are temporary variable number1 = inputNumber; number2 = inputNumber; // iterate over all digits of the number while (number1 > 0 ) { // determine the last digit of the number reminder1 = number1 % 10 ; while (number2 > 0 ) { // finds the last digit reminder2 = number2 % 10 ; // comparing the last digit if (reminder1 == reminder2) { // increments the count variable by 1 uniqueCount++; } // removes the last digit from the number number2 = number2 / 10 ; } // removes the last digit from the number number1 = number1 / 10 ; } if (uniqueCount == 1 ) { return true ; } else { return false ; } } // Example 496 // First, we find the factors of 496 i.e. 1, 2, 4, 8, // 16, 31, 62, 124, and 248. Let's find the sum of // factors (1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 +248 = // 496). public boolean checkPerfectNumber( int inputNumber) { long sum = 0 ; int idx = 1 ; // executes until the condition becomes false while (idx <= inputNumber / 2 ) { if (inputNumber % idx == 0 ) { // calculates the sum of factors sum = sum + idx; } // end of if // after each iteration, increments the value // of variable i by 1 idx++; } // end of while // compares sum with the number if (sum == inputNumber) { return true ; } else return false ; } } |
Now let’s start creating JUnits
TestNumberSampleService.java
Java
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; public class TestNumberSampleService { // JUnit for Armstrong Number @Test public void testForPositiveArmstrongNumber() { NumberSampleService numberSampleService = new NumberSampleService(); assertEquals( true , numberSampleService.checkArmstrongNumber( 371 )); assertNotEquals( true , numberSampleService.checkArmstrongNumber( 3000 )); assertEquals( true , numberSampleService.checkArmstrongNumber( 153 )); } // JUnit for Unique Number @Test public void testForUniqueNumber() { NumberSampleService numberSampleService = new NumberSampleService(); assertEquals( true , numberSampleService.checkUniqueNumber( 12345 )); assertNotEquals( true , numberSampleService.checkUniqueNumber( 1100 )); assertEquals( true , numberSampleService.checkUniqueNumber( 98765 )); } // JUnit for Perfect Number @Test public void testForPerfectNumber() { NumberSampleService numberSampleService = new NumberSampleService(); assertEquals( true , numberSampleService.checkPerfectNumber( 28 )); assertNotEquals( true , numberSampleService.checkPerfectNumber( 4558 )); assertEquals( true , numberSampleService.checkPerfectNumber( 8128 )); } } |
We can write n number of business logic in a single java file or multiple java files. Similarly, we can write the JUnit test cases for each and every method by having the @Test annotation. This way of writing enhances our business logic to get stronger.
If the code is perfect in business logic and all test cases are run successfully, then we can able to see the output as indicated
In case of any errors, it will be indicated below
Errors indicate that the (expected result and the actual result should match) there are some errors in the actual result. With that, we can come to know about the existence of error and with that, we can correct the business logic.
Conclusion
Compared to manual testing, automated testing using JUNIT is very efficient. To get quality software, writing JUnit test cases helps.