From 45af6987c10828802e5f40dd3ee3a9f2d0cc0bb1 Mon Sep 17 00:00:00 2001 From: Victor Zalevskii Date: Thu, 2 Sep 2021 15:30:07 +0300 Subject: [PATCH] Provided missing tests --- .../java/com/iluwatar/caching/AppTest.java | 14 +--- .../com/iluwatar/caching/CachingTest.java | 12 +-- .../caching/database/MongoDbTest.java | 84 +++++++++++++++++++ 3 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 caching/src/test/java/com/iluwatar/caching/database/MongoDbTest.java diff --git a/caching/src/test/java/com/iluwatar/caching/AppTest.java b/caching/src/test/java/com/iluwatar/caching/AppTest.java index f83eefa0e..a50b687c2 100644 --- a/caching/src/test/java/com/iluwatar/caching/AppTest.java +++ b/caching/src/test/java/com/iluwatar/caching/AppTest.java @@ -25,19 +25,15 @@ package com.iluwatar.caching; import org.junit.jupiter.api.Test; -import java.io.IOException; - import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; /** * Tests that Caching example runs without errors. */ class AppTest { - /** * Issue: Add at least one assertion to this test case. - * + *

* Solution: Inserted assertion to check whether the execution of the main method in {@link App} * throws an exception. */ @@ -46,12 +42,4 @@ class AppTest { void shouldExecuteApplicationWithoutException() { assertDoesNotThrow(() -> App.main(new String[]{})); } - - @Test - void executeAppWithException(){ - assertThrows( - NoClassDefFoundError.class, - () -> App.main(new String[]{"--mongo"}) - ); - } } diff --git a/caching/src/test/java/com/iluwatar/caching/CachingTest.java b/caching/src/test/java/com/iluwatar/caching/CachingTest.java index 8a02869bc..0c70e24de 100644 --- a/caching/src/test/java/com/iluwatar/caching/CachingTest.java +++ b/caching/src/test/java/com/iluwatar/caching/CachingTest.java @@ -23,11 +23,11 @@ package com.iluwatar.caching; -import static org.junit.jupiter.api.Assertions.assertNotNull; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertNotNull; + /** * Application test */ @@ -48,25 +48,25 @@ class CachingTest { @Test void testReadAndWriteThroughStrategy() { - assertNotNull(app); + assertNotNull(app); app.useReadAndWriteThroughStrategy(); } @Test void testReadThroughAndWriteAroundStrategy() { - assertNotNull(app); + assertNotNull(app); app.useReadThroughAndWriteAroundStrategy(); } @Test void testReadThroughAndWriteBehindStrategy() { - assertNotNull(app); + assertNotNull(app); app.useReadThroughAndWriteBehindStrategy(); } @Test void testCacheAsideStrategy() { - assertNotNull(app); + assertNotNull(app); app.useCacheAsideStategy(); } } diff --git a/caching/src/test/java/com/iluwatar/caching/database/MongoDbTest.java b/caching/src/test/java/com/iluwatar/caching/database/MongoDbTest.java new file mode 100644 index 000000000..cfb2b718f --- /dev/null +++ b/caching/src/test/java/com/iluwatar/caching/database/MongoDbTest.java @@ -0,0 +1,84 @@ +package com.iluwatar.caching.database; + +import com.iluwatar.caching.UserAccount; +import com.iluwatar.caching.constants.CachingConstants; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.internal.util.reflection.Whitebox; + +import static com.iluwatar.caching.constants.CachingConstants.ADD_INFO; +import static com.iluwatar.caching.constants.CachingConstants.USER_ID; +import static com.iluwatar.caching.constants.CachingConstants.USER_NAME; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +class MongoDbTest { + private static final String ID = "123"; + private static final String NAME = "Some user"; + private static final String ADDITIONAL_INFO = "Some app Info"; + + @Mock + MongoDatabase db; + private MongoDb mongoDb = new MongoDb(); + + private UserAccount userAccount; + + @BeforeEach + void init() { + db = mock(MongoDatabase.class); + Whitebox.setInternalState(mongoDb, "db", db); + userAccount = new UserAccount(ID, NAME, ADDITIONAL_INFO); + + + } + + @Test + void connect() { + assertDoesNotThrow(() -> mongoDb.connect()); + } + + @Test + void readFromDb() { + Document document = new Document(USER_ID, ID) + .append(USER_NAME, NAME) + .append(ADD_INFO, ADDITIONAL_INFO); + MongoCollection mongoCollection = mock(MongoCollection.class); + when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection); + + FindIterable findIterable = mock(FindIterable.class); + when(mongoCollection.find(any(Document.class))).thenReturn(findIterable); + + when(findIterable.first()).thenReturn(document); + + assertEquals(mongoDb.readFromDb(ID),userAccount); + } + + @Test + void writeToDb() { + MongoCollection mongoCollection = mock(MongoCollection.class); + when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection); + doNothing().when(mongoCollection).insertOne(any(Document.class)); + assertDoesNotThrow(()-> {mongoDb.writeToDb(userAccount);}); + } + + @Test + void updateDb() { + MongoCollection mongoCollection = mock(MongoCollection.class); + when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection); + assertDoesNotThrow(()-> {mongoDb.updateDb(userAccount);}); + } + + @Test + void upsertDb() { + MongoCollection mongoCollection = mock(MongoCollection.class); + when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection); + assertDoesNotThrow(()-> {mongoDb.upsertDb(userAccount);}); + } +} \ No newline at end of file