Answers
I would share you some snapshots from one of existing project to show how junit can be applied successfully
So this is the general structure of the project and the imports you would need .
You can multiple test cases like
@Test
public void testGetCook() {
cook = cookDAO.get(3);
assertEquals("Successfully fetched a single cook from the table!","Television",cook.getName());
}
@Test
public void testUpdateCook() {
cook = cookDAO.get(3);
cook.setName("TV");
assertEquals("Successfully updated a single cook in the table!",true,cookDAO.update(cook));
}
This code is from the same file . the important line is the assertEquals for you . So if the update has been successfull then the test case pass .
After that you need to write click on the file and Run As --> Junit .
So for your example , you need to run the test case on your methods for example .
private double calculateTax(double gross){
double federalTax = gross*FEDERAL_TAX_RATE;
double stateTax = gross*STATE_TAX_RATE;
return federalTax+stateTax;
}
assertEquals("Successfully calculated test",expectedvalue,calculateTax(100.00));
@Test
public void testUpdateCook() {
// Create the PayCalculator object here.
assertEquals("Successfully calculated test",expectedvalue,obj.calculateTax(100.00));
}
Hope this helps.
Page.jsp UserDetailD... CookTest.java X onlineshopping [online-shopping master] L 1 package com.homsil.shoppingbackend.test; > Servers > shoppingbackend [online-shopping master] 3e import static org.junit.Assert.assertEquals; src/main/java com.homsil.shoppingbackend 5 import org.junit. BeforeClass; ► at com.homsil.shoppingbackend.config import org.junit.Test; > E com.homsil.shoppingbackend.dao import org.springframework.context.annotation. AnnotationConfigApplicationContext; . com.homsil.shoppingbackend.daoimpl 9 import com.homil.shoppingbackend.dao.
CookDAO; > CookDAOImpl.java 10 import com.homsil.shoppingbackend.dto. Cook; Location DAOImpl.java ProductDAOImpl.java 12 public class CookTest { > L UserDetailDAOImpl.java com.homsil.shoppingbackend.dto private static AnnotationConfigApplicationContext context; src/test/java #com.homsil.shoppingbackend . com.homsil.shoppingbackend.test private static CookDAO cookDAO; ► D. CookTest.java ► de Location Test.java ► do ProductTestCase.java oio con