JUnit for Armstrong, Unique and Perfect Numbers as a Maven Project
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" ?>
< 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 >
< 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 >
< 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 {
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;
number1 = inputNumber;
number2 = inputNumber;
while (number1 > 0 ) {
reminder1 = number1 % 10 ;
while (number2 > 0 ) {
reminder2 = number2 % 10 ;
if (reminder1 == reminder2) {
uniqueCount++;
}
number2 = number2 / 10 ;
}
number1 = number1 / 10 ;
}
if (uniqueCount == 1 ) {
return true ;
}
else {
return false ;
}
}
public boolean checkPerfectNumber( int inputNumber)
{
long sum = 0 ;
int idx = 1 ;
while (idx <= inputNumber / 2 ) {
if (inputNumber % idx == 0 ) {
sum = sum + idx;
}
idx++;
}
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 {
@Test
public void testForPositiveArmstrongNumber() {
NumberSampleService numberSampleService = new NumberSampleService();
assertEquals( true , numberSampleService.checkArmstrongNumber( 371 ));
assertNotEquals( true , numberSampleService.checkArmstrongNumber( 3000 ));
assertEquals( true , numberSampleService.checkArmstrongNumber( 153 ));
}
@Test
public void testForUniqueNumber() {
NumberSampleService numberSampleService = new NumberSampleService();
assertEquals( true , numberSampleService.checkUniqueNumber( 12345 ));
assertNotEquals( true , numberSampleService.checkUniqueNumber( 1100 ));
assertEquals( true , numberSampleService.checkUniqueNumber( 98765 ));
}
@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.
Last Updated :
28 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...