Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring Junit Test

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 189
    Comment on it

    Hello Guys! Testing is a very most important part of any project. Without good testing, a good product is uncertain. We have different frameworks available for testing the product functionality. We have a good framework JUnit available in Java. Which can be configured with the project easily. I am writing this blog with the aim to explain how we can configure JUnit with a all-in-one framework Spring. Follow is the base class I love to create to load the spring application context and configure Spring to enable JUnit.

    package com.multipli.base.dao;
    
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class BaseDAOTest {
    
    }
    

    In the above class, I configured class SpringJUnit4ClassRunner to take authority to handle JUnit test cases. And loaded the spring application context from the class path in next line. Now we can have as many as classes extending it and having their own test cases as per the testing class. For example-

    package com.multipli.dao.test;
    
    import static org.junit.Assert.*;
    
    import java.math.BigDecimal;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.annotation.Rollback;
    import org.springframework.transaction.annotation.Transactional;
    import org.junit.Test;
    
    import com.evontech.base.dao.*;
    
    public class PaymentDAOTest extends BaseDAOTest {
    
        @Autowired
        IPaymentDAO paymentDAO;
    
        @Test
        @Transactional
        public void testGetAll() {
            List<payment> payments = paymentDAO.getAll();
            System.out.println(payments);
        }
    
        @Test
        @Transactional
        public void testGetByLimits() {
            List<payment> payments = paymentDAO.getByLimits(1, 2);
            System.out.println(payments);
        }
    
        @Test
        @Transactional
        @Rollback(false)
        public void savePayment3() {
    
            Payment payment = new Payment();
            payment.setCanNumber("15019CLA02");
            payment.setGrpOrdRefNo(1109153003);     
            payment.setTransactionAmount(new BigDecimal(30000.00));
    
            assertNotNull(paymentDAO.save(payment));
        }
    
        @Test
        @Transactional
        public void testGetByCan() {
            String CAN = "15019CLA02";
            List<payment> payments = paymentDAO.getByCAN(CAN);
            System.out.println(payments);
        }
    
        @Test
        @Transactional
        public void testGetByFolio() {
            String folio = "DINESH1021";
            List<payment> payments = paymentDAO.getByFolio(folio);
            System.out.println(payments);
        }
    }
    
    

    In the above class, I have written the code to test a Payment DAO and its functionality.

    Annotation @Test defines method as a test case for the class and annotation @Transactional defines transaction is transactional, means either the entire transaction will execute successfully or none.

    Hope this would help you. Thanks. Happy Coding.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: