The article represents code samples for Junit tests and Mockito, a mocking framework. In addition, it describes different aspects of unit testing and mocking.
The goal is to create a simple piece of software which caters to the requirement of school admission where in applicants submit their admission application.
To meet above requirement, following different component is designed:
Source Code: AdmissionApplication.java
public class AdmissionApplication {
private AdmissionApplicationDAO admissionApplicationDao;
private ApplicationValidation applicationValidation;
public AdmissionApplication() {
}
public ApplicationResult recordNewApplication( Applicant applicant ) {
ApplicationResult result = null;
result = applicationValidation.validate( applicant );
if( !result.isSuccess() ) {
return result;
}
result = admissionApplicationDao.persist(applicant);
if( result.isSuccess() ) {
result.setMessage( "Admission application successful" );
} else {
result.setMessage( "Admission application failed" );
}
return result;
}
public AdmissionApplicationDAO getAdmissionApplicationDao() {
return admissionApplicationDao;
}
public void setAdmissionApplicationDao(
AdmissionApplicationDAO admissionApplicationDao) {
this.admissionApplicationDao = admissionApplicationDao;
}
public ApplicationValidation getApplicationValidation() {
return applicationValidation;
}
public void setApplicationValidation(ApplicationValidation applicationValidation) {
this.applicationValidation = applicationValidation;
}
}
Source Code (Junit with Mocking): AdmissionApplicationTest.java
In the source code below, following points should be noted:
The important point to note is that mocks have been created for two objects such as ApplicationValidation, and AdmissionApplicationDAO. That implies that code of AdmissionApplication is tested in isolation by mocking dependent classes.
public class AdmissionApplicationTest {
private AdmissionApplication aa;
@Mock
private ApplicationValidation appValidation;
@Mock
private AdmissionApplicationDAO aaDao;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
aa = new AdmissionApplication();
aa.setApplicationValidation( appValidation );
aa.setAdmissionApplicationDao( aaDao );
}
@After
public void tearDown() throws Exception {
aa = null;
appValidation = null;
aaDao = null;
}
@Test
public void applicationSuccessfulIfAllDetailsGiven() {
Applicant applicant = new Applicant();
ApplicationResult result = new ApplicationResult();
result.setSuccess( true );
Mockito.when( appValidation.validate(applicant) ).thenReturn( result );
Mockito.when( aaDao.persist(applicant)).thenReturn( result );
result = aa.recordNewApplication( applicant );
assertTrue( result.isSuccess() );
}
@Test
public void applicationFailureIfValidationFailed() {
Applicant applicant = new Applicant();
ApplicationResult result = new ApplicationResult();
result.setSuccess( false );
Mockito.when( appValidation.validate(applicant) ).thenReturn( result );
result = aa.recordNewApplication( applicant );
Mockito.verify(appValidation).validate(applicant);
Mockito.verify(aaDao, Mockito.never()).persist(applicant);
assertFalse( result.isSuccess() );
}
@Test
public void applicationFailureIfDBPersistenceFailed() {
Applicant applicant = new Applicant();
ApplicationResult result = new ApplicationResult();
result.setSuccess( true );
Mockito.when( appValidation.validate(applicant) ).thenReturn( result );
result.setSuccess( false );
Mockito.when( aaDao.persist(applicant) ).thenReturn( result );
result = aa.recordNewApplication( applicant );
assertFalse( result.isSuccess() );
}
}
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…