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.
Ok thats it for now. In the next post I will show how to unit test the business / service layer of the lead microservice.
- AssertJ - provides fluent assertions for Java.
- Testing improvements in Spring Boot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Post a Comment