From 75046a3f3268fc2aa9ee490d4689f2b61f4b63c0 Mon Sep 17 00:00:00 2001 From: Shahul Basha <32072554+shahulbasha@users.noreply.github.com> Date: Tue, 5 Sep 2023 20:41:18 -0400 Subject: [PATCH] BAEL-6830 (#14722) * BAEL-6830 initial commit * BAEL-6830 clean up code --- .../java/com/baeldung/constructor/User.java | 18 ++++++ .../com/baeldung/constructor/UserService.java | 18 ++++++ .../constructor/UserServiceUnitTest.java | 55 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/constructor/User.java create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/constructor/UserService.java create mode 100644 testing-modules/mockito-2/src/test/java/com/baeldung/constructor/UserServiceUnitTest.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/constructor/User.java b/testing-modules/mockito-2/src/main/java/com/baeldung/constructor/User.java new file mode 100644 index 0000000000..ab4070044f --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/constructor/User.java @@ -0,0 +1,18 @@ +package com.baeldung.constructor; + +public class User { + + private final UserService userService; + + public User() { + this.userService = new UserService(); + } + + public User(String userName) { + this.userService = new UserService(userName); + } + + public String getUserName(){ + return userService.getUserName(); + } +} diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/constructor/UserService.java b/testing-modules/mockito-2/src/main/java/com/baeldung/constructor/UserService.java new file mode 100644 index 0000000000..bd5fcfdbda --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/constructor/UserService.java @@ -0,0 +1,18 @@ +package com.baeldung.constructor; + +public class UserService { + + private final String userName; + + public UserService(String userName) { + this.userName = userName; + } + + public UserService() { + this.userName = "Unknown"; + } + + public String getUserName(){ + return this.userName; + } +} diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/constructor/UserServiceUnitTest.java b/testing-modules/mockito-2/src/test/java/com/baeldung/constructor/UserServiceUnitTest.java new file mode 100644 index 0000000000..9683226609 --- /dev/null +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/constructor/UserServiceUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.constructor; + +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; + +class UserServiceUnitTest { + + @Test + void whenConstructorInvokedWithInitializer_ThenMockObjectShouldBeCreated(){ + try(MockedConstruction mockUserService = Mockito.mockConstruction(UserService.class,(mock,context)-> when(mock.getUserName()).thenReturn("John Doe"))){ + User user = new User(); + Assertions.assertEquals(1,mockUserService.constructed().size()); + Assertions.assertEquals("John Doe",user.getUserName()); + } + } + + @Test + void whenConstructorInvokedWithoutInitializer_ThenMockObjectShouldBeCreatedWithNullFields(){ + try(MockedConstruction mockUserService = Mockito.mockConstruction(UserService.class)){ + User user = new User(); + Assertions.assertEquals(1,mockUserService.constructed().size()); + Assertions.assertNull(user.getUserName()); + } + } + + @Test + void whenConstructorInvokedWithParameters_ThenMockObjectShouldBeCreated(){ + try(MockedConstruction mockUserService = Mockito.mockConstruction(UserService.class,(mock, context) -> when(mock.getUserName()).thenReturn("John Doe"))){ + User user = new User("Mike"); + Assertions.assertEquals(1,mockUserService.constructed().size()); + Assertions.assertEquals("John Doe",user.getUserName()); + } + } + + @Test + void whenMultipleConstructorsInvoked_ThenMultipleMockObjectsShouldBeCreated(){ + try(MockedConstruction mockUserService = Mockito.mockConstruction(UserService.class)){ + User user = new User(); + User secondUser = new User(); + User thirdUser = new User("Mike"); + + when(mockUserService.constructed().get(0).getUserName()).thenReturn("John Doe"); + when(mockUserService.constructed().get(1).getUserName()).thenReturn("Steve Smith"); + + Assertions.assertEquals(3,mockUserService.constructed().size()); + Assertions.assertEquals("John Doe",user.getUserName()); + Assertions.assertEquals("Steve Smith",secondUser.getUserName()); + Assertions.assertNull(thirdUser.getUserName()); + } + } +}