Skip to content

Commit 27ae6d3

Browse files
Merge pull request #30 from leonard1thecoder/create-unit-tests-for-get-country-by-name-entity-method
creating units tests for get country by name
2 parents 8a41cf2 + f746fb4 commit 27ae6d3

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.airpenthouse.GoTel.entities.countries;
2+
3+
import org.airpenthouse.GoTel.entities.country.CountriesEntity;
4+
import org.junit.jupiter.api.*;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.mockito.InjectMocks;
7+
import org.mockito.Mock;
8+
import org.mockito.junit.jupiter.MockitoExtension;
9+
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
12+
import java.sql.PreparedStatement;
13+
import java.util.List;
14+
import java.util.Set;
15+
16+
import static org.airpenthouse.GoTel.util.PropertiesUtilManager.*;
17+
import static org.junit.jupiter.api.Assertions.*;
18+
19+
@ExtendWith(MockitoExtension.class)
20+
public class TestCountriesEntity {
21+
22+
@Mock
23+
private PreparedStatement ps;
24+
@InjectMocks
25+
private CountriesEntity countriesEntity;
26+
27+
@BeforeAll
28+
public static void init() {
29+
30+
}
31+
32+
@AfterEach
33+
public void initAfterEach() {
34+
setProperties("countryName", "");
35+
}
36+
37+
@AfterAll
38+
public static void clean(){
39+
40+
}
41+
42+
@Test
43+
public void TestPrivateMethod_GetCountryByName() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
44+
setProperties("countryName", "South Africa");
45+
var list = reflectPrivateMethod("getCountryByName");
46+
assertTrue(list.get(0).getCountryName().equals(getPropertiesValue("countryName")));
47+
}
48+
49+
/*
50+
Here testing when country is not provide
51+
*/
52+
@Test
53+
public void TestPrivateMethod_GetCountryByNameEmptyCountryName() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
54+
setProperties("countryName", "");
55+
var list = reflectPrivateMethod("getCountryByName");
56+
assertTrue(list.isEmpty());
57+
}
58+
59+
/*
60+
Test when server is down throw NullPointer
61+
*/
62+
63+
@Test
64+
public void TestPrivateMethod_GetCountryByNameWhenServerIsDown() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
65+
setProperties("countryName", "");
66+
assertThrows(NullPointerException.class,()-> reflectPrivateMethod("getCountryByName"));
67+
}
68+
69+
/*
70+
Here testing when country is not correct
71+
*/
72+
@Test
73+
public void TestPrivateMethod_GetCountryByNameEmptyIsInvalid() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
74+
setProperties("countryName", "South African Flag");
75+
var list = reflectPrivateMethod("getCountryByName");
76+
assertTrue(list.isEmpty());
77+
}
78+
79+
private List<CountriesEntity> reflectPrivateMethod(String methodName) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
80+
Method reflectedMethodName = CountriesEntity.class.getDeclaredMethod(methodName);
81+
reflectedMethodName.setAccessible(true);
82+
83+
var set = (Set<CountriesEntity>) reflectedMethodName.invoke(countriesEntity);
84+
85+
return set.stream().toList();
86+
}
87+
88+
}

0 commit comments

Comments
 (0)