diff --git a/pom.xml b/pom.xml
index 8bfdd36c1d..cc0aca635a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -794,6 +794,7 @@
spring-reactive-modules
spring-swagger-codegen/custom-validations-opeanpi-codegen
testing-modules/testing-assertions
+ testing-modules/mockito-simple
persistence-modules/fauna
persistence-modules/spring-data-rest
@@ -1050,6 +1051,7 @@
spring-reactive-modules
spring-swagger-codegen/custom-validations-opeanpi-codegen
testing-modules/testing-assertions
+ testing-modules/mockito-simple
persistence-modules/fauna
persistence-modules/spring-data-rest
diff --git a/testing-modules/mockito-simple/pom.xml b/testing-modules/mockito-simple/pom.xml
index 29c7966a08..30928cfe35 100644
--- a/testing-modules/mockito-simple/pom.xml
+++ b/testing-modules/mockito-simple/pom.xml
@@ -55,9 +55,8 @@
- 5.3.20
-
- 4.8.0
+ 6.0.8
+ 5.3.1
\ No newline at end of file
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/MessageService.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/MessageService.java
deleted file mode 100644
index d156c59571..0000000000
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/MessageService.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.baeldung.domain.service;
-
-import com.baeldung.domain.model.Message;
-import org.springframework.stereotype.Service;
-
-@Service
-public class MessageService {
-
- public Message deliverMessage (Message message) {
-
- return message;
- }
-}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/MailClient.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/MailClient.java
deleted file mode 100644
index d10169ef7a..0000000000
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/MailClient.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package com.baeldung.junit5.mockito.repository;
-
-import com.baeldung.junit5.mockito.User;
-
-public interface MailClient {
-
- void sendUserRegistrationMail(User user);
-
-}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/UserService.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/UserService.java
deleted file mode 100644
index 326d019b4a..0000000000
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/UserService.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package com.baeldung.junit5.mockito.service;
-
-import com.baeldung.junit5.mockito.User;
-
-public interface UserService {
-
- User register(User user);
-
-}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/AuthenticationStatus.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/AuthenticationStatus.java
index 8307b4323b..0ba8a70021 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/AuthenticationStatus.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/AuthenticationStatus.java
@@ -1,7 +1,5 @@
package com.baeldung.mockito.argumentcaptor;
public enum AuthenticationStatus {
- AUTHENTICATED,
- NOT_AUTHENTICATED,
- ERROR
+ AUTHENTICATED, NOT_AUTHENTICATED, ERROR
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/Credentials.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/Credentials.java
index d5d60bb6fc..598c7c93fa 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/Credentials.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/Credentials.java
@@ -10,5 +10,16 @@ public class Credentials {
this.password = password;
this.key = key;
}
-}
+ public String getName() {
+ return name;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public String getKey() {
+ return key;
+ }
+}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/Format.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/Format.java
index 4c10375159..50363d7c17 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/Format.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/Format.java
@@ -1,6 +1,5 @@
package com.baeldung.mockito.argumentcaptor;
public enum Format {
- TEXT_ONLY,
- HTML
+ TEXT_ONLY, HTML
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/ServiceStatus.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/ServiceStatus.java
index 65def5af64..3951174b30 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/ServiceStatus.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentcaptor/ServiceStatus.java
@@ -1,7 +1,5 @@
package com.baeldung.mockito.argumentcaptor;
public enum ServiceStatus {
- UP,
- DOWN,
- AUTHENTICATED
+ UP, DOWN, AUTHENTICATED
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/api/Flower.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/Flower.java
similarity index 90%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/app/api/Flower.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/Flower.java
index dc1c36e3ff..8bfd0d1b02 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/api/Flower.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/Flower.java
@@ -1,4 +1,4 @@
-package com.baeldung.app.api;
+package com.baeldung.mockito.argumentmatchers;
public class Flower {
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/model/Message.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/Message.java
similarity index 94%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/domain/model/Message.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/Message.java
index a516d5619b..60ddc5ac04 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/model/Message.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/Message.java
@@ -1,4 +1,4 @@
-package com.baeldung.domain.model;
+package com.baeldung.mockito.argumentmatchers;
import java.util.Date;
import java.util.UUID;
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/api/MessageDTO.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/MessageDTO.java
similarity index 90%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/app/api/MessageDTO.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/MessageDTO.java
index 1d9da244b0..7dce0ab75b 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/api/MessageDTO.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/MessageDTO.java
@@ -1,6 +1,7 @@
-package com.baeldung.app.api;
+package com.baeldung.mockito.argumentmatchers;
public class MessageDTO {
+
private String from;
private String to;
private String text;
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/FlowerController.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/controller/FlowerController.java
similarity index 67%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/FlowerController.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/controller/FlowerController.java
index 10f602088c..e0c7cfb62e 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/FlowerController.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/controller/FlowerController.java
@@ -1,13 +1,14 @@
-package com.baeldung.app.rest;
+package com.baeldung.mockito.argumentmatchers.controller;
-import com.baeldung.app.api.Flower;
-import com.baeldung.domain.service.FlowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
+import com.baeldung.mockito.argumentmatchers.Flower;
+import com.baeldung.mockito.argumentmatchers.service.FlowerService;
+
@Controller
@RequestMapping("/flowers")
public class FlowerController {
@@ -16,12 +17,12 @@ public class FlowerController {
private FlowerService flowerService;
@PostMapping("/isAFlower")
- public String isAFlower (@RequestBody String flower) {
+ public String isAFlower(@RequestBody String flower) {
return flowerService.analyze(flower);
}
@PostMapping("/isABigFlower")
- public Boolean isABigFlower (@RequestBody Flower flower) {
+ public Boolean isABigFlower(@RequestBody Flower flower) {
return flowerService.isABigFlower(flower.getName(), flower.getPetals());
}
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/MessageController.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/controller/MessageController.java
similarity index 73%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/MessageController.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/controller/MessageController.java
index 6911197197..4a6cb3f5d6 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/MessageController.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/controller/MessageController.java
@@ -1,14 +1,15 @@
-package com.baeldung.app.rest;
+package com.baeldung.mockito.argumentmatchers.controller;
-import com.baeldung.app.api.MessageDTO;
-import com.baeldung.domain.model.Message;
-import com.baeldung.domain.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
+import com.baeldung.mockito.argumentmatchers.Message;
+import com.baeldung.mockito.argumentmatchers.MessageDTO;
+import com.baeldung.mockito.argumentmatchers.service.MessageService;
+
import java.time.Instant;
import java.util.Date;
import java.util.UUID;
@@ -21,7 +22,7 @@ public class MessageController {
private MessageService messageService;
@PostMapping
- public Message createMessage (@RequestBody MessageDTO messageDTO) {
+ public Message createMessage(@RequestBody MessageDTO messageDTO) {
Message message = new Message();
message.setText(messageDTO.getText());
message.setFrom(messageDTO.getFrom());
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/FlowerService.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/service/FlowerService.java
similarity index 75%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/FlowerService.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/service/FlowerService.java
index 74a9308890..f654638fd3 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/FlowerService.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/service/FlowerService.java
@@ -1,4 +1,4 @@
-package com.baeldung.domain.service;
+package com.baeldung.mockito.argumentmatchers.service;
import org.springframework.stereotype.Service;
@@ -11,15 +11,15 @@ public class FlowerService {
private List flowers = Arrays.asList("Poppy", "Ageratum", "Carnation", "Diascia", "Lantana");
public String analyze(String name) {
- if(flowers.contains(name)) {
+ if (flowers.contains(name)) {
return "flower";
}
return null;
}
public boolean isABigFlower(String name, int petals) {
- if(flowers.contains(name)) {
- if(petals > 10) {
+ if (flowers.contains(name)) {
+ if (petals > 10) {
return true;
}
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/service/MessageService.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/service/MessageService.java
new file mode 100644
index 0000000000..3f9265db0e
--- /dev/null
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/argumentmatchers/service/MessageService.java
@@ -0,0 +1,14 @@
+package com.baeldung.mockito.argumentmatchers.service;
+
+import org.springframework.stereotype.Service;
+
+import com.baeldung.mockito.argumentmatchers.Message;
+
+@Service
+public class MessageService {
+
+ public Message deliverMessage(Message message) {
+
+ return message;
+ }
+}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/User.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/User.java
similarity index 92%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/User.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/User.java
index 4276fd62b9..4457d59b02 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/User.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/User.java
@@ -1,34 +1,39 @@
-package com.baeldung.junit5.mockito;
+package com.baeldung.mockito.junit5;
public class User {
-
+
private Integer id;
private String name;
private int age;
-
+
public User() {
}
-
+
public User(String name, int age) {
this.name = name;
this.age = age;
}
-
+
public Integer getId() {
return id;
}
+
public void setId(Integer id) {
this.id = id;
}
+
public String getName() {
return name;
}
+
public void setName(String name) {
this.name = name;
}
+
public int getAge() {
return age;
}
+
public void setAge(int age) {
this.age = age;
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/MailClient.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/MailClient.java
new file mode 100644
index 0000000000..258de77cd5
--- /dev/null
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/MailClient.java
@@ -0,0 +1,9 @@
+package com.baeldung.mockito.junit5.repository;
+
+import com.baeldung.mockito.junit5.User;
+
+public interface MailClient {
+
+ void sendUserRegistrationMail(User user);
+
+}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/SettingRepository.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/SettingRepository.java
similarity index 65%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/SettingRepository.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/SettingRepository.java
index 23ae8fa35c..094be12770 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/SettingRepository.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/SettingRepository.java
@@ -1,7 +1,7 @@
-package com.baeldung.junit5.mockito.repository;
+package com.baeldung.mockito.junit5.repository;
public interface SettingRepository {
-
+
int getUserMinAge();
int getUserNameMinLength();
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/UserRepository.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/UserRepository.java
similarity index 56%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/UserRepository.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/UserRepository.java
index e1b84c774b..9bc1b0d38c 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/repository/UserRepository.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/repository/UserRepository.java
@@ -1,10 +1,11 @@
-package com.baeldung.junit5.mockito.repository;
+package com.baeldung.mockito.junit5.repository;
-import com.baeldung.junit5.mockito.User;
+import com.baeldung.mockito.junit5.User;
public interface UserRepository {
User insert(User user);
+
boolean isUsernameAlreadyExists(String userName);
-
+
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/DefaultUserService.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/DefaultUserService.java
similarity index 65%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/DefaultUserService.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/DefaultUserService.java
index 888edbd710..bad4e46682 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/DefaultUserService.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/DefaultUserService.java
@@ -1,16 +1,16 @@
-package com.baeldung.junit5.mockito.service;
+package com.baeldung.mockito.junit5.service;
-import com.baeldung.junit5.mockito.User;
-import com.baeldung.junit5.mockito.repository.MailClient;
-import com.baeldung.junit5.mockito.repository.SettingRepository;
-import com.baeldung.junit5.mockito.repository.UserRepository;
+import com.baeldung.mockito.junit5.User;
+import com.baeldung.mockito.junit5.repository.MailClient;
+import com.baeldung.mockito.junit5.repository.SettingRepository;
+import com.baeldung.mockito.junit5.repository.UserRepository;
public class DefaultUserService implements UserService {
-
+
private UserRepository userRepository;
private SettingRepository settingRepository;
private MailClient mailClient;
-
+
public DefaultUserService(UserRepository userRepository, SettingRepository settingRepository, MailClient mailClient) {
this.userRepository = userRepository;
this.settingRepository = settingRepository;
@@ -26,19 +26,20 @@ public class DefaultUserService implements UserService {
}
private void validate(User user) {
- if(user.getName() == null) {
+ if (user.getName() == null) {
throw new RuntimeException(Errors.USER_NAME_REQUIRED);
}
- if(user.getName().length() < settingRepository.getUserNameMinLength()) {
+ if (user.getName()
+ .length() < settingRepository.getUserNameMinLength()) {
throw new RuntimeException(Errors.USER_NAME_SHORT);
}
-
- if(user.getAge() < settingRepository.getUserMinAge()) {
+
+ if (user.getAge() < settingRepository.getUserMinAge()) {
throw new RuntimeException(Errors.USER_AGE_YOUNG);
}
-
- if(userRepository.isUsernameAlreadyExists(user.getName())) {
+
+ if (userRepository.isUsernameAlreadyExists(user.getName())) {
throw new RuntimeException(Errors.USER_NAME_DUPLICATE);
}
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/Errors.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/Errors.java
similarity index 86%
rename from testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/Errors.java
rename to testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/Errors.java
index 28283754a3..8a6882124a 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/junit5/mockito/service/Errors.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/Errors.java
@@ -1,4 +1,4 @@
-package com.baeldung.junit5.mockito.service;
+package com.baeldung.mockito.junit5.service;
public class Errors {
@@ -6,5 +6,5 @@ public class Errors {
public static final String USER_NAME_SHORT = "user.name.short";
public static final String USER_AGE_YOUNG = "user.age.young";
public static final String USER_NAME_DUPLICATE = "user.name.duplicate";
-
+
}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/UserService.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/UserService.java
new file mode 100644
index 0000000000..fe1fbe4f13
--- /dev/null
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/junit5/service/UserService.java
@@ -0,0 +1,9 @@
+package com.baeldung.mockito.junit5.service;
+
+import com.baeldung.mockito.junit5.User;
+
+public interface UserService {
+
+ User register(User user);
+
+}
diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/mockedstatic/StaticUtils.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/mockedstatic/StaticUtils.java
index 2e2f5ead33..5b2e57ae26 100644
--- a/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/mockedstatic/StaticUtils.java
+++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/mockito/mockedstatic/StaticUtils.java
@@ -18,5 +18,4 @@ public class StaticUtils {
public static String name() {
return "Baeldung";
}
-
}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java
deleted file mode 100644
index d611cd9e45..0000000000
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java
+++ /dev/null
@@ -1,94 +0,0 @@
-package com.baeldung.app.rest;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.AdditionalMatchers.or;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.ArgumentMatchers.endsWith;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-import com.baeldung.app.api.Flower;
-import com.baeldung.domain.service.FlowerService;
-
-@ExtendWith(MockitoExtension.class)
-class FlowerControllerUnitTest {
-
- @InjectMocks
- private FlowerController flowerController;
-
- @Mock
- private FlowerService flowerService;
-
- @Test
- void givenPoppyFlower_whenUsingDoReturn_thenCorrect() {
- doReturn("Flower").when(flowerService).analyze("poppy");
-
- String response = flowerController.isAFlower("poppy");
- assertThat(response).isEqualTo("Flower");
- }
-
- @Test
- void givenAnyString_whenUsingArgumentMatcher_thenCorrect() {
- when(flowerService.analyze(anyString())).thenReturn("Flower");
-
- String response = flowerController.isAFlower("violetta");
- assertThat(response).isEqualTo("Flower");
- }
-
- @Test
- void whenIncorrectMatchers_thenThrowsError() {
- assertThrows(InvalidUseOfMatchersException.class, () -> {
- when(flowerService.isABigFlower("poppy", anyInt())).thenReturn(true);
- });
-
- Flower flower = new Flower("poppy", 15);
-
- Boolean response = flowerController.isABigFlower(flower);
- assertThat(response).isFalse();
- }
-
- @Test
- void whenCorrectMatchers_thenCorrect() {
- when(flowerService.isABigFlower(eq("poppy"), anyInt())).thenReturn(true);
-
- Flower flower = new Flower("poppy", 15);
-
- Boolean response = flowerController.isABigFlower(flower);
- assertThat(response).isTrue();
- }
-
- @Test
- void whenUsingMatchersAsReturnValue_thenThrowsError() {
- flowerController.isAFlower("poppy");
-
- String orMatcher = or(eq("poppy"), endsWith("y"));
- assertThrows(InvalidUseOfMatchersException.class, () -> {
- verify(flowerService).analyze(orMatcher);
- });
- }
-
- @Test
- void whenUsingMatchersAsOngoingStubbing_thenCorrect1() {
- flowerController.isAFlower("poppy");
-
- verify(flowerService).analyze(or(eq("poppy"), endsWith("y")));
- }
-
- @Test
- void whenUsingMatchersAsOngoingStubbing_thenCorrect2() {
- flowerController.isAFlower("lily");
-
- verify(flowerService).analyze(or(eq("poppy"), endsWith("y")));
- }
-}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/domain/util/MessageMatcher.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/domain/util/MessageMatcher.java
deleted file mode 100644
index 51db07fde7..0000000000
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/domain/util/MessageMatcher.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package com.baeldung.domain.util;
-
-import com.baeldung.domain.model.Message;
-import org.mockito.ArgumentMatcher;
-
-public class MessageMatcher implements ArgumentMatcher {
-
- private Message left;
-
- public MessageMatcher(Message message) {
- this.left = message;
- }
-
- @Override
- public boolean matches(Message right) {
- return left.getFrom().equals(right.getFrom()) &&
- left.getTo().equals(right.getTo()) &&
- left.getText().equals(right.getText()) &&
- right.getDate() != null &&
- right.getId() != null;
- }
-}
\ No newline at end of file
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/FinalList.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/FinalList.java
index 27b7534978..c81e0bb79b 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/FinalList.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/FinalList.java
@@ -2,9 +2,9 @@ package com.baeldung.mockito;
public final class FinalList extends MyList {
- @Override
- public int size() {
- return 1;
- }
+ @Override
+ public int size() {
+ return 1;
+ }
}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MyDictionary.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MyDictionary.java
index 92a4498696..bb7b076ea6 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MyDictionary.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MyDictionary.java
@@ -3,15 +3,15 @@ package com.baeldung.mockito;
import java.util.HashMap;
import java.util.Map;
-class MyDictionary {
+public class MyDictionary {
private Map wordMap;
- MyDictionary() {
+ public MyDictionary() {
wordMap = new HashMap<>();
}
- MyDictionary(Map wordMap) {
+ public MyDictionary(Map wordMap) {
this.wordMap = wordMap;
}
@@ -19,7 +19,7 @@ class MyDictionary {
wordMap.put(word, meaning);
}
- String getMeaning(final String word) {
+ public String getMeaning(final String word) {
return wordMap.get(word);
}
}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationUnitTest.java
similarity index 89%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationUnitTest.java
index acfd87e047..20c70ce27b 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationUnitTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.mockito;
+package com.baeldung.mockito.annotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doReturn;
@@ -20,6 +20,8 @@ import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
+import com.baeldung.mockito.MyDictionary;
+
@ExtendWith(MockitoExtension.class)
class MockitoAnnotationUnitTest {
@@ -29,19 +31,20 @@ class MockitoAnnotationUnitTest {
@Spy
private List spiedList = new ArrayList<>();
- // Use either @RunWith(MockitoJUnitRunner.class) or manually openMocks in the @Before method
+ // Use either @ExtendWith(MockitoExtension.class) or manually openMocks in the @BeforeEach method
/*
- @Before
+ @BeforeEach
public void init() {
- MockitoAnnotations.openMocks(this);
+ MockitoAnnotations.openMocks(this);
}
*/
-
+
// tests
@Test
void whenNotUseMockAnnotation_thenCorrect() {
- final List mockList = mock(List.class);
+ final List mockList = mock(ArrayList.class);
+
mockList.add("one");
verify(mockList).add("one");
assertEquals(0, mockList.size());
@@ -63,6 +66,7 @@ class MockitoAnnotationUnitTest {
@Test
void whenNotUseSpyAnnotation_thenCorrect() {
final List spyList = spy(new ArrayList());
+
spyList.add("one");
spyList.add("two");
@@ -88,11 +92,12 @@ class MockitoAnnotationUnitTest {
doReturn(100).when(spiedList).size();
assertEquals(100, spiedList.size());
}
-
+
@Test
void whenNotUseCaptorAnnotation_thenCorrect() {
final List mockList = mock(List.class);
final ArgumentCaptor arg = ArgumentCaptor.forClass(String.class);
+
mockList.add("one");
verify(mockList).add(arg.capture());
@@ -100,8 +105,7 @@ class MockitoAnnotationUnitTest {
}
@Captor
- private
- ArgumentCaptor argCaptor;
+ private ArgumentCaptor argCaptor;
@Test
void whenUseCaptorAnnotation_thenTheSame() {
@@ -123,5 +127,4 @@ class MockitoAnnotationUnitTest {
assertEquals("aMeaning", dic.getMeaning("aWord"));
}
-
}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest.java
similarity index 93%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest.java
index 512f538a58..b8dadc6deb 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.mockito;
+package com.baeldung.mockito.annotations;
import org.junit.Rule;
import org.junit.Test;
@@ -6,13 +6,11 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
-
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
-
public class MockitoAnnotationsInitWithMockitoJUnitRuleUnitTest {
@Rule
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsInjectIntoSpyUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsInjectIntoSpyUnitTest.java
similarity index 92%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsInjectIntoSpyUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsInjectIntoSpyUnitTest.java
index 89a253c855..91dd30a20b 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsInjectIntoSpyUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsInjectIntoSpyUnitTest.java
@@ -1,11 +1,12 @@
-package com.baeldung.mockito;
-
+package com.baeldung.mockito.annotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
+import java.util.Map;
+
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -13,8 +14,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.Map;
-
+import com.baeldung.mockito.MyDictionary;
@ExtendWith(MockitoExtension.class)
class MockitoAnnotationsInjectIntoSpyUnitTest {
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsUninitializedUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsUninitializedUnitTest.java
similarity index 91%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsUninitializedUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsUninitializedUnitTest.java
index 10b0aa6626..7c317c6aca 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoAnnotationsUninitializedUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/annotations/MockitoAnnotationsUninitializedUnitTest.java
@@ -1,14 +1,13 @@
-package com.baeldung.mockito;
-
+package com.baeldung.mockito.annotations;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
+import java.util.List;
+
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
-import java.util.List;
-
class MockitoAnnotationsUninitializedUnitTest {
@Mock
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/FlowerControllerUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/FlowerControllerUnitTest.java
new file mode 100644
index 0000000000..e9dcc4a774
--- /dev/null
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/FlowerControllerUnitTest.java
@@ -0,0 +1,80 @@
+package com.baeldung.mockito.argumentmatchers;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.AdditionalMatchers.or;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.endsWith;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import com.baeldung.mockito.argumentmatchers.controller.FlowerController;
+import com.baeldung.mockito.argumentmatchers.service.FlowerService;
+
+@ExtendWith(MockitoExtension.class)
+class FlowerControllerUnitTest {
+
+ @InjectMocks
+ private FlowerController flowerController;
+
+ @Mock
+ private FlowerService flowerService;
+
+ @Test
+ void givenPoppyFlower_whenUsingDoReturn_thenCorrect() {
+ doReturn("Flower").when(flowerService).analyze("poppy");
+
+ String response = flowerController.isAFlower("poppy");
+ assertThat(response).isEqualTo("Flower");
+ }
+
+ @Test
+ void givenAnyFlower_whenUsingArgumentMatcher_thenCorrect() {
+ when(flowerService.analyze(anyString())).thenReturn("Flower");
+
+ String response = flowerController.isAFlower("violetta");
+ assertThat(response).isEqualTo("Flower");
+ }
+
+ @Test
+ void givenIncorrectMatchers_whenUsingArgumentMatchers_thenThrowsError() {
+ assertThrows(InvalidUseOfMatchersException.class,
+ () -> when(flowerService.isABigFlower("poppy", anyInt())).thenReturn(true));
+ }
+
+ @Test
+ void givenCorrectMatchers_whenUsingArgumentMatchers_thenCorrect() {
+ when(flowerService.isABigFlower(eq("poppy"), anyInt())).thenReturn(true);
+
+ Flower flower = new Flower("poppy", 15);
+
+ Boolean response = flowerController.isABigFlower(flower);
+ assertThat(response).isTrue();
+ }
+
+ @Test
+ void givenMatchersOutsideofStubbing_whenUsingMatchersAsReturnValue_thenThrowsError() {
+ flowerController.isAFlower("poppy");
+
+ String orMatcher = or(eq("poppy"), endsWith("y"));
+ assertThrows(InvalidUseOfMatchersException.class,
+ () -> verify(flowerService).analyze(orMatcher));
+ }
+
+ @Test
+ void givenMatchersAsOngoingStubbing_whenUsingMatchers_thenCorrect() {
+ flowerController.isAFlower("poppy");
+
+ verify(flowerService).analyze(or(eq("poppy"), endsWith("y")));
+ }
+}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/MessageControllerUnitTest.java
similarity index 87%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/MessageControllerUnitTest.java
index 84b5bf241e..89548c9a99 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/MessageControllerUnitTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.app.rest;
+package com.baeldung.mockito.argumentmatchers;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
@@ -11,10 +11,8 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import com.baeldung.app.api.MessageDTO;
-import com.baeldung.domain.model.Message;
-import com.baeldung.domain.service.MessageService;
-import com.baeldung.domain.util.MessageMatcher;
+import com.baeldung.mockito.argumentmatchers.controller.MessageController;
+import com.baeldung.mockito.argumentmatchers.service.MessageService;
@ExtendWith(MockitoExtension.class)
class MessageControllerUnitTest {
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/MessageMatcher.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/MessageMatcher.java
new file mode 100644
index 0000000000..8834df6c38
--- /dev/null
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/argumentmatchers/MessageMatcher.java
@@ -0,0 +1,20 @@
+package com.baeldung.mockito.argumentmatchers;
+
+import org.mockito.ArgumentMatcher;
+
+public class MessageMatcher implements ArgumentMatcher {
+
+ private Message left;
+
+ public MessageMatcher(Message message) {
+ this.left = message;
+ }
+
+ @Override
+ public boolean matches(Message right) {
+ return left.getFrom().equals(right.getFrom()) &&
+ left.getTo().equals(right.getTo()) &&
+ left.getText().equals(right.getText()) &&
+ right.getDate() != null && right.getId() != null;
+ }
+}
\ No newline at end of file
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoWhenThenExamplesUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/behaviour/MockitoWhenThenExamplesUnitTest.java
similarity index 72%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoWhenThenExamplesUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/behaviour/MockitoWhenThenExamplesUnitTest.java
index c570629ccd..c4551f43ec 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoWhenThenExamplesUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/behaviour/MockitoWhenThenExamplesUnitTest.java
@@ -1,7 +1,8 @@
-package com.baeldung.mockito;
+package com.baeldung.mockito.behaviour;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
@@ -14,11 +15,12 @@ import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
+import com.baeldung.mockito.MyList;
class MockitoWhenThenExamplesUnitTest {
@Test
- final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() {
+ final void whenSimpleReturnBehaviourConfigured_thenCorrect() {
final MyList listMock = mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);
@@ -27,7 +29,7 @@ class MockitoWhenThenExamplesUnitTest {
}
@Test
- final void whenMockReturnBehaviorIsConfigured2_thenBehaviorIsVerified() {
+ final void whenSimpleReturnBehaviourConfiguredAlternatively_thenCorrect() {
final MyList listMock = mock(MyList.class);
doReturn(false).when(listMock).add(anyString());
@@ -40,55 +42,50 @@ class MockitoWhenThenExamplesUnitTest {
final MyList listMock = mock(MyList.class);
when(listMock.add(anyString())).thenThrow(IllegalStateException.class);
- assertThrows(IllegalStateException.class, () -> {
- listMock.add(randomAlphabetic(6));
- });
-
+ assertThrows(IllegalStateException.class, () -> listMock.add(randomAlphabetic(6)));
}
@Test
- final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingOnlyOnce_thenNoExceptionIsThrown() {
- final MyList listMock = mock(MyList.class);
- when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
-
- listMock.add(randomAlphabetic(6));
- }
-
- @Test
- final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() {
+ final void givenMethodHasNoReturnType_whenCallingMethod_thenExceptionIsThrown() {
final MyList listMock = mock(MyList.class);
doThrow(NullPointerException.class).when(listMock).clear();
- assertThrows(NullPointerException.class, () -> {
- listMock.clear();
- });
-
+ assertThrows(NullPointerException.class, () -> listMock.clear());
}
@Test
final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() {
final MyList listMock = mock(MyList.class);
- when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
+ when(listMock.add(anyString()))
+ .thenReturn(false)
+ .thenThrow(IllegalStateException.class);
assertThrows(IllegalStateException.class, () -> {
listMock.add(randomAlphabetic(6));
listMock.add(randomAlphabetic(6));
});
-
}
@Test
- final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() {
+ final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingOnlyOnce_thenNoExceptionIsThrown() {
+ final MyList listMock = mock(MyList.class);
+ when(listMock.add(anyString()))
+ .thenReturn(false)
+ .thenThrow(IllegalStateException.class);
+
+ assertThatNoException().isThrownBy(() -> listMock.add(randomAlphabetic(6)));
+ }
+
+ @Test
+ final void whenSpyBehaviourConfigured_thenCorrect() {
final MyList instance = new MyList();
final MyList spy = spy(instance);
doThrow(NullPointerException.class).when(spy).size();
- assertThrows(NullPointerException.class, () -> {
- spy.size();
- });
+ assertThrows(NullPointerException.class, () -> spy.size());
}
-
+
@Test
final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMethodIsCalled() {
final MyList listMock = mock(MyList.class);
@@ -96,9 +93,9 @@ class MockitoWhenThenExamplesUnitTest {
assertThat(listMock).hasSize(1);
}
-
+
@Test
- final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenRealMethodIsCalled() {
+ final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenCustomerAnswerIsCalled() {
final MyList listMock = mock(MyList.class);
doAnswer(invocation -> "Always the same").when(listMock).get(anyInt());
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoExceptionUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/exceptions/MockitoExceptionUnitTest.java
similarity index 50%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoExceptionUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/exceptions/MockitoExceptionUnitTest.java
index 843edd8d0a..112a6fd7b6 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoExceptionUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/exceptions/MockitoExceptionUnitTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.mockito;
+package com.baeldung.mockito.exceptions;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
@@ -6,66 +6,53 @@ import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
-import org.mockito.junit.jupiter.MockitoExtension;
-@ExtendWith(MockitoExtension.class)
+import com.baeldung.mockito.MyDictionary;
+
class MockitoExceptionUnitTest {
@Test
- void whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
+ void givenNonVoidReturnType_whenUsingWhenThen_thenExceptionIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
when(dictMock.getMeaning(anyString())).thenThrow(NullPointerException.class);
- assertThrows(NullPointerException.class, () -> {
- dictMock.getMeaning("word");
- });
+ assertThrows(NullPointerException.class, () -> dictMock.getMeaning("word"));
}
@Test
- void whenConfigVoidRetunMethodToThrowEx_thenExIsThrown() {
+ void givenVoidReturnType_whenUsingDoThrow_thenExceptionIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
doThrow(IllegalStateException.class).when(dictMock)
.add(anyString(), anyString());
- assertThrows(IllegalStateException.class, () -> {
- dictMock.add("word", "meaning");
- });
+ assertThrows(IllegalStateException.class, () -> dictMock.add("word", "meaning"));
}
@Test
- void whenConfigNonVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
+ void givenNonVoidReturnType_whenUsingWhenThenAndExeceptionAsNewObject_thenExceptionIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
when(dictMock.getMeaning(anyString())).thenThrow(new NullPointerException("Error occurred"));
- assertThrows(NullPointerException.class, () -> {
- dictMock.getMeaning("word");
- });
+ assertThrows(NullPointerException.class, () -> dictMock.getMeaning("word"));
}
@Test
- void whenConfigVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
+ void givenNonVoidReturnType_whenUsingDoThrowAndExeceptionAsNewObject_thenExceptionIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
- doThrow(new IllegalStateException("Error occurred")).when(dictMock)
- .add(anyString(), anyString());
- assertThrows(IllegalStateException.class, () -> {
- dictMock.add("word", "meaning");
- });
+ doThrow(new IllegalStateException("Error occurred")).when(dictMock).add(anyString(), anyString());
+
+ assertThrows(IllegalStateException.class, () -> dictMock.add("word", "meaning"));
}
- // =====
-
@Test
- void givenSpy_whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
+ void givenSpyAndNonVoidReturnType_whenUsingWhenThen_thenExceptionIsThrown() {
MyDictionary dict = new MyDictionary();
MyDictionary spy = Mockito.spy(dict);
when(spy.getMeaning(anyString())).thenThrow(NullPointerException.class);
- assertThrows(NullPointerException.class, () -> {
- spy.getMeaning("word");
- });
+
+ assertThrows(NullPointerException.class, () -> spy.getMeaning("word"));
}
}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/junit5/UserServiceUnitTest.java
similarity index 88%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/junit5/UserServiceUnitTest.java
index cc38d617b7..45eb42a00f 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/junit5/UserServiceUnitTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.junit5.mockito;
+package com.baeldung.mockito.junit5;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -18,12 +18,12 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
-import com.baeldung.junit5.mockito.repository.MailClient;
-import com.baeldung.junit5.mockito.repository.SettingRepository;
-import com.baeldung.junit5.mockito.repository.UserRepository;
-import com.baeldung.junit5.mockito.service.DefaultUserService;
-import com.baeldung.junit5.mockito.service.Errors;
-import com.baeldung.junit5.mockito.service.UserService;
+import com.baeldung.mockito.junit5.repository.MailClient;
+import com.baeldung.mockito.junit5.repository.SettingRepository;
+import com.baeldung.mockito.junit5.repository.UserRepository;
+import com.baeldung.mockito.junit5.service.DefaultUserService;
+import com.baeldung.mockito.junit5.service.Errors;
+import com.baeldung.mockito.junit5.service.UserService;
@ExtendWith(MockitoExtension.class)
class UserServiceUnitTest {
@@ -43,11 +43,14 @@ class UserServiceUnitTest {
@BeforeEach
void init(@Mock SettingRepository settingRepository) {
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
- lenient().when(settingRepository.getUserMinAge())
- .thenReturn(10);
+
+ lenient().when(settingRepository.getUserMinAge()).thenReturn(10);
+
when(settingRepository.getUserNameMinLength()).thenReturn(4);
+
lenient().when(userRepository.isUsernameAlreadyExists(any(String.class)))
.thenReturn(false);
+
this.settingRepository = settingRepository;
}
@@ -77,6 +80,8 @@ class UserServiceUnitTest {
verify(mailClient).sendUserRegistrationMail(insertedUser);
}
+ // additional tests
+
@Test
void givenShortName_whenSaveUser_thenGiveShortUsernameError() {
// Given
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockFinalsUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockfinal/MockFinalsUnitTest.java
similarity index 70%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockFinalsUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockfinal/MockFinalsUnitTest.java
index 8abd51bb85..6041ee1a9b 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockFinalsUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockfinal/MockFinalsUnitTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.mockito;
+package com.baeldung.mockito.mockfinal;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@@ -8,28 +8,27 @@ import static org.mockito.Mockito.withSettings;
import org.junit.jupiter.api.Test;
import org.mockito.MockMakers;
+import com.baeldung.mockito.FinalList;
+import com.baeldung.mockito.MyList;
+
class MockFinalsUnitTest {
@Test
- void whenMockFinalMethodMockWorks() {
-
- MyList myList = new MyList();
+ void whenMockFinalMethod_thenMockWorks() {
MyList mock = mock(MyList.class);
when(mock.finalMethod()).thenReturn(1);
- assertThat(mock.finalMethod()).isNotEqualTo(myList.finalMethod());
+ assertThat(mock.finalMethod()).isNotZero();
}
@Test
- public void whenMockFinalClassMockWorks() {
-
- FinalList finalList = new FinalList();
+ void whenMockFinalClass_thenMockWorks() {
FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2);
- assertThat(mock.size()).isNotEqualTo(finalList.size());
+ assertThat(mock.size()).isNotEqualTo(1);
}
@Test
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockmethods/MockitoMockMethodsUnitTest.java
similarity index 86%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockmethods/MockitoMockMethodsUnitTest.java
index 6e9167f0d0..696208c9ec 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockmethods/MockitoMockMethodsUnitTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.mockito;
+package com.baeldung.mockito.mockmethods;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
@@ -16,36 +16,41 @@ import org.mockito.exceptions.verification.TooFewActualInvocations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
-class MockitoMockUnitTest {
+import com.baeldung.mockito.MyList;
+
+class MockitoMockMethodsUnitTest {
@Test
void whenUsingSimpleMock_thenCorrect() {
MyList listMock = mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);
+
boolean added = listMock.add(randomAlphabetic(6));
- verify(listMock).add(anyString());
assertThat(added).isFalse();
+ verify(listMock).add(anyString());
}
@Test
- void whenUsingMockWithName_thenCorrect() {
+ void givenFewActualInvocationThanConfigured_whenUsingMockWithName_thenExceptionIsThrown() {
MyList listMock = mock(MyList.class, "myMock");
+
when(listMock.add(anyString())).thenReturn(false);
listMock.add(randomAlphabetic(6));
assertThatThrownBy(() -> verify(listMock, times(2)).add(anyString()))
- .isInstanceOf(TooFewActualInvocations.class)
- .hasMessageContaining("myMock.add");
+ .isInstanceOf(TooFewActualInvocations.class)
+ .hasMessageContaining("myMock.add");
}
private static class CustomAnswer implements Answer {
+
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return false;
}
}
-
+
@Test
void whenUsingMockWithAnswer_thenCorrect() {
MyList listMock = mock(MyList.class, new CustomAnswer());
@@ -59,6 +64,7 @@ class MockitoMockUnitTest {
void whenUsingMockWithSettings_thenCorrect() {
MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer());
MyList listMock = mock(MyList.class, customSettings);
+
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockedstatic/MockedStaticUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockstatic/MockStaticUnitTest.java
similarity index 90%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockedstatic/MockedStaticUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockstatic/MockStaticUnitTest.java
index 309f066a87..b9268f3c6a 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockedstatic/MockedStaticUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/mockstatic/MockStaticUnitTest.java
@@ -1,14 +1,16 @@
-package com.baeldung.mockito.mockedstatic;
+package com.baeldung.mockito.mockstatic;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
+import com.baeldung.mockito.mockedstatic.StaticUtils;
+
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.mockStatic;
import java.util.Arrays;
-class MockedStaticUnitTest {
+class MockStaticUnitTest {
@Test
void givenStaticMethodWithNoArgs_whenMocked_thenReturnsMockSuccessfully() {
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingMockOrSpyUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingMockOrSpyUnitTest.java
index e1023c5e57..0a510ba6cb 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingMockOrSpyUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingMockOrSpyUnitTest.java
@@ -18,14 +18,14 @@ class MockitoMisusingMockOrSpyUnitTest {
List list = new ArrayList();
assertThatThrownBy(() -> doReturn(100).when(list).size())
- .isInstanceOf(NotAMockException.class)
- .hasMessageContaining("Argument passed to when() is not a mock!");
+ .isInstanceOf(NotAMockException.class)
+ .hasMessageContaining("Argument passed to when() is not a mock!");
}
-
+
@Test
void givenASpy_whenDoReturn_thenNoError() {
final List spyList = spy(new ArrayList<>());
-
+
assertThatNoException().isThrownBy(() -> doReturn(100).when(spyList).size());
}
}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoSpyUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoSpyUnitTest.java
index caef0c3b4b..400de07886 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoSpyUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoSpyUnitTest.java
@@ -16,10 +16,10 @@ import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
-public class MockitoSpyUnitTest {
+class MockitoSpyUnitTest {
@Test
- void whenSpyingOnList_thenCorrect() {
+ void givenUsingSpyMethod_whenSpyingOnList_thenCorrect() {
final List list = new ArrayList();
final List spyList = spy(list);
@@ -36,7 +36,7 @@ public class MockitoSpyUnitTest {
private List aSpyList = new ArrayList();
@Test
- void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
+ void givenUsingSpyAnnotation_whenSpyingOnList_thenCorrect() {
aSpyList.add("one");
aSpyList.add("two");
@@ -47,13 +47,14 @@ public class MockitoSpyUnitTest {
}
@Test
- void whenStubASpy_thenStubbed() {
+ void givenASpy_whenStubbingTheBehaviour_thenCorrect() {
final List list = new ArrayList();
final List spyList = spy(list);
assertEquals(0, spyList.size());
doReturn(100).when(spyList).size();
+
assertThat(spyList).hasSize(100);
}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoVerifyExamplesUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/verify/MockitoVerifyExamplesUnitTest.java
similarity index 73%
rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoVerifyExamplesUnitTest.java
rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/verify/MockitoVerifyExamplesUnitTest.java
index a236fc7cfc..902bad33f5 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoVerifyExamplesUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/verify/MockitoVerifyExamplesUnitTest.java
@@ -1,4 +1,4 @@
-package com.baeldung.mockito;
+package com.baeldung.mockito.verify;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -20,34 +20,33 @@ import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.exceptions.verification.NoInteractionsWanted;
+import com.baeldung.mockito.MyList;
import com.google.common.collect.Lists;
class MockitoVerifyExamplesUnitTest {
- // tests
-
@Test
- final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() {
+ final void givenInteractionWithMock_whenVerifyingInteraction_thenCorrect() {
final List mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList).size();
}
@Test
- final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() {
+ final void givenOneInteractionWithMock_whenVerifyingNumberOfInteractions_thenCorrect() {
final List mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList, times(1)).size();
}
@Test
- final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() {
+ final void givenNoInteractionWithWholeMock_whenVerifyingInteractions_thenCorrect() {
final List mockedList = mock(MyList.class);
verifyNoInteractions(mockedList);
}
@Test
- final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() {
+ final void givenNoInteractionWithSpecificMethod_whenVerifyingInteractions_thenCorrect() {
final List mockedList = mock(MyList.class);
verify(mockedList, times(0)).size();
}
@@ -59,14 +58,12 @@ class MockitoVerifyExamplesUnitTest {
mockedList.clear();
verify(mockedList).size();
- assertThrows(NoInteractionsWanted.class, () -> {
- verifyNoMoreInteractions(mockedList);
- });
+ assertThrows(NoInteractionsWanted.class, () -> verifyNoMoreInteractions(mockedList));
}
@Test
- final void whenVerifyingOrderOfInteractions_thenCorrect() {
+ final void givenInteractionsInOrder_whenVerifyingOrderOfInteractions_thenCorrect() {
final List mockedList = mock(MyList.class);
mockedList.size();
mockedList.add("a parameter");
@@ -79,7 +76,7 @@ class MockitoVerifyExamplesUnitTest {
}
@Test
- final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() {
+ final void givenNoInteraction_whenVerifyingAnInteraction_thenCorrect() {
final List mockedList = mock(MyList.class);
mockedList.size();
@@ -87,7 +84,7 @@ class MockitoVerifyExamplesUnitTest {
}
@Test
- final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() {
+ final void givenInteractionAtLeastOnce_whenVerifyingAnInteraction_thenCorrect() {
final List mockedList = mock(MyList.class);
mockedList.clear();
mockedList.clear();
@@ -100,7 +97,7 @@ class MockitoVerifyExamplesUnitTest {
// with arguments
@Test
- final void whenVerifyingAnInteractionWithExactArgument_thenCorrect() {
+ final void givenInteractionWithExactArgument_whenVerifyingAnInteraction_thenCorrect() {
final List mockedList = mock(MyList.class);
mockedList.add("test");
@@ -108,7 +105,7 @@ class MockitoVerifyExamplesUnitTest {
}
@Test
- final void whenVerifyingAnInteractionWithAnyArgument_thenCorrect() {
+ final void givenInteractionWithAnyArgument_whenVerifyingAnInteraction_thenCorrect() {
final List mockedList = mock(MyList.class);
mockedList.add("test");
@@ -116,15 +113,14 @@ class MockitoVerifyExamplesUnitTest {
}
@Test
- final void whenVerifyingAnInteractionWithArgumentCapture_thenCorrect() {
+ final void givenInteraction_whenVerifyingAnInteractionWithArgumentCapture_thenCorrect() {
final List mockedList = mock(MyList.class);
- mockedList.addAll(Lists.newArrayList("someElement"));
-
+ mockedList.addAll(Lists. newArrayList("someElement"));
+
final ArgumentCaptor> argumentCaptor = ArgumentCaptor.forClass(List.class);
verify(mockedList).addAll(argumentCaptor.capture());
-
+
final List capturedArgument = argumentCaptor.getValue();
assertThat(capturedArgument).contains("someElement");
}
-
}
diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/voidmethods/MockitoVoidMethodsUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/voidmethods/MockitoVoidMethodsUnitTest.java
index 9b8a713a15..ee8c36435a 100644
--- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/voidmethods/MockitoVoidMethodsUnitTest.java
+++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/voidmethods/MockitoVoidMethodsUnitTest.java
@@ -1,6 +1,5 @@
package com.baeldung.mockito.voidmethods;
-
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
@@ -15,17 +14,23 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
-import org.mockito.junit.jupiter.MockitoExtension;
import com.baeldung.mockito.MyList;
-@ExtendWith(MockitoExtension.class)
class MockitoVoidMethodsUnitTest {
@Test
- void whenAddCalledVerified() {
+ void whenAddCalled_thenVerified() {
+ MyList myList = mock(MyList.class);
+ doNothing().when(myList).add(isA(Integer.class), isA(String.class));
+ myList.add(0, "");
+
+ verify(myList, times(1)).add(0, "");
+ }
+
+ @Test
+ void whenAddCalled_thenVerified2() {
MyList myList = mock(MyList.class);
myList.add(0, "");
@@ -33,8 +38,9 @@ class MockitoVoidMethodsUnitTest {
}
@Test
- void givenNull_addThrows() {
+ void givenNull_whenAddCalled_thenThrowsException() {
MyList myList = mock(MyList.class);
+
assertThrows(Exception.class, () -> {
doThrow().when(myList).add(isA(Integer.class), isNull());
});
@@ -43,36 +49,42 @@ class MockitoVoidMethodsUnitTest {
}
@Test
- void whenAddCalledValueCaptured() {
+ void givenArgumentCaptor_whenAddCalled_thenValueCaptured() {
MyList myList = mock(MyList.class);
+
ArgumentCaptor valueCapture = ArgumentCaptor.forClass(String.class);
doNothing().when(myList).add(any(Integer.class), valueCapture.capture());
+
myList.add(0, "captured");
assertEquals("captured", valueCapture.getValue());
}
@Test
- void whenAddCalledAnswered() {
+ void givenDoAnswer_whenAddCalled_thenAnswered() {
MyList myList = mock(MyList.class);
+
doAnswer(invocation -> {
Object arg0 = invocation.getArgument(0);
Object arg1 = invocation.getArgument(1);
- //do something with the arguments here
assertEquals(3, arg0);
assertEquals("answer me", arg1);
return null;
- }).when(myList).add(any(Integer.class), any(String.class));
+ }).when(myList)
+ .add(any(Integer.class), any(String.class));
+
myList.add(3, "answer me");
}
@Test
- void whenAddCalledRealMethodCalled() {
+ void givenDoCallRealMethod_whenAddCalled_thenRealMethodCalled() {
MyList myList = mock(MyList.class);
- doCallRealMethod().when(myList).add(any(Integer.class), any(String.class));
+
+ doCallRealMethod().when(myList)
+ .add(any(Integer.class), any(String.class));
myList.add(1, "real");
-
+
verify(myList, times(1)).add(1, "real");
}
}
diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml
index 8fb7a93a02..a6b5fba570 100644
--- a/testing-modules/pom.xml
+++ b/testing-modules/pom.xml
@@ -31,7 +31,7 @@
junit5-migration
load-testing-comparison
mockito
- mockito-simple
+
mocks
mocks-2
mockserver