diff --git a/pom.xml b/pom.xml index 5c1ea68..f94aa88 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,20 @@ mssql-jdbc 6.1.0.jre8 + + + org.mockito + mockito-all + 1.9.5 + test + + + + junit + junit + 4.12 + test + diff --git a/src/main/java/com/rshka/mcs/service/CustomerServiceImpl.java b/src/main/java/com/rshka/mcs/service/CustomerServiceImpl.java index ef4714e..a0609dd 100644 --- a/src/main/java/com/rshka/mcs/service/CustomerServiceImpl.java +++ b/src/main/java/com/rshka/mcs/service/CustomerServiceImpl.java @@ -14,6 +14,14 @@ public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDao customerDao; + public CustomerServiceImpl() { + + } + + public CustomerServiceImpl(CustomerDao customerDao) { + this.customerDao = customerDao; + } + public Customer getCustomer(String document) { return customerDao.getCustomer(document); diff --git a/src/test/java/com/rshka/mcs/services/CustomerServiceImplTest.java b/src/test/java/com/rshka/mcs/services/CustomerServiceImplTest.java new file mode 100644 index 0000000..07d4615 --- /dev/null +++ b/src/test/java/com/rshka/mcs/services/CustomerServiceImplTest.java @@ -0,0 +1,67 @@ +package com.rshka.mcs.services; + +import static org.junit.Assert.assertNotNull; + +import javax.ws.rs.ext.ExceptionMapper; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.dao.EmptyResultDataAccessException; + +import com.rshka.mcs.beans.Customer; +import com.rshka.mcs.dao.CustomerDaoImpl; +import com.rshka.mcs.service.CustomerServiceImpl; +import static com.rshka.mcs.utils.TestUtils.getCustomerByDocument; + +@RunWith(MockitoJUnitRunner.class) +public class CustomerServiceImplTest { + + @InjectMocks + CustomerServiceImpl customerServiceImpl; + + @Mock + private CustomerDaoImpl customerDaoImpl; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + public CustomerServiceImplTest() { + } + + @Test + public void testGetCustomerOk() { + + Mockito.when(this.customerDaoImpl.getCustomer(Mockito.anyString())).thenReturn(getCustomerByDocument()); + + this.customerServiceImpl = new CustomerServiceImpl(customerDaoImpl); + + Customer customer = this.customerServiceImpl.getCustomer(Mockito.anyString()); + + assertNotNull(customer); + + } + + @Test + public void testGetCustomerNotOk() { + + this.customerServiceImpl = new CustomerServiceImpl(customerDaoImpl); + + Mockito.when(this.customerDaoImpl.getCustomer(Mockito.anyString())).thenThrow(new EmptyResultDataAccessException("Cannot find customer", 1)); + + EmptyResultDataAccessException expectedException = new EmptyResultDataAccessException("Cannot find customer", 1); + + thrown.expect(EmptyResultDataAccessException.class); + + Customer customer = this.customerServiceImpl.getCustomer(Mockito.anyString()); + + + } + +} \ No newline at end of file diff --git a/src/test/java/com/rshka/mcs/utils/TestUtils.java b/src/test/java/com/rshka/mcs/utils/TestUtils.java new file mode 100644 index 0000000..d1a2537 --- /dev/null +++ b/src/test/java/com/rshka/mcs/utils/TestUtils.java @@ -0,0 +1,11 @@ +package com.rshka.mcs.utils; + +import com.rshka.mcs.beans.Customer; + +public class TestUtils { + + public static Customer getCustomerByDocument() { + return new Customer(1, "Persona Nombre", "Avenida 1", "133 443", "12312312"); + } + +} diff --git a/target/classes/com/rshka/mcs/service/CustomerServiceImpl.class b/target/classes/com/rshka/mcs/service/CustomerServiceImpl.class index cb84905..bcf96c2 100644 Binary files a/target/classes/com/rshka/mcs/service/CustomerServiceImpl.class and b/target/classes/com/rshka/mcs/service/CustomerServiceImpl.class differ diff --git a/target/m2e-wtp/web-resources/META-INF/maven/com.rshk.demo.mcs/demo_mcs/pom.properties b/target/m2e-wtp/web-resources/META-INF/maven/com.rshk.demo.mcs/demo_mcs/pom.properties index c5fc77a..11661be 100644 --- a/target/m2e-wtp/web-resources/META-INF/maven/com.rshk.demo.mcs/demo_mcs/pom.properties +++ b/target/m2e-wtp/web-resources/META-INF/maven/com.rshk.demo.mcs/demo_mcs/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven Integration for Eclipse -#Thu Jan 16 22:35:34 PYST 2020 +#Fri Jan 17 07:51:57 PYST 2020 version=0.0.1-SNAPSHOT groupId=com.rshk.demo.mcs m2e.projectName=demo_mcs diff --git a/target/m2e-wtp/web-resources/META-INF/maven/com.rshk.demo.mcs/demo_mcs/pom.xml b/target/m2e-wtp/web-resources/META-INF/maven/com.rshk.demo.mcs/demo_mcs/pom.xml index 5c1ea68..f94aa88 100644 --- a/target/m2e-wtp/web-resources/META-INF/maven/com.rshk.demo.mcs/demo_mcs/pom.xml +++ b/target/m2e-wtp/web-resources/META-INF/maven/com.rshk.demo.mcs/demo_mcs/pom.xml @@ -55,6 +55,20 @@ mssql-jdbc 6.1.0.jre8 + + + org.mockito + mockito-all + 1.9.5 + test + + + + junit + junit + 4.12 + test +