- Add dependency on PowerMock.
- Using PowerMock and Mockito to mock a Static Method.
- PowerMockitoMockingStaticMethodTest
pom.xml
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
Other Useful Snippets
@RunWith(PowerMockRunner.class)
@PrepareForTest({ UtilityClass.class})
PowerMockito.mockStatic(UtilityClass.class);
when(UtilityClass.staticMethod(anyLong())).thenReturn(150);
PowerMockito.verifyStatic();
UtilityClass.staticMethod(1 + 2 + 3);
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.in28minutes.mockito</groupId>
<artifactId>mockito-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.in28minutes.business;
import java.util.ArrayList;
import java.util.List;
import com.in28minutes.data.api.TodoService;
public class TodoBusinessImpl {
private TodoService todoService;
TodoBusinessImpl(TodoService todoService) {
this.todoService = todoService;
}
public List<String> retrieveTodosRelatedToSpring(String user) {
List<String> filteredTodos = new ArrayList<String>();
List<String> allTodos = todoService.retrieveTodos(user);
for (String todo : allTodos) {
if (todo.contains("Spring")) {
filteredTodos.add(todo);
}
}
return filteredTodos;
}
public void deleteTodosNotRelatedToSpring(String user) {
List<String> allTodos = todoService.retrieveTodos(user);
for (String todo : allTodos) {
if (!todo.contains("Spring")) {
todoService.deleteTodo(todo);
}
}
}
}
package com.in28minutes.data.api;
import java.util.List;
// External Service - Lets say this comes from WunderList
public interface TodoService {
public List<String> retrieveTodos(String user);
void deleteTodo(String todo);
}
package com.in28minutes.junit.business;
import java.util.List;
import com.in28minutes.junit.business.exception.DifferentCurrenciesException;
import com.in28minutes.junit.model.Amount;
import com.in28minutes.junit.model.Product;
public interface ClientBO {
Amount getClientProductsSum(List<Product> products)
throws DifferentCurrenciesException;
}
package com.in28minutes.junit.business;
import java.math.BigDecimal;
import java.util.List;
import com.in28minutes.junit.business.exception.DifferentCurrenciesException;
import com.in28minutes.junit.model.Amount;
import com.in28minutes.junit.model.AmountImpl;
import com.in28minutes.junit.model.Currency;
import com.in28minutes.junit.model.Product;
public class ClientBOImpl implements ClientBO {
public Amount getClientProductsSum(List<Product> products)
throws DifferentCurrenciesException {
if (products.size() == 0)
return new AmountImpl(BigDecimal.ZERO, Currency.EURO);
if (!isCurrencySameForAllProducts(products)) {
throw new DifferentCurrenciesException();
}
BigDecimal productSum = calculateProductSum(products);
Currency firstProductCurrency = products.get(0).getAmount()
.getCurrency();
return new AmountImpl(productSum, firstProductCurrency);
}
private BigDecimal calculateProductSum(List<Product> products) {
BigDecimal sum = BigDecimal.ZERO;
// Calculate Sum of Products
for (Product product : products) {
sum = sum.add(product.getAmount().getValue());
}
return sum;
}
private boolean isCurrencySameForAllProducts(List<Product> products)
throws DifferentCurrenciesException {
Currency firstProductCurrency = products.get(0).getAmount()
.getCurrency();
for (Product product : products) {
boolean currencySameAsFirstProduct = product.getAmount()
.getCurrency().equals(firstProductCurrency);
if (!currencySameAsFirstProduct) {
return false;
}
}
return true;
}
}
package com.in28minutes.junit.business.exception;
public class DifferentCurrenciesException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
}
package com.in28minutes.junit.helper;
public class StringHelper {
public String truncateAInFirst2Positions(String str) {
if (str.length() <= 2)
return str.replaceAll("A", "");
String first2Chars = str.substring(0, 2);
String stringMinusFirst2Chars = str.substring(2);
return first2Chars.replaceAll("A", "") + stringMinusFirst2Chars;
}
public boolean areFirstAndLastTwoCharactersTheSame(String str) {
if (str.length() <= 1)
return false;
if (str.length() == 2)
return true;
String first2Chars = str.substring(0, 2);
String last2Chars = str.substring(str.length() - 2);
return first2Chars.equals(last2Chars);
}
}
package com.in28minutes.junit.model;
import java.math.BigDecimal;
public interface Amount {
BigDecimal getValue();
Currency getCurrency();
}
package com.in28minutes.junit.model;
import java.math.BigDecimal;
public class AmountImpl implements Amount {
BigDecimal value;
Currency currency;
public AmountImpl(BigDecimal value, Currency currency) {
super();
this.value = value;
this.currency = currency;
}
public void setValue(BigDecimal value) {
this.value = value;
}
@Override
public BigDecimal getValue() {
return value;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
@Override
public Currency getCurrency() {
return currency;
}
}
package com.in28minutes.junit.model;
import java.math.BigDecimal;
import java.util.List;
/**
* Client Model API.
*/
public interface Client {
long getId();
String getName();
Enum<?> getType();
List<Collateral> getCollaterals();
List<Product> getProducts();
void setProductAmount(BigDecimal productAmount);
BigDecimal getProductAmount();
}
package com.in28minutes.junit.model;
import java.math.BigDecimal;
import java.util.List;
/**
* Client Model API.
*/
public class ClientImpl implements Client {
private long id;
private String name;
private ClientType type;
private List<Collateral> collaterals;
private List<Product> products;
private BigDecimal productAmount;
public ClientImpl(long id, String name, ClientType type,
List<Collateral> collaterals, List<Product> products) {
super();
this.id = id;
this.name = name;
this.type = type;
this.collaterals = collaterals;
this.products = products;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public ClientType getType() {
return type;
}
public void setType(ClientType type) {
this.type = type;
}
@Override
public List<Collateral> getCollaterals() {
return collaterals;
}
public void setCollaterals(List<Collateral> collaterals) {
this.collaterals = collaterals;
}
@Override
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
@Override
public BigDecimal getProductAmount() {
return productAmount;
}
@Override
public void setProductAmount(BigDecimal productAmount) {
this.productAmount = productAmount;
}
}
package com.in28minutes.junit.model;
import java.util.Arrays;
import java.util.List;
/**
* Available types of customers
*/
public enum ClientType {
/**
*
*/
PRIVATE("P"),
/**
*
*/
BUSINESS("Z");
private final String textValue;
/**
* List of natural person types.
*/
public static final List<String> NATURAL_PERSON_TYPES = Arrays
.asList(ClientType.PRIVATE.toString());
/**
* List of corporate types.
*/
public static final List<String> CORPORATE_TYPES = Arrays
.asList(ClientType.BUSINESS.toString());
ClientType(final String textValue) {
this.textValue = textValue;
}
@Override
public String toString() {
return textValue;
}
}
package com.in28minutes.junit.model;
/**
* Collateral Model API.
*/
public interface Collateral {
long getId();
String getName();
CollateralType getType();
}
package com.in28minutes.junit.model;
/**
* Collateral Model Object.
*/
public class CollateralImpl implements Collateral {
private long id;
private String name;
private CollateralType type;
public CollateralImpl(long id, String name, CollateralType type) {
super();
this.id = id;
this.name = name;
this.type = type;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public CollateralType getType() {
return type;
}
public void setType(CollateralType type) {
this.type = type;
}
}
package com.in28minutes.junit.model;
import java.util.Arrays;
import java.util.List;
/**
* Available types of customers
*/
public enum CollateralType {
REAL_ESTATE("REA"), BONDS("BND"), MUTUAL_FUNDS("MFD"), STOCKS("STK");
private final String textValue;
/**
* All collateral types classified as securities.
*/
public static final List<CollateralType> SECURITIES = Arrays.asList(BONDS,
MUTUAL_FUNDS, STOCKS);
CollateralType(final String textValue) {
this.textValue = textValue;
}
@Override
public String toString() {
return textValue;
}
}
package com.in28minutes.junit.model;
public enum Currency {
EURO("EUR"), UNITED_STATES_DOLLAR("USD"), INDIAN_RUPEE("INR");
private final String textValue;
Currency(final String textValue) {
this.textValue = textValue;
}
@Override
public String toString() {
return textValue;
}
}
package com.in28minutes.junit.model;
/**
* Product Model API.
*/
public interface Product {
long getId();
String getName();
ProductType getType();
Amount getAmount();
}
package com.in28minutes.junit.model;
/**
* Collateral Model Object.
*/
public class ProductImpl implements Product {
private long id;
private String name;
private ProductType type;
private Amount amount;
public ProductImpl(long id, String name, ProductType type, Amount amount) {
super();
this.id = id;
this.name = name;
this.type = type;
this.amount = amount;
}
@Override
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
@Override
public Amount getAmount() {
return amount;
}
public void setAmount(Amount amount) {
this.amount = amount;
}
}
package com.in28minutes.junit.model;
/**
* Available types of customers
*/
public enum ProductType {
LOAN("LN"), KREDIT("KRD"), BANK_GUARANTEE("BG");
private final String textValue;
ProductType(final String textValue) {
this.textValue = textValue;
}
@Override
public String toString() {
return textValue;
}
}
package com.in28minutes.powermock;
import java.util.ArrayList;
import java.util.List;
interface Dependency {
List<Integer> retrieveAllStats();
}
public class SystemUnderTest {
private Dependency dependency;
public int methodUsingAnArrayListConstructor() {
ArrayList list = new ArrayList();
return list.size();
}
public int methodCallingAStaticMethod() {
//privateMethodUnderTest calls static method SomeClass.staticMethod
List<Integer> stats = dependency.retrieveAllStats();
long sum = 0;
for (int stat : stats)
sum += stat;
return UtilityClass.staticMethod(sum);
}
private long privateMethodUnderTest() {
List<Integer> stats = dependency.retrieveAllStats();
long sum = 0;
for (int stat : stats)
sum += stat;
return sum;
}
}
package com.in28minutes.powermock;
public class UtilityClass {
static int staticMethod(long value) {
// Some complex logic is done here...
throw new RuntimeException(
"I dont want to be executed. I will anyway be mocked out.");
}
}
package com.clarity.business;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.in28minutes.junit.business.ClientBO;
import com.in28minutes.junit.business.ClientBOImpl;
import com.in28minutes.junit.business.exception.DifferentCurrenciesException;
import com.in28minutes.junit.model.Amount;
import com.in28minutes.junit.model.AmountImpl;
import com.in28minutes.junit.model.Currency;
import com.in28minutes.junit.model.Product;
import com.in28minutes.junit.model.ProductImpl;
import com.in28minutes.junit.model.ProductType;
public class ClientBOTest {
private ClientBO clientBO = new ClientBOImpl();
@Test
public void testClientProductSum() throws DifferentCurrenciesException {
List<Product> products = new ArrayList<Product>();
products.add(new ProductImpl(100, "Product 15",
ProductType.BANK_GUARANTEE, new AmountImpl(
new BigDecimal("5.0"), Currency.EURO)));
products.add(new ProductImpl(120, "Product 20",
ProductType.BANK_GUARANTEE, new AmountImpl(
new BigDecimal("6.0"), Currency.EURO)));
Amount temp = clientBO.getClientProductsSum(products);
assertEquals(Currency.EURO, temp.getCurrency());
assertEquals(new BigDecimal("11.0"), temp.getValue());
}
@Test(expected = DifferentCurrenciesException.class)
public void testClientProductSum1() throws DifferentCurrenciesException {
List<Product> products = new ArrayList<Product>();
products.add(new ProductImpl(100, "Product 15",
ProductType.BANK_GUARANTEE, new AmountImpl(
new BigDecimal("5.0"), Currency.INDIAN_RUPEE)));
products.add(new ProductImpl(120, "Product 20",
ProductType.BANK_GUARANTEE, new AmountImpl(
new BigDecimal("6.0"), Currency.EURO)));
@SuppressWarnings("unused")
Amount temp = null;
temp = clientBO.getClientProductsSum(products);
}
@Test
public void testClientProductSum2() {
List<Product> products = new ArrayList<Product>();
Amount temp = null;
try {
temp = clientBO.getClientProductsSum(products);
} catch (DifferentCurrenciesException e) {
}
assertEquals(Currency.EURO, temp.getCurrency());
assertEquals(BigDecimal.ZERO, temp.getValue());
}
}
package com.clarity.business;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.in28minutes.junit.business.ClientBO;
import com.in28minutes.junit.business.ClientBOImpl;
import com.in28minutes.junit.business.exception.DifferentCurrenciesException;
import com.in28minutes.junit.model.Amount;
import com.in28minutes.junit.model.AmountImpl;
import com.in28minutes.junit.model.Currency;
import com.in28minutes.junit.model.Product;
import com.in28minutes.junit.model.ProductImpl;
import com.in28minutes.junit.model.ProductType;
public class ClientBOTestRefactored {
private ClientBO clientBO = new ClientBOImpl();
@Test
public void testClientProductSum_AllProductsSameCurrency()
throws DifferentCurrenciesException {
Amount[] amounts = {
new AmountImpl(new BigDecimal("5.0"), Currency.EURO),
new AmountImpl(new BigDecimal("6.0"), Currency.EURO) };
Amount expected = new AmountImpl(new BigDecimal("11.0"), Currency.EURO);
List<Product> products = createProductListWithAmounts(amounts);
Amount actual = clientBO.getClientProductsSum(products);
assertAmount(actual, expected);
}
@Test(expected = DifferentCurrenciesException.class)
public void testClientProductSum_DifferentCurrencies_ThrowsException()
throws DifferentCurrenciesException {
Amount[] amounts = {
new AmountImpl(new BigDecimal("5.0"), Currency.EURO),
new AmountImpl(new BigDecimal("6.0"), Currency.INDIAN_RUPEE) };
List<Product> products = createProductListWithAmounts(amounts);
@SuppressWarnings("unused")
Amount actual = clientBO.getClientProductsSum(products);
}
@Test
public void testClientProductSum_NoProducts()
throws DifferentCurrenciesException {
Amount[] amounts = {};
Amount expected = new AmountImpl(BigDecimal.ZERO, Currency.EURO);
List<Product> products = createProductListWithAmounts(amounts);
Amount actual = clientBO.getClientProductsSum(products);
assertAmount(actual, expected);
}
private void assertAmount(Amount actual, Amount expected) {
assertEquals(expected.getCurrency(), actual.getCurrency());
assertEquals(expected.getValue(), actual.getValue());
}
private List<Product> createProductListWithAmounts(Amount[] amounts) {
List<Product> products = new ArrayList<Product>();
for (Amount amount : amounts) {
products.add(new ProductImpl(100, "Product 15",
ProductType.BANK_GUARANTEE, amount));
}
return products;
}
}
package com.in28minutes.business;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import com.in28minutes.data.api.TodoService;
@RunWith(MockitoJUnitRunner.class)
public class TodoBusinessImplMockitoInjectMocksTest {
@Mock
TodoService todoService;
@InjectMocks
TodoBusinessImpl todoBusinessImpl;
@Captor
ArgumentCaptor<String> stringArgumentCaptor;
@Test
public void usingMockito() {
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
List<String> todos = todoBusinessImpl
.retrieveTodosRelatedToSpring("Ranga");
assertEquals(2, todos.size());
}
@Test
public void usingMockito_UsingBDD() {
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
//given
given(todoService.retrieveTodos("Ranga")).willReturn(allTodos);
//when
List<String> todos = todoBusinessImpl
.retrieveTodosRelatedToSpring("Ranga");
//then
assertThat(todos.size(), is(2));
}
@Test
public void letsTestDeleteNow() {
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
todoBusinessImpl.deleteTodosNotRelatedToSpring("Ranga");
verify(todoService).deleteTodo("Learn to Dance");
verify(todoService, Mockito.never()).deleteTodo("Learn Spring MVC");
verify(todoService, Mockito.never()).deleteTodo("Learn Spring");
verify(todoService, Mockito.times(1)).deleteTodo("Learn to Dance");
// atLeastOnce, atLeast
}
@Test
public void captureArgument() {
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
Mockito.when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
todoBusinessImpl.deleteTodosNotRelatedToSpring("Ranga");
Mockito.verify(todoService).deleteTodo(stringArgumentCaptor.capture());
assertEquals("Learn to Dance", stringArgumentCaptor.getValue());
}
}
package com.in28minutes.business;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import com.in28minutes.data.api.TodoService;
public class TodoBusinessImplMockitoRulesTest {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
TodoService todoService;
@InjectMocks
TodoBusinessImpl todoBusinessImpl;
@Captor
ArgumentCaptor<String> stringArgumentCaptor;
@Test
public void usingMockito() {
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
List<String> todos = todoBusinessImpl
.retrieveTodosRelatedToSpring("Ranga");
assertEquals(2, todos.size());
}
@Test
public void usingMockito_UsingBDD() {
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
//given
given(todoService.retrieveTodos("Ranga")).willReturn(allTodos);
//when
List<String> todos = todoBusinessImpl
.retrieveTodosRelatedToSpring("Ranga");
//then
assertThat(todos.size(), is(2));
}
@Test
public void letsTestDeleteNow() {
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
todoBusinessImpl.deleteTodosNotRelatedToSpring("Ranga");
verify(todoService).deleteTodo("Learn to Dance");
verify(todoService, Mockito.never()).deleteTodo("Learn Spring MVC");
verify(todoService, Mockito.never()).deleteTodo("Learn Spring");
verify(todoService, Mockito.times(1)).deleteTodo("Learn to Dance");
// atLeastOnce, atLeast
}
@Test
public void captureArgument() {
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
Mockito.when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
todoBusinessImpl.deleteTodosNotRelatedToSpring("Ranga");
Mockito.verify(todoService).deleteTodo(stringArgumentCaptor.capture());
assertEquals("Learn to Dance", stringArgumentCaptor.getValue());
}
}
package com.in28minutes.business;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import com.in28minutes.data.api.TodoService;
public class TodoBusinessImplMockitoTest {
@Test
public void usingMockito() {
TodoService todoService = mock(TodoService.class);
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService);
List<String> todos = todoBusinessImpl
.retrieveTodosRelatedToSpring("Ranga");
assertEquals(2, todos.size());
}
@Test
public void usingMockito_UsingBDD() {
TodoService todoService = mock(TodoService.class);
TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService);
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
//given
given(todoService.retrieveTodos("Ranga")).willReturn(allTodos);
//when
List<String> todos = todoBusinessImpl
.retrieveTodosRelatedToSpring("Ranga");
//then
assertThat(todos.size(), is(2));
}
@Test
public void letsTestDeleteNow() {
TodoService todoService = mock(TodoService.class);
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService);
todoBusinessImpl.deleteTodosNotRelatedToSpring("Ranga");
verify(todoService).deleteTodo("Learn to Dance");
verify(todoService, Mockito.never()).deleteTodo("Learn Spring MVC");
verify(todoService, Mockito.never()).deleteTodo("Learn Spring");
verify(todoService, Mockito.times(1)).deleteTodo("Learn to Dance");
// atLeastOnce, atLeast
}
@Test
public void captureArgument() {
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor
.forClass(String.class);
TodoService todoService = mock(TodoService.class);
List<String> allTodos = Arrays.asList("Learn Spring MVC",
"Learn Spring", "Learn to Dance");
Mockito.when(todoService.retrieveTodos("Ranga")).thenReturn(allTodos);
TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService);
todoBusinessImpl.deleteTodosNotRelatedToSpring("Ranga");
Mockito.verify(todoService).deleteTodo(argumentCaptor.capture());
assertEquals("Learn to Dance", argumentCaptor.getValue());
}
}
package com.in28minutes.business;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import com.in28minutes.data.api.TodoService;
import com.in28minutes.data.stub.TodoServiceStub;
public class TodoBusinessImplStubTest {
@Test
public void usingAStub() {
TodoService todoService = new TodoServiceStub();
TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService);
List<String> todos = todoBusinessImpl
.retrieveTodosRelatedToSpring("Ranga");
assertEquals(2, todos.size());
}
}
package com.in28minutes.data.stub;
import java.util.Arrays;
import java.util.List;
import com.in28minutes.data.api.TodoService;
public class TodoServiceStub implements TodoService {
public List<String> retrieveTodos(String user) {
return Arrays.asList("Learn Spring MVC", "Learn Spring",
"Learn to Dance");
}
public void deleteTodo(String todo) {
}
}
package com.in28minutes.junit.helper;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
public class ArraysCompareTest {
@Test
public void testArraySort_RandomArray() {
int[] numbers = { 12, 3, 4, 1 };
int[] expected = { 1, 3, 4, 12 };
Arrays.sort(numbers);
assertArrayEquals(expected, numbers);
}
@Test(expected=NullPointerException.class)
public void testArraySort_NullArray() {
int[] numbers = null;
Arrays.sort(numbers);
}
@Test(timeout=100)
public void testSort_Performance(){
int array[] = {12,23,4};
for(int i=1;i<=1000000;i++)
{
array[0] = i;
Arrays.sort(array);
}
}
}
package com.in28minutes.junit.helper;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
public class ArraysTest {
@Test(timeout=100)
public void testPerformance() {
for(int i=0;i<1000000;i++){
Arrays.sort(new int[]{i,i-1,i+1});
}
}
}
package com.in28minutes.junit.helper;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class QuickBeforeAfterTest {
@BeforeClass
public static void beforeClass(){
System.out.println("Before Class");
}
@Before
public void setup(){
System.out.println("Before Test");
}
@Test
public void test1() {
System.out.println("test1 executed");
}
@Test
public void test2() {
System.out.println("test2 executed");
}
@After
public void teardown() {
System.out.println("After test");
}
@AfterClass
public static void afterClass(){
System.out.println("After Class");
}
}
package com.in28minutes.junit.helper;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class StringHelperParameterizedTest {
// AACD => CD ACD => CD CDEF=>CDEF CDAA => CDAA
StringHelper helper = new StringHelper();
private String input;
private String expectedOutput;
public StringHelperParameterizedTest(String input, String expectedOutput) {
this.input = input;
this.expectedOutput = expectedOutput;
}
@Parameters
public static Collection<String[]> testConditions() {
String expectedOutputs[][] = {
{ "AACD", "CD" },
{ "ACD", "CD" } };
return Arrays.asList(expectedOutputs);
}
@Test
public void testTruncateAInFirst2Positions() {
assertEquals(expectedOutput,
helper.truncateAInFirst2Positions(input));
}
}
package com.in28minutes.junit.helper;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class StringHelperTest {
// AACD => CD ACD => CD CDEF=>CDEF CDAA => CDAA
StringHelper helper;
@Before
public void before(){
helper = new StringHelper();
}
@Test
public void testTruncateAInFirst2Positions_AinFirst2Positions() {
assertEquals("CD", helper.truncateAInFirst2Positions("AACD"));
}
@Test
public void testTruncateAInFirst2Positions_AinFirstPosition() {
assertEquals("CD", helper.truncateAInFirst2Positions("ACD"));
}
// ABCD => false, ABAB => true, AB => true, A => false
@Test
public void testAreFirstAndLastTwoCharactersTheSame_BasicNegativeScenario() {
assertFalse(
helper.areFirstAndLastTwoCharactersTheSame("ABCD"));
}
@Test
public void testAreFirstAndLastTwoCharactersTheSame_BasicPositiveScenario() {
assertTrue(
helper.areFirstAndLastTwoCharactersTheSame("ABAB"));
}
}
package com.in28minutes.junit.suite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.in28minutes.junit.helper.ArraysTest;
import com.in28minutes.junit.helper.StringHelperTest;
@RunWith(Suite.class)
@SuiteClasses({ArraysTest.class,StringHelperTest.class})
public class DummyTestSuite {
}
package com.in28minutes.mockito;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class FirstMockitoTest {
@Test
public void test() {
assertTrue(true);
}
}
package com.in28minutes.mockito;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.core.Every.everyItem;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class HamcrestMatcherTest {
@Test
public void basicHamcrestMatchers() {
List<Integer> scores = Arrays.asList(99, 100, 101, 105);
assertThat(scores, hasSize(4));
assertThat(scores, hasItems(100, 101));
assertThat(scores, everyItem(greaterThan(90)));
assertThat(scores, everyItem(lessThan(200)));
// String
assertThat("", isEmptyString());
assertThat(null, isEmptyOrNullString());
// Array
Integer[] marks = { 1, 2, 3 };
assertThat(marks, arrayWithSize(3));
assertThat(marks, arrayContainingInAnyOrder(2, 3, 1));
}
}
package com.in28minutes.mockito;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import org.junit.Test;
import org.mockito.Mockito;
public class ListTest {
@Test
public void letsMockListSize() {
List list = mock(List.class);
when(list.size()).thenReturn(10);
assertEquals(10, list.size());
}
@Test
public void letsMockListSizeWithMultipleReturnValues() {
List list = mock(List.class);
when(list.size()).thenReturn(10).thenReturn(20);
assertEquals(10, list.size()); // First Call
assertEquals(20, list.size()); // Second Call
}
@Test
public void letsMockListGet() {
List<String> list = mock(List.class);
when(list.get(0)).thenReturn("in28Minutes");
assertEquals("in28Minutes", list.get(0));
assertNull(list.get(1));
}
@Test(expected = RuntimeException.class)
public void letsMockListGetToThrowException() {
List<String> list = mock(List.class);
when(list.get(Mockito.anyInt())).thenThrow(
new RuntimeException("Something went wrong"));
list.get(0);
}
@Test
public void letsMockListGetWithAny() {
List<String> list = mock(List.class);
Mockito.when(list.get(Mockito.anyInt())).thenReturn("in28Minutes");
// If you are using argument matchers, all arguments
// have to be provided by matchers.
assertEquals("in28Minutes", list.get(0));
assertEquals("in28Minutes", list.get(1));
}
@Test
public void bddAliases_UsingGivenWillReturn() {
List<String> list = mock(List.class);
//given
given(list.get(Mockito.anyInt())).willReturn("in28Minutes");
//then
assertThat("in28Minutes", is(list.get(0)));
assertThat("in28Minutes", is(list.get(0)));
}
}
package com.in28minutes.mockito;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.stub;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class SpyTest {
@Test
public void creatingASpyOnArrayList() {
List<String> listSpy = spy(ArrayList.class);
listSpy.add("Ranga");
listSpy.add("in28Minutes");
verify(listSpy).add("Ranga");
verify(listSpy).add("in28Minutes");
assertEquals(2, listSpy.size());
assertEquals("Ranga", listSpy.get(0));
}
@Test
public void creatingASpyOnArrayList_overridingSpecificMethods() {
List<String> listSpy = spy(ArrayList.class);
listSpy.add("Ranga");
listSpy.add("in28Minutes");
stub(listSpy.size()).toReturn(-1);
assertEquals(-1, listSpy.size());
assertEquals("Ranga", listSpy.get(0));
// @Spy Annotation
}
}
package com.in28minutes.powermock;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ UtilityClass.class})
public class PowerMockitoMockingStaticMethodTest {
@Mock
Dependency dependencyMock;
@InjectMocks
SystemUnderTest systemUnderTest;
@Test
public void powerMockito_MockingAStaticMethodCall() {
when(dependencyMock.retrieveAllStats()).thenReturn(
Arrays.asList(1, 2, 3));
PowerMockito.mockStatic(UtilityClass.class);
when(UtilityClass.staticMethod(anyLong())).thenReturn(150);
assertEquals(150, systemUnderTest.methodCallingAStaticMethod());
//To verify a specific method call
//First : Call PowerMockito.verifyStatic()
//Second : Call the method to be verified
PowerMockito.verifyStatic();
UtilityClass.staticMethod(1 + 2 + 3);
// verify exact number of calls
//PowerMockito.verifyStatic(Mockito.times(1));
}
}