Skip to main content

Complete Unit tests - Microservices - Repository Layer

In the previous post, I introduced the unit tests for the repository layer of the lead microservice. In this short post, I will do some cleanup, add all the unit tests. Finally, I replaced all the JUNIT asserts with AssertJ asserts. AssserJ is also recommended by the Spring champions over at Pivotal.



package com.effectiv.crm.repository.ut;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
import com.effectiv.crm.domain.Lead;
import com.effectiv.crm.repository.LeadRepository;
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
/**
* Lead Repository Unit test This test cannot be run with @DataJpaTest due to
* issues with Spring Data JPA custom repository method
*
* @author Dhrubo
*
*/
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@Transactional
@RunWith(SpringRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
// @WithMockUser(username = "test@t.com", roles = { "ADMIN", "USER" })
@DatabaseSetup("lead.xml")
// TODO WithSecurityContextTestExecutionListener.class to be added in future to
// include security in this test
public class LeadRepositoryTest {
@Autowired
private LeadRepository repository;
@Test
public void findOne() {
Lead persistedLead = this.repository.findOne("1");
assertThat(persistedLead.getId()).isEqualTo("1");
assertThat(persistedLead.getFirstName()).isEqualTo("Virat");
}
@Test
public void findOneDoesNotExist() {
Lead noLeadEntry = this.repository.findOne("26");
assertThat(noLeadEntry).isNull();
}
@Test
public void findAll() {
List<Lead> leadList = repository.findAll();
assertThat(leadList.size()).isEqualTo(25);
assertThat(leadList.get(13).getFirstName()).isEqualTo("Sachin");
}
@Test
public void save() {
Lead lead = new Lead();
lead.setAddress(null);
lead.setAnnualRevenue(50000.00D);
lead.setCompany("EFFECTIV");
lead.setDeleted(false);
lead.setDescription("Unit - test - entity :: Lead");
lead.setDesignation("Project Manager");
lead.setEmail("effectvlead@effectiv.com");
lead.setEmailOptOut(false);
Map<String, String> extension = new HashMap<String, String>();
extension.put("flex_field_string", "some value");
extension.put("flex_field_date", "02/20/2017");
extension.put("flex_field_number", "77");
lead.setExtensions(extension);
lead.setFacebook("effectiv");
lead.setFax("22445566");
lead.setFirstName("Effectiv");
lead.setLastName("Systems");
lead.setIndustry(null);
lead.setLeadSource(null);
lead.setLeadStatus(null);
lead.setMobile("1234567890");
lead.setNoOfEmployees(22);
lead.setPhone("9999988888");
lead.setRating(null);
lead.setSalutation(null);
lead.setTwitter("@effectiv");
lead.setWebsite("http://www.effectivcrm.com");
Lead savedLead = repository.save(lead);
assertThat(savedLead.getId()).isNotNull();
}
@Test
public void delete() {
Lead retrievedLead = this.repository.findOne("11");
retrievedLead.setDeleted(true);
Lead retrievedLeadAfterSave = repository.save(retrievedLead);
assertThat(retrievedLeadAfterSave.isDeleted()).isEqualTo(true);
}
@Test
public void update() {
Lead retrievedLead = this.repository.findOne("1");
retrievedLead.setFirstName("Viraat");
retrievedLead.setLastName("Kohli");
retrievedLead.setAnnualRevenue(6000);
Lead persistedLead = this.repository.save(retrievedLead);
assertThat(persistedLead.getId()).isEqualTo(retrievedLead.getId()); // update
// must
// not
// change
// id
assertThat(persistedLead.getFirstName()).isEqualTo("Viraat");
assertThat(persistedLead.getLastName()).isEqualTo("Kohli");
assertThat(persistedLead.getAnnualRevenue()).isEqualTo(6000);
}
@Test
public void purge() {
Lead lead = repository.findOne("16");
assertThat(lead).isNotNull();
assertThat(lead.getId()).isEqualTo("16");
repository.delete("16");
lead = repository.findOne("16");
assertThat(lead).isNull();
}
}

Ok thats it for now. In the next post I will show how to unit test the business / service layer of the lead microservice.

Comments

Popular posts from this blog

Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2 Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- Tiles --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>${tiles.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>${tiles.version}</version> <scope>compile</s...

Breaking down the CRM monolith

In my previous posts, I have shared some theory regarding microservices. But it's time to start some implementation. I love to write code and see and feel things working. So I will start a series to refactor a monolithic CRM system and transform it into microservices based flexible software. Big ball of mud. Customer Relationship Management(CRM) is that giant software which existed since time immemorial and is used by all companies in some form or shape. Big enterprises will buy CRM software (also known as packages) from top CRM vendors like Oracle, SAP, Salesforce etc and then employ an army of consultants to try and implement it. Most of the classic CRM systems in the market today, even if deployed on the cloud are the big monolithic ball of mud. They are the gigantic piece of software with the huge feature set. Most often those requirements are surplus to the requirement or they will not fit into the processes of the company. So the company has to hire these certified consu...

Getting started with Prime faces 2

Prime faces is an amazing JSF framework from Cagatay Civici ( http://cagataycivici.wordpress.com/ ). Its wonderful because it is easy to use, minimal dependencies, has probably the widest set of controls among all JSF frameworks, easy to integrate with Spring (including Spring Security) , Java EE EJBs, and last but not the least mobile UI support. So I decided to give Prime faces a try, before selecting it to use in my projects. Step 1 – Create Maven 2 project As a first step to integrating Prime faces, create a Maven 2 project in Eclipse. You will need to select ‘maven-archetype-webapp’. Step 2 – Add repositories and dependencies in pom.xml I will be using Prime faces 2 with JSF 2 on Tomcat 6. Since the dependencies for Prime Faces and JSF 2 (JSF 2.0.3 is required) are available on different repositories, I will add them to my pom file first. The listing below shows my pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/X...