0

I am new to Junit and Mockito and Spring. I am trying to understand when to use Spring when Unit Testing. I am able to unit test below code without using Spring. I read many articles saying one of the benefits of Spring is when unit testing. I am confused since I was successfully able test my code using JUnit and Mockito then where and why use Spring?

Class Product

public class Product {

}

Interface ProductDao

public interface ProductDao {

    int getAvailableProducts(Product product);

    int orderProduct(Product product, int orderedQuantity);

}

Class ProductServiceTest

public class ProductService {

    private ProductDao productDao;

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    public boolean buy(Product product, int orderedQuantity) throws InsufficientProductsException {
        boolean transactionStatus = false;
        int availableQuantity = productDao.getAvailableProducts(product);
        if (orderedQuantity > availableQuantity) {
            throw new InsufficientProductsException();
        }
        productDao.orderProduct(product, orderedQuantity);
        transactionStatus = true;
        return transactionStatus;
    }

}

Class ProductServiceTest

import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;

import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ProductServiceTest {

    private ProductService productService;
    private ProductDao productDao;
    private Product product;
    private int purchaseQuantity = 15;

    @Before
    public void setupMock() {
        productService = new ProductService();
        product = mock(Product.class);
        productDao = mock(ProductDao.class);
        productService.setProductDao(productDao);
    }

    @Test
    public void testBuy() throws InsufficientProductsException {
        int availableQuantity = 30;
        System.out.println("Stubbing getAvailableProducts(product) to return " + availableQuantity);
        when(productDao.getAvailableProducts(product)).thenReturn(availableQuantity);
        System.out.println("Calling ProductService.buy(product," + purchaseQuantity + ")");
        productService.buy(product, purchaseQuantity);
        System.out.println("Verifying ProductDao(product, " + purchaseQuantity + ") is called");
        verify(productDao).orderProduct(product, purchaseQuantity);
        System.out.println("Verifying getAvailableProducts(product) is called at least once");
        verify(productDao, atLeastOnce()).getAvailableProducts(product);
        System.out.println("Verifying order of method calls on ProductDao: First call getAvailableProducts() followed by orderProduct()");
        InOrder order = inOrder(productDao);
        order.verify(productDao).getAvailableProducts(product);
        order.verify(productDao).orderProduct(product, purchaseQuantity);
    }

}

The difference between JUnit and Mockito?

If we mock everything (For example, everything inside buy method in ProductService) are we really unit testing. Mock everything setup everything how it is expected to behave. Then what is the point unit testing?

PiJei
  • 584
  • 4
  • 19
Neha
  • 745
  • 4
  • 10
  • 18

1 Answers1

0

So, the main difference between Mockito and Junit(both are for unit testing), is that when you have no calls of another classes methods - you use junit. When you have behavior inside your method with call of method of another class - you have to create mock and “simulate” behavior of that call, and you can’t do this with junit. For this case you need mockito or Power Mock. If you want to make a real call of another method - it’s no longer unit test, it becomes integration test. And you need to initialize a hole spring context. Hope it will help for understanding.

Valerii
  • 106
  • 9