Reputation: 1316
Completely new to Mockito, here is what I started with:
Class Under Test User.java:
package com.test.mockito;
public class User {
private ProductManager productManager;
public boolean buy(Product product, int quantity) throws InsufficientProductsException {
boolean transactionStatus=false;
int availableQuantity = productManager.getAvailableProducts(product);
if (quantity < availableQuantity) {
throw new InsufficientProductsException();
}
productManager.orderProduct(product, quantity);
transactionStatus=true;
return transactionStatus;
}
public void setProductManager(ProductManager productManager) {
this.productManager = productManager;
}
}
Mock Objects: Product.java
package com.test.mockito;
public class Product {
}
ProductManager.java
package com.test.mockito;
public interface ProductManager {
int getAvailableProducts(Product product);
int orderProduct(Product product, int num);
}
Exception Class: InsufficientProductsException.java
package com.test.mockito;
public class InsufficientProductsException extends Exception {
private static final long serialVersionUID = 1L;
}
And finally the test code.
package com.test.mockito;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.*;
public class UserTest {
private User user;
private ProductManager productManager;
private Product product;
private int purchaseQuantity = 15;
@Before
public void setupMock() {
user = new User();
productManager = mock(ProductManager.class);
user.setProductManager(productManager);
product = mock(Product.class);
}
@Test(expected=InsufficientProductsException.class)
public void purchaseButInsufficientAvailableQuantity() throws InsufficientProductsException {
int availableQuantity = 3;
System.out.println("Train getAvailableProducts(product) to return " + availableQuantity);
when(productManager.getAvailableProducts(product)).thenReturn(availableQuantity);
try {
System.out.println("User.buy(" + purchaseQuantity + ") should fail with InsufficientProductsException");
user.buy(product,purchaseQuantity);
} catch (InsufficientProductsException e) {
System.out.println("InsufficientProductsException is thrown");
verify(productManager, times(0)).orderProduct(product, purchaseQuantity);
System.out.println("Verified orderProduct(product, " + purchaseQuantity + ") is not called");
throw e;
}
}
}
The test fails and it appears because the expected InsufficientProductsException was not thrown by User. Maven test reports:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.test.mockito.UserTest
Train getAvailableProducts(product) to return 3
User.buy(15) should fail with InsufficientProductsException
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.66 sec <<< FAILURE!
Results :
Failed tests: purchaseButInsufficientAvailableQuantity(com.test.mockito.UserTest): Expected exception: com.test.mockito.InsufficientProductsException
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
This might appear silly, but really can't get through what I'm doing wrong.
Upvotes: 1
Views: 595
Reputation: 312219
You have the check the wrong way round - you should throw an exception if the quantity
is more than the availableQuantity
, not less.
I.e., you should replace this check:
if (quantity < availableQuantity) {
throw new InsufficientProductsException();
}
With this one:
if (quantity > availableQuantity) {
throw new InsufficientProductsException();
}
Upvotes: 6