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() );
}
}
Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…
In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…
In this blog, I aim to provide a comprehensive list of valuable resources for learning…
Have you ever wondered how systems determine whether to grant or deny access, and how…
What revolutionary technologies and industries will define the future of business in 2025? As we…
For data scientists and machine learning researchers, 2024 has been a landmark year in AI…