@@ -5,6 +5,15 @@ import 'package:test/test.dart';
55// Create a mock implementation of the abstract class
66class MockHtKVStorageService extends Mock implements HtKVStorageService {}
77
8+ // Define sample exceptions for testing purposes
9+ const testWriteException = StorageWriteException ('testKey' , 'testValue' );
10+ const testReadException = StorageReadException ('testKey' );
11+ const testDeleteException = StorageDeleteException ('testKey' );
12+ const testClearException = StorageClearException ();
13+ const testTypeMismatchException =
14+ StorageTypeMismatchException ('testKey' , String , int );
15+ const testKeyNotFoundException = StorageKeyNotFoundException ('testKey' );
16+
817void main () {
918 group ('HtKVStorageService Abstract Class' , () {
1019 late MockHtKVStorageService mockStorageService;
@@ -135,5 +144,178 @@ void main() {
135144 await mockStorageService.clearAll ();
136145 verify (() => mockStorageService.clearAll ()).called (1 );
137146 });
147+
148+ // --- Exception Tests ---
149+
150+ test ('writeString throws StorageWriteException on failure' , () async {
151+ when (
152+ () => mockStorageService.writeString (
153+ key: any (named: 'key' ),
154+ value: any (named: 'value' ),
155+ ),
156+ ).thenThrow (testWriteException);
157+
158+ expect (
159+ () =>
160+ mockStorageService.writeString (key: 'testKey' , value: 'testValue' ),
161+ throwsA (isA <StorageWriteException >()),
162+ );
163+ });
164+
165+ test ('readString throws StorageReadException on failure' , () async {
166+ when (() => mockStorageService.readString (key: any (named: 'key' )))
167+ .thenThrow (testReadException);
168+
169+ expect (
170+ () => mockStorageService.readString (key: 'testKey' ),
171+ throwsA (isA <StorageReadException >()),
172+ );
173+ });
174+
175+ test ('readString throws StorageTypeMismatchException on type mismatch' ,
176+ () async {
177+ when (() => mockStorageService.readString (key: any (named: 'key' )))
178+ .thenThrow (testTypeMismatchException);
179+
180+ expect (
181+ () => mockStorageService.readString (key: 'testKey' ),
182+ throwsA (isA <StorageTypeMismatchException >()),
183+ );
184+ });
185+
186+ test ('writeBool throws StorageWriteException on failure' , () async {
187+ when (
188+ () => mockStorageService.writeBool (
189+ key: any (named: 'key' ),
190+ value: any (named: 'value' ),
191+ ),
192+ ).thenThrow (testWriteException);
193+
194+ expect (
195+ () => mockStorageService.writeBool (key: 'testKey' , value: true ),
196+ throwsA (isA <StorageWriteException >()),
197+ );
198+ });
199+
200+ test ('readBool throws StorageReadException on failure' , () async {
201+ when (() => mockStorageService.readBool (key: any (named: 'key' )))
202+ .thenThrow (testReadException);
203+
204+ expect (
205+ () => mockStorageService.readBool (key: 'testKey' ),
206+ throwsA (isA <StorageReadException >()),
207+ );
208+ });
209+
210+ test ('readBool throws StorageTypeMismatchException on type mismatch' ,
211+ () async {
212+ when (() => mockStorageService.readBool (key: any (named: 'key' )))
213+ .thenThrow (testTypeMismatchException);
214+
215+ expect (
216+ () => mockStorageService.readBool (key: 'testKey' ),
217+ throwsA (isA <StorageTypeMismatchException >()),
218+ );
219+ });
220+
221+ test ('writeInt throws StorageWriteException on failure' , () async {
222+ when (
223+ () => mockStorageService.writeInt (
224+ key: any (named: 'key' ),
225+ value: any (named: 'value' ),
226+ ),
227+ ).thenThrow (testWriteException);
228+
229+ expect (
230+ () => mockStorageService.writeInt (key: 'testKey' , value: 1 ),
231+ throwsA (isA <StorageWriteException >()),
232+ );
233+ });
234+
235+ test ('readInt throws StorageReadException on failure' , () async {
236+ when (() => mockStorageService.readInt (key: any (named: 'key' )))
237+ .thenThrow (testReadException);
238+
239+ expect (
240+ () => mockStorageService.readInt (key: 'testKey' ),
241+ throwsA (isA <StorageReadException >()),
242+ );
243+ });
244+
245+ test ('readInt throws StorageTypeMismatchException on type mismatch' ,
246+ () async {
247+ when (() => mockStorageService.readInt (key: any (named: 'key' )))
248+ .thenThrow (testTypeMismatchException);
249+
250+ expect (
251+ () => mockStorageService.readInt (key: 'testKey' ),
252+ throwsA (isA <StorageTypeMismatchException >()),
253+ );
254+ });
255+
256+ test ('writeDouble throws StorageWriteException on failure' , () async {
257+ when (
258+ () => mockStorageService.writeDouble (
259+ key: any (named: 'key' ),
260+ value: any (named: 'value' ),
261+ ),
262+ ).thenThrow (testWriteException);
263+
264+ expect (
265+ () => mockStorageService.writeDouble (key: 'testKey' , value: 1 ),
266+ throwsA (isA <StorageWriteException >()),
267+ );
268+ });
269+
270+ test ('readDouble throws StorageReadException on failure' , () async {
271+ when (() => mockStorageService.readDouble (key: any (named: 'key' )))
272+ .thenThrow (testReadException);
273+
274+ expect (
275+ () => mockStorageService.readDouble (key: 'testKey' ),
276+ throwsA (isA <StorageReadException >()),
277+ );
278+ });
279+
280+ test ('readDouble throws StorageTypeMismatchException on type mismatch' ,
281+ () async {
282+ when (() => mockStorageService.readDouble (key: any (named: 'key' )))
283+ .thenThrow (testTypeMismatchException);
284+
285+ expect (
286+ () => mockStorageService.readDouble (key: 'testKey' ),
287+ throwsA (isA <StorageTypeMismatchException >()),
288+ );
289+ });
290+
291+ test ('delete throws StorageDeleteException on failure' , () async {
292+ when (() => mockStorageService.delete (key: any (named: 'key' )))
293+ .thenThrow (testDeleteException);
294+
295+ expect (
296+ () => mockStorageService.delete (key: 'testKey' ),
297+ throwsA (isA <StorageDeleteException >()),
298+ );
299+ });
300+
301+ test ('delete might throw StorageKeyNotFoundException' , () async {
302+ // Testing the possibility as per documentation comment
303+ when (() => mockStorageService.delete (key: any (named: 'key' )))
304+ .thenThrow (testKeyNotFoundException);
305+
306+ expect (
307+ () => mockStorageService.delete (key: 'testKey' ),
308+ throwsA (isA <StorageKeyNotFoundException >()),
309+ );
310+ });
311+
312+ test ('clearAll throws StorageClearException on failure' , () async {
313+ when (() => mockStorageService.clearAll ()).thenThrow (testClearException);
314+
315+ expect (
316+ () => mockStorageService.clearAll (),
317+ throwsA (isA <StorageClearException >()),
318+ );
319+ });
138320 });
139321}
0 commit comments