-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostcodeServiceTest.java
106 lines (80 loc) · 3.41 KB
/
PostcodeServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.nizam.geo.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import com.nizam.geo.entity.PostcodeEntity;
import com.nizam.geo.repository.PostcodeRepository;
public class PostcodeServiceTest {
@Mock
private PostcodeRepository postcodeRepository;
@InjectMocks
private PostcodeService postcodeService;
@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}
@Test
public void testFindByPostcode() {
String postcode = "ABC 123";
PostcodeEntity mockEntity = new PostcodeEntity();
mockEntity.setPostcode(postcode);
mockEntity.setLatitude(50.2125650);
mockEntity.setLongitude(-5.2977680);
when(postcodeRepository.findByPostcode(postcode)).thenReturn(mockEntity);
PostcodeEntity result = postcodeService.findByPostcode(postcode);
assertEquals(mockEntity, result);
}
@Test
public void testGetAllPostcodes() {
PostcodeEntity mockEntity1 = new PostcodeEntity();
mockEntity1.setPostcode("ABC 123");
mockEntity1.setLatitude(50.2125650);
mockEntity1.setLongitude(-5.2977680);
PostcodeEntity mockEntity2 = new PostcodeEntity();
mockEntity1.setPostcode("XYZ 789");
mockEntity1.setLatitude(51.2125650);
mockEntity1.setLongitude(-6.2977680);
List<PostcodeEntity> mockEntities = Arrays.asList(mockEntity1, mockEntity2);
when(postcodeRepository.findAll()).thenReturn(mockEntities);
List<PostcodeEntity> result = postcodeService.getAllPostcodes();
assertEquals(2, result.size());
assertEquals(mockEntities.get(0), result.get(0));
}
@Test
public void testUpdatePostcodeCoordinates_EntityExists() {
String postcode = "ABC 123";
double latitude = 50.2125650;
double longitude = -5.2977680;
PostcodeEntity mockEntity = new PostcodeEntity(); // Existing entity
mockEntity.setPostcode(postcode);
mockEntity.setLatitude(latitude);
mockEntity.setLongitude(longitude);
when(postcodeRepository.findByPostcode(postcode)).thenReturn(mockEntity);
double newLatitude = 50.50;
double newLongitude = -5.50;
postcodeService.updatePostcodeCoordinates(postcode, newLatitude, newLongitude);
verify(postcodeRepository, times(1)).save(mockEntity);
assertEquals(newLatitude, mockEntity.getLatitude());
assertEquals(newLongitude, mockEntity.getLongitude());
}
@Test
public void testUpdatePostcodeCoordinates_EntityNotFound() {
String postcode = "99999"; // Assuming this postcode does not exist
double latitude = 41.0;
double longitude = -72.0;
when(postcodeRepository.findByPostcode(postcode)).thenReturn(null);
postcodeService.updatePostcodeCoordinates(postcode, latitude, longitude);
verify(postcodeRepository, never()).save(any());
}
}