Skip to content

Commit 858a9e8

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

File tree

8 files changed

+54
-47
lines changed

8 files changed

+54
-47
lines changed

unit-testing/src/main/java/com/onlyfullstack/unittesting/bean/Employee.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/*
2-
* Copyright (c) 2019 Mastercard. All rights reserved.
3-
*/
4-
51
package com.onlyfullstack.unittesting.bean;
62

3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
76
public final class Employee {
87

98
private Integer id;
@@ -15,14 +14,14 @@ public final class Employee {
1514
private Double Salary;
1615

1716
@Override
18-
public boolean equals(Object o) {
19-
return true;
17+
public boolean equals(Object that) {
18+
return EqualsBuilder.reflectionEquals(this, that);
2019
}
2120

2221
@Override
2322
public int hashCode()
2423
{
25-
return 0;
24+
return HashCodeBuilder.reflectionHashCode(this);
2625
}
2726

2827
public Integer getId() {

unit-testing/src/main/java/com/onlyfullstack/unittesting/service/EmployeeService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright (c) 2019 Mastercard. All rights reserved.
3-
*/
4-
51
package com.onlyfullstack.unittesting.service;
62

73
import java.util.Collections;

unit-testing/src/main/java/com/onlyfullstack/unittesting/service/ExceptionHandling.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
/*
2-
* Copyright (c) 2019 Mastercard. All rights reserved.
3-
*/
4-
51
package com.onlyfullstack.unittesting.service;
62

73
import org.apache.commons.lang3.StringUtils;
84

95
/**
10-
* TODO: Javadoc
6+
* This class contains the business logic to throw an exception
117
*/
128
public final class ExceptionHandling {
139

1410
public String convertIntoUpperCase(String input) {
15-
if(StringUtils.isEmpty(input)) {
11+
if (StringUtils.isEmpty(input)) {
1612
throw new IllegalArgumentException("Empty value is passed.");
1713
}
1814
return input.toUpperCase();

unit-testing/src/test/java/com/onlyfullstack/unittesting/CollectionMatcher.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright (c) 2019 Mastercard. All rights reserved.
3-
*/
4-
51
package com.onlyfullstack.unittesting;
62

73
import java.util.ArrayList;
@@ -10,7 +6,6 @@
106
import java.util.List;
117
import java.util.Map;
128

13-
import org.hamcrest.collection.IsMapContaining;
149
import org.junit.Test;
1510

1611
import static org.hamcrest.Matchers.contains;

unit-testing/src/test/java/com/onlyfullstack/unittesting/MatcherExample.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright (c) 2019 Mastercard. All rights reserved.
3-
*/
4-
51
package com.onlyfullstack.unittesting;
62

73
import org.junit.Test;
@@ -45,7 +41,7 @@ public void startsWith_example() {
4541
@Test
4642
public void endsWith_example() {
4743
String name = "OnlyFullstack";
48-
String newName = "Only";
44+
String newName = "stack";
4945
assertThat(name, endsWith(newName));
5046
}
5147

unit-testing/src/test/java/com/onlyfullstack/unittesting/NumberMatcher.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright (c) 2019 Mastercard. All rights reserved.
3-
*/
4-
51
package com.onlyfullstack.unittesting;
62

73
import org.junit.Test;
@@ -13,7 +9,7 @@
139
import static org.junit.Assert.assertThat;
1410

1511
/**
16-
* TODO: Javadoc
12+
* This class contains the Number matchers
1713
*/
1814
public final class NumberMatcher {
1915

unit-testing/src/test/java/com/onlyfullstack/unittesting/service/EmployeeServiceTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright (c) 2019 Mastercard. All rights reserved.
3-
*/
4-
51
package com.onlyfullstack.unittesting.service;
62

73
import com.onlyfullstack.unittesting.bean.Employee;
@@ -12,8 +8,15 @@
128
import org.junit.BeforeClass;
139
import org.junit.Test;
1410

15-
import static org.hamcrest.CoreMatchers.*;
16-
import static org.junit.Assert.*;
11+
import static org.hamcrest.CoreMatchers.is;
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertFalse;
14+
import static org.junit.Assert.assertNotNull;
15+
import static org.junit.Assert.assertNotSame;
16+
import static org.junit.Assert.assertNull;
17+
import static org.junit.Assert.assertSame;
18+
import static org.junit.Assert.assertThat;
19+
import static org.junit.Assert.assertTrue;
1720

1821
/**
1922
* This class contains Unit Test cases of {@link EmployeeService}
@@ -66,7 +69,9 @@ public void isValidEmployee_withNegativeSalary() {
6669
@Test
6770
public void assertEquals_example() {
6871
Employee employeeNew = new Employee();
69-
employee.setSalary(1000000.0);
72+
employeeNew.setSalary(1000000.0);
73+
employeeNew.setName("Saurabh");
74+
employeeNew.setId(1);
7075
assertEquals("EMPLOYEE OBJECT", employee, employeeNew);
7176
}
7277

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
1-
/*
2-
* Copyright (c) 2019 Mastercard. All rights reserved.
3-
*/
4-
51
package com.onlyfullstack.unittesting.service;
62

3+
import org.assertj.core.api.Assertions;
74
import org.hamcrest.CoreMatchers;
5+
import org.junit.Rule;
86
import org.junit.Test;
7+
import org.junit.rules.ExpectedException;
8+
9+
import static org.assertj.core.api.Assertions.fail;
10+
import static org.hamcrest.MatcherAssert.assertThat;
911

10-
import static org.junit.Assert.*;
1112

1213
/**
13-
* TODO: Javadoc
14+
* This class contains the different ways to assert certain exception in Junit.
1415
*/
1516
public class ExceptionHandlingTest {
1617

1718
ExceptionHandling exceptionHandling = new ExceptionHandling();
1819

20+
@Rule
21+
public ExpectedException exception = ExpectedException.none();
22+
1923
@Test
2024
public void convertIntoUpperCase_withValidInput() {
2125
assertThat("ABC", CoreMatchers.is(exceptionHandling.convertIntoUpperCase("abc")));
2226
}
2327

2428
@Test
25-
public void convertIntoUpperCase_withInvalidInput_tryCatchIdem() {
26-
assertThat("ABC", CoreMatchers.is(exceptionHandling.convertIntoUpperCase("")));
29+
public void convertIntoUpperCase_withInvalidInput_tryCatchIdiom() {
30+
try {
31+
exceptionHandling.convertIntoUpperCase("");
32+
fail("It should throw IllegalArgumentException");
33+
} catch (IllegalArgumentException e) {
34+
Assertions.assertThat(e)
35+
.isInstanceOf(IllegalArgumentException.class)
36+
.hasMessage("Empty value is passed.");
37+
}
38+
}
39+
40+
@Test(expected = IllegalArgumentException.class)
41+
public void convertIntoUpperCase_withInvalidInput_testExpected() {
42+
exceptionHandling.convertIntoUpperCase("");
43+
}
44+
45+
@Test
46+
public void convertIntoUpperCase_withInvalidInput_ExpectedExceptionRule() {
47+
exception.expect(IllegalArgumentException.class);
48+
exception.expectMessage("Empty value is passed.");
49+
exceptionHandling.convertIntoUpperCase("");
2750
}
51+
2852
}

0 commit comments

Comments
 (0)