08. Spring - Writing JUnit test case.
Writing JUnit test case.
JUnit
test case write කිරීමේදී පහත dependencies අවශ්ය වේ.
<!--
JUNIT VERSION 4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Right
click on the src/test/java
and create a class within a package.
import org.junit.Assert.assertEquals;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import dao.CategoryDAO;
import dto.Category;
public class CategoryTestCase {
private static AnnotationConfigApplicationContext context;
private static CategoryDAO categoryDAO;
private Category category;
@BeforeClass
public static void init() {
context = new AnnotationConfigApplicationContext();
context.scan("com.lhu.backend");
context.refresh();
categoryDAO = (CategoryDAO)context.getBean("categoryDAO");
}
@Test
publicvoidtestCRUDCategory() {
// add operation
category = newCategory();
category.setName("Laptop");
category.setDescription("This is some description for laptop!");
category.setImageURL("CAT_1.png");
assertEquals("Successfully added",true,categoryDAO.add(category));
// fetching and updating the category
category = categoryDAO.get(2);
category.setName("TV");
assertEquals("Successfully updated",true,categoryDAO.update(category));
// delete the category
assertEquals("Successfully deleted",true,categoryDAO.delete(category));
//fetching the list
assertEquals("Successfully fetched",1,categoryDAO.list().size());
}
}
Right click on created the test class then Run As - JUnit Test
Comments
Post a Comment