BAEL-6830 (#14742)
* BAEL-6830 example changed based on editor review comments * BAEL-6830 example changed based on editor review comments * BAEL-6830 example changed based on editor review comments
This commit is contained in:
parent
fa5eb584ed
commit
f3069c6ebc
@ -0,0 +1,22 @@
|
||||
package com.baeldung.constructor;
|
||||
|
||||
public class PaymentProcessor {
|
||||
|
||||
private final PaymentService paymentService;
|
||||
|
||||
public PaymentProcessor(PaymentService paymentService) {
|
||||
this.paymentService = paymentService;
|
||||
}
|
||||
|
||||
public PaymentProcessor() {
|
||||
this.paymentService = new PaymentService();
|
||||
}
|
||||
|
||||
public PaymentProcessor(String paymentMode) {
|
||||
this.paymentService = new PaymentService(paymentMode);
|
||||
}
|
||||
|
||||
public String processPayment(){
|
||||
return paymentService.processPayment();
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.constructor;
|
||||
|
||||
public class PaymentService {
|
||||
|
||||
private final String paymentMode;
|
||||
|
||||
public PaymentService(String paymentMode) {
|
||||
this.paymentMode = paymentMode;
|
||||
}
|
||||
|
||||
public PaymentService() {
|
||||
this.paymentMode = "Cash";
|
||||
}
|
||||
|
||||
public String processPayment(){
|
||||
// simulate processing payment and returns the payment mode
|
||||
return this.paymentMode;
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
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 PaymentServiceUnitTest {
|
||||
|
||||
@Test
|
||||
void whenConstructorInvokedWithInitializer_ThenMockObjectShouldBeCreated(){
|
||||
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class,(mock,context)-> when(mock.processPayment()).thenReturn("Credit"))){
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor();
|
||||
Assertions.assertEquals(1,mockPaymentService.constructed().size());
|
||||
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenConstructorInvokedWithoutInitializer_ThenMockObjectShouldBeCreatedWithNullFields(){
|
||||
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class)){
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor();
|
||||
Assertions.assertEquals(1,mockPaymentService.constructed().size());
|
||||
Assertions.assertNull(paymentProcessor.processPayment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenConstructorInvokedWithParameters_ThenMockObjectShouldBeCreated(){
|
||||
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class,(mock, context) -> when(mock.processPayment()).thenReturn("Credit"))){
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor("Debit");
|
||||
Assertions.assertEquals(1,mockPaymentService.constructed().size());
|
||||
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMultipleConstructorsInvoked_ThenMultipleMockObjectsShouldBeCreated(){
|
||||
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class)){
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor();
|
||||
PaymentProcessor secondPaymentProcessor = new PaymentProcessor();
|
||||
PaymentProcessor thirdPaymentProcessor = new PaymentProcessor("Debit");
|
||||
|
||||
when(mockPaymentService.constructed().get(0).processPayment()).thenReturn("Credit");
|
||||
when(mockPaymentService.constructed().get(1).processPayment()).thenReturn("Online Banking");
|
||||
|
||||
Assertions.assertEquals(3,mockPaymentService.constructed().size());
|
||||
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
|
||||
Assertions.assertEquals("Online Banking", secondPaymentProcessor.processPayment());
|
||||
Assertions.assertNull(thirdPaymentProcessor.processPayment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDependencyInjectionIsUsed_ThenMockObjectShouldBeCreated(){
|
||||
PaymentService mockPaymentService = Mockito.mock(PaymentService.class);
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor(mockPaymentService);
|
||||
when(mockPaymentService.processPayment()).thenReturn("Online Banking");
|
||||
Assertions.assertEquals("Online Banking", paymentProcessor.processPayment());
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
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<UserService> 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<UserService> 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<UserService> 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<UserService> 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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user