Skip to content

Commit 45af698

Browse files
committed
Provided missing tests
1 parent 6adb27c commit 45af698

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
import org.junit.jupiter.api.Test;
2727

28-
import java.io.IOException;
29-
3028
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
31-
import static org.junit.jupiter.api.Assertions.assertThrows;
3229

3330
/**
3431
* Tests that Caching example runs without errors.
3532
*/
3633
class AppTest {
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
void shouldExecuteApplicationWithoutException() {
4743
assertDoesNotThrow(() -> App.main(new String[]{}));
4844
}
49-
50-
@Test
51-
void executeAppWithException(){
52-
assertThrows(
53-
NoClassDefFoundError.class,
54-
() -> App.main(new String[]{"--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
package com.iluwatar.caching;
2525

26-
import static org.junit.jupiter.api.Assertions.assertNotNull;
27-
2826
import org.junit.jupiter.api.BeforeEach;
2927
import org.junit.jupiter.api.Test;
3028

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

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

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

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

6767
@Test
6868
void testCacheAsideStrategy() {
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+
package com.iluwatar.caching.database;
2+
3+
import com.iluwatar.caching.UserAccount;
4+
import com.iluwatar.caching.constants.CachingConstants;
5+
import com.mongodb.client.FindIterable;
6+
import com.mongodb.client.MongoCollection;
7+
import com.mongodb.client.MongoDatabase;
8+
import org.bson.Document;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.mockito.Mock;
12+
import org.mockito.internal.util.reflection.Whitebox;
13+
14+
import static com.iluwatar.caching.constants.CachingConstants.ADD_INFO;
15+
import static com.iluwatar.caching.constants.CachingConstants.USER_ID;
16+
import static com.iluwatar.caching.constants.CachingConstants.USER_NAME;
17+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.Mockito.*;
21+
22+
class MongoDbTest {
23+
private static final String ID = "123";
24+
private static final String NAME = "Some user";
25+
private static final String ADDITIONAL_INFO = "Some app Info";
26+
27+
@Mock
28+
MongoDatabase db;
29+
private MongoDb mongoDb = new MongoDb();
30+
31+
private UserAccount userAccount;
32+
33+
@BeforeEach
34+
void init() {
35+
db = mock(MongoDatabase.class);
36+
Whitebox.setInternalState(mongoDb, "db", db);
37+
userAccount = new UserAccount(ID, NAME, ADDITIONAL_INFO);
38+
39+
40+
}
41+
42+
@Test
43+
void connect() {
44+
assertDoesNotThrow(() -> mongoDb.connect());
45+
}
46+
47+
@Test
48+
void readFromDb() {
49+
Document document = new Document(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+
void writeToDb() {
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+
void updateDb() {
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+
void upsertDb() {
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)