Skip to content

Commit 3bc0e36

Browse files
author
onlyfullstack
committed
Adding test cases
1 parent 858a9e8 commit 3bc0e36

File tree

5 files changed

+263
-0
lines changed

5 files changed

+263
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.onlyfullstack.unittesting.bean;
2+
3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
6+
public final class Customer {
7+
8+
private Integer id;
9+
10+
private String name;
11+
12+
private String address;
13+
14+
public Customer(Integer id, String name, String address) {
15+
this.id = id;
16+
this.name = name;
17+
this.address = address;
18+
}
19+
20+
@Override
21+
public boolean equals(Object that) {
22+
return EqualsBuilder.reflectionEquals(this, that);
23+
}
24+
25+
@Override
26+
public int hashCode()
27+
{
28+
return HashCodeBuilder.reflectionHashCode(this);
29+
}
30+
31+
public Integer getId() {
32+
return id;
33+
}
34+
35+
public void setId(Integer id) {
36+
this.id = id;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public String getAddress() {
48+
return address;
49+
}
50+
51+
public void setAddress(String address) {
52+
this.address = address;
53+
}
54+
55+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.onlyfullstack.unittesting.repository;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.onlyfullstack.unittesting.bean.Customer;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class CustomerRepository {
11+
12+
private static Map<Integer, Customer> customerMap = new HashMap<>();
13+
14+
{
15+
customerMap.put(1, new Customer(1, "ABC", "Pune"));
16+
customerMap.put(2, new Customer(2, "XYZ", "Pune"));
17+
customerMap.put(3, new Customer(3, "PQR", "Pune"));
18+
customerMap.put(4, new Customer(4, "MNO", "Pune"));
19+
}
20+
21+
public boolean saveCusomer(Customer customer) {
22+
customerMap.put(customer.getId(), customer);
23+
return true;
24+
}
25+
26+
public Customer getCustomer(Integer customerId) {
27+
return customerMap.get(customerId);
28+
}
29+
30+
public void updateCustomer(Customer customer) {
31+
customerMap.put(customer.getId(), customer);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.onlyfullstack.unittesting.service;
2+
3+
import com.onlyfullstack.unittesting.bean.Customer;
4+
import com.onlyfullstack.unittesting.repository.CustomerRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
public final class CustomerService {
8+
9+
@Autowired
10+
public CustomerRepository repository;
11+
12+
public boolean saveCustomer(Customer customer) {
13+
if (customer == null) {
14+
throw new IllegalArgumentException("Invalid Customer details passed.");
15+
}
16+
return repository.saveCusomer(customer);
17+
}
18+
19+
public Customer getCustomer(Integer customerId) {
20+
if (customerId == null || customerId < 0) {
21+
throw new IllegalArgumentException("Invalid Customer details passed.");
22+
}
23+
return repository.getCustomer(customerId);
24+
}
25+
26+
public Customer updateCustomer(Customer customer) {
27+
if (customer == null || customer.getId() < 0) {
28+
throw new IllegalArgumentException("Invalid Customer details passed.");
29+
}
30+
repository.updateCustomer(customer);
31+
return customer;
32+
}
33+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.onlyfullstack.unittesting;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.Mock;
9+
import org.mockito.Spy;
10+
import org.mockito.junit.MockitoJUnitRunner;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertNull;
14+
import static org.mockito.Mockito.when;
15+
16+
/**
17+
* This class identifies the differences in between Mock and Spy
18+
*/
19+
@RunWith(MockitoJUnitRunner.class)
20+
public final class MockVsSpy {
21+
22+
/*
23+
We dont need to instantiate the mock List as the @Mock will create and instantiate the list for us
24+
*/
25+
@Mock
26+
private List<String> mockedList;
27+
28+
/*
29+
We need to instantiate the list object as the @Spy will use the real objects method if we dont mock them
30+
*/
31+
@Spy
32+
private List<String> spyList = new ArrayList();
33+
34+
@Test
35+
public void testMockList_checkDefaultBehaviour_whenMethodIsNotMocked() {
36+
/*If we dont mock the methods of @Mock object and try to call them
37+
then it will not do anything.*/
38+
39+
mockedList.add("test"); // add the String into list which will not do anything
40+
assertNull(mockedList.get(0)); // As the String was not added into the list it will return null value
41+
}
42+
43+
@Test
44+
public void testSpyList_checkDefaultBehaviour_whenMethodIsNotMocked() {
45+
/* add method is not mocked so the spyList will execute
46+
* the default behavoir of the add method and it will add a new String into list*/
47+
spyList.add("test");
48+
assertEquals("test", spyList.get(0));
49+
}
50+
51+
@Test
52+
public void testMockList_whenMethodIsMocked() {
53+
/*If we dont mock the methods of @Mock object and try to call them
54+
then it will not do anything.*/
55+
when(mockedList.size()).thenReturn(10);
56+
mockedList.add("One");
57+
assertNull(mockedList.get(0)); // Again the execution of add and get methods will not have any impact on mocked object
58+
59+
assertEquals(10, mockedList.size()); // As the String was not added into the list it will return null value
60+
}
61+
62+
@Test
63+
public void testSpyList_whenMethodIsMocked() {
64+
/* add method is not mocked so the spyList will execute
65+
* the default behaviour of the add method and it will add a new String into list*/
66+
when(spyList.size()).thenReturn(10);
67+
spyList.add("One");
68+
assertEquals("One", spyList.get(0));
69+
70+
assertEquals(10, spyList.size()); // size method will return 10 as we have mocked its implementation
71+
}
72+
73+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.onlyfullstack.unittesting.service;
2+
3+
import com.onlyfullstack.unittesting.bean.Customer;
4+
import com.onlyfullstack.unittesting.repository.CustomerRepository;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.InjectMocks;
8+
import org.mockito.Mock;
9+
import org.mockito.junit.MockitoJUnitRunner;
10+
11+
import static org.hamcrest.CoreMatchers.is;
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertThat;
14+
import static org.mockito.Mockito.any;
15+
import static org.mockito.Mockito.doAnswer;
16+
import static org.mockito.Mockito.doThrow;
17+
import static org.mockito.Mockito.eq;
18+
import static org.mockito.Mockito.times;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.verifyNoMoreInteractions;
21+
import static org.mockito.Mockito.when;
22+
23+
/**
24+
* This class contains usage of Mockito
25+
*/
26+
@RunWith(MockitoJUnitRunner.class)
27+
public class CustomerServiceTest {
28+
29+
@Mock
30+
CustomerRepository repository;
31+
32+
@InjectMocks
33+
CustomerService customerService;
34+
35+
@Test
36+
public void saveCustomer_withValidCustomer_when_thenReturn() {
37+
Customer customer = new Customer(6, "QQQ", "Mumbai");
38+
when(repository.saveCusomer(any())).thenReturn(true);
39+
Boolean save = customerService.saveCustomer(customer);
40+
assertThat(true, is(save));
41+
verify(repository, times(1)).saveCusomer(eq(customer));
42+
}
43+
44+
@Test(expected = IllegalStateException.class)
45+
public void saveCustomer_withValidCustomer_when_thenThrow() {
46+
Customer customer = new Customer(6, "QQQ", "Mumbai");
47+
when(repository.saveCusomer(any())).thenThrow(new IllegalStateException());
48+
customerService.saveCustomer(customer);
49+
verifyNoMoreInteractions();
50+
}
51+
52+
@Test
53+
public void updateCustomer_doAnswer_when() {
54+
Customer customer = new Customer(6, "QQQ", "Mumbai");
55+
doAnswer((arguments) -> {
56+
System.out.println("Inside doAnswer block");
57+
assertEquals(customer, arguments.getArgument(0));
58+
return null;
59+
}).when(repository).updateCustomer(any(Customer.class));
60+
customerService.updateCustomer(customer);
61+
}
62+
63+
@Test(expected = Exception.class)
64+
public void updateCustomer_doNothing_when() {
65+
Customer customer = new Customer(6, "QQQ", "Mumbai");
66+
doThrow(new Exception("Database connection issue")).when(repository).updateCustomer(any(Customer.class));
67+
customerService.updateCustomer(customer);
68+
}
69+
}

0 commit comments

Comments
 (0)