Sai Upadhyayula
Sai Upadhyayula

Reputation: 2496

Unordered Execution of Tests in JUnit for ShoppingCart

I am learning TDD practices and writing Unit Tests.While trying to create tests for Shopping Cart Application I expected an unordered execution of my tests.

Here are my list of tests:

Test 1: Can Create a Shopping Cart which is Empty

Test 2: Can Add Product to Shopping Cart

Test 3: Can Calculate Total Price of Shopping Cart

I am assuming the order of test execution must be

Test1

Test2

Test3

But the actual Order of Test Execution in my code is

Test1

Test3

Test2

I found out that it is a bad practice to specify the order of execution for our Unit Tests, which I want to avoid.What may be the reason for this kind of behavior and what is the solution?

Here is my code:

package com.techie.kart;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import java.math.BigDecimal;
import org.junit.Before;
import org.junit.Test;

import com.techie.kart.model.Product;

public class ShoppingKartTest {

    private ShoppingCart cart;
    private Product appleProduct;
    private Product sonyProduct;

    @Before
    public void setup() {
        cart = new ShoppingCart();
        appleProduct = new Product.ProductBuilder().name("Mobile")
                .price(new BigDecimal(10000.25)).description("This is IPhone")
                .manufacturer("Apple").build();
        sonyProduct = new Product.ProductBuilder().name("Mobile")
                .price(new BigDecimal(50000.00)).description("This is Sony")
                .manufacturer("Sony").build();

    }

    @Test
    public void CanCreateAnEmptyShoppingCart() {
        System.out.println("First Test Case:"+cart.getProductsCount());
        assertEquals(0, cart.getProductsCount());
    }

    @Test
    public void CanAddProductsToKart() {
        cart.addProduct(appleProduct);
        cart.addProduct(sonyProduct);
        assertNotEquals(0, cart.getProductsCount());
        System.out.println("Second Test Case:"+cart.getProductsCount());
        assertEquals(2, cart.getProductsCount());
        assertEquals(new BigDecimal(60000.25), cart.getTotal());
        System.out.println("Second Test Case:"+cart.getTotal());
    }

    @Test
    public void CanCalculateTotalCartPrice() {
        System.out.println("Third Test Case:"+cart.getTotal());
        System.out.println("Third Test Case:"+cart.getProductsCount());
        assertEquals(new BigDecimal(60000.25), cart.getTotal());
    }
}

Actual Output:

First Test Case:0
Third Test Case:0
Third Test Case:0
Second Test Case:2
Second Test Case:60000.25

Expected Output(My Assumption):

First Test Case:0
Second Test Case:2
Second Test Case:60000.25
Third Test Case:2
Third Test Case:60000.25

Upvotes: 1

Views: 1382

Answers (1)

Kevin
Kevin

Reputation: 91

For each individual test case, the only thing you can expect to have been executed is whatever is in the method tagged by the "@Before". So for the scope of your third method, the cart has just been initialized without any products being added. You need to add the products again in this method, regardless of whether they have been added in any other test case. When writing individual test cases, treat them as if no other test cases exist.

Upvotes: 3

Related Questions