Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit45af698

Browse files
committed
Provided missing tests
1 parent6adb27c commit45af698

File tree

3 files changed

+91
-19
lines changed

3 files changed

+91
-19
lines changed

‎caching/src/test/java/com/iluwatar/caching/AppTest.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,15 @@
2525

2626
importorg.junit.jupiter.api.Test;
2727

28-
importjava.io.IOException;
29-
3028
importstaticorg.junit.jupiter.api.Assertions.assertDoesNotThrow;
31-
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
3229

3330
/**
3431
* Tests that Caching example runs without errors.
3532
*/
3633
classAppTest {
37-
3834
/**
3935
* Issue: Add at least one assertion to this test case.
40-
*
36+
* <p>
4137
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
4238
* throws an exception.
4339
*/
@@ -46,12 +42,4 @@ class AppTest {
4642
voidshouldExecuteApplicationWithoutException() {
4743
assertDoesNotThrow(() ->App.main(newString[]{}));
4844
}
49-
50-
@Test
51-
voidexecuteAppWithException(){
52-
assertThrows(
53-
NoClassDefFoundError.class,
54-
() ->App.main(newString[]{"--mongo"})
55-
);
56-
}
5745
}

‎caching/src/test/java/com/iluwatar/caching/CachingTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
packagecom.iluwatar.caching;
2525

26-
importstaticorg.junit.jupiter.api.Assertions.assertNotNull;
27-
2826
importorg.junit.jupiter.api.BeforeEach;
2927
importorg.junit.jupiter.api.Test;
3028

29+
importstaticorg.junit.jupiter.api.Assertions.assertNotNull;
30+
3131
/**
3232
* Application test
3333
*/
@@ -48,25 +48,25 @@ public void setUp() {
4848

4949
@Test
5050
voidtestReadAndWriteThroughStrategy() {
51-
assertNotNull(app);
51+
assertNotNull(app);
5252
app.useReadAndWriteThroughStrategy();
5353
}
5454

5555
@Test
5656
voidtestReadThroughAndWriteAroundStrategy() {
57-
assertNotNull(app);
57+
assertNotNull(app);
5858
app.useReadThroughAndWriteAroundStrategy();
5959
}
6060

6161
@Test
6262
voidtestReadThroughAndWriteBehindStrategy() {
63-
assertNotNull(app);
63+
assertNotNull(app);
6464
app.useReadThroughAndWriteBehindStrategy();
6565
}
6666

6767
@Test
6868
voidtestCacheAsideStrategy() {
69-
assertNotNull(app);
69+
assertNotNull(app);
7070
app.useCacheAsideStategy();
7171
}
7272
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
packagecom.iluwatar.caching.database;
2+
3+
importcom.iluwatar.caching.UserAccount;
4+
importcom.iluwatar.caching.constants.CachingConstants;
5+
importcom.mongodb.client.FindIterable;
6+
importcom.mongodb.client.MongoCollection;
7+
importcom.mongodb.client.MongoDatabase;
8+
importorg.bson.Document;
9+
importorg.junit.jupiter.api.BeforeEach;
10+
importorg.junit.jupiter.api.Test;
11+
importorg.mockito.Mock;
12+
importorg.mockito.internal.util.reflection.Whitebox;
13+
14+
importstaticcom.iluwatar.caching.constants.CachingConstants.ADD_INFO;
15+
importstaticcom.iluwatar.caching.constants.CachingConstants.USER_ID;
16+
importstaticcom.iluwatar.caching.constants.CachingConstants.USER_NAME;
17+
importstaticorg.junit.jupiter.api.Assertions.assertDoesNotThrow;
18+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
19+
importstaticorg.mockito.ArgumentMatchers.any;
20+
importstaticorg.mockito.Mockito.*;
21+
22+
classMongoDbTest {
23+
privatestaticfinalStringID ="123";
24+
privatestaticfinalStringNAME ="Some user";
25+
privatestaticfinalStringADDITIONAL_INFO ="Some app Info";
26+
27+
@Mock
28+
MongoDatabasedb;
29+
privateMongoDbmongoDb =newMongoDb();
30+
31+
privateUserAccountuserAccount;
32+
33+
@BeforeEach
34+
voidinit() {
35+
db =mock(MongoDatabase.class);
36+
Whitebox.setInternalState(mongoDb,"db",db);
37+
userAccount =newUserAccount(ID,NAME,ADDITIONAL_INFO);
38+
39+
40+
}
41+
42+
@Test
43+
voidconnect() {
44+
assertDoesNotThrow(() ->mongoDb.connect());
45+
}
46+
47+
@Test
48+
voidreadFromDb() {
49+
Documentdocument =newDocument(USER_ID,ID)
50+
.append(USER_NAME,NAME)
51+
.append(ADD_INFO,ADDITIONAL_INFO);
52+
MongoCollection<Document>mongoCollection =mock(MongoCollection.class);
53+
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
54+
55+
FindIterable<Document>findIterable =mock(FindIterable.class);
56+
when(mongoCollection.find(any(Document.class))).thenReturn(findIterable);
57+
58+
when(findIterable.first()).thenReturn(document);
59+
60+
assertEquals(mongoDb.readFromDb(ID),userAccount);
61+
}
62+
63+
@Test
64+
voidwriteToDb() {
65+
MongoCollection<Document>mongoCollection =mock(MongoCollection.class);
66+
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
67+
doNothing().when(mongoCollection).insertOne(any(Document.class));
68+
assertDoesNotThrow(()-> {mongoDb.writeToDb(userAccount);});
69+
}
70+
71+
@Test
72+
voidupdateDb() {
73+
MongoCollection<Document>mongoCollection =mock(MongoCollection.class);
74+
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
75+
assertDoesNotThrow(()-> {mongoDb.updateDb(userAccount);});
76+
}
77+
78+
@Test
79+
voidupsertDb() {
80+
MongoCollection<Document>mongoCollection =mock(MongoCollection.class);
81+
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
82+
assertDoesNotThrow(()-> {mongoDb.upsertDb(userAccount);});
83+
}
84+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp