$.DummyDaoImplTest.java Source code

Java tutorial

Introduction

Here is the source code for $.DummyDaoImplTest.java

Source

    #set($symbol_pound='#')#set($symbol_dollar='$')#set($symbol_escape='\')
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
package ${package}.repositories;

import ${package}.entities.Dummy;
    import java.util.ArrayList;
    import java.util.List;
    import junit.framework.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.transaction.TransactionConfiguration;
    import org.springframework.transaction.annotation.Transactional;

    /**
     *
     * @author david
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:spring-config.xml", "classpath:spring-config-test.xml" })
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
    @Transactional
    public class DummyDaoImplTest {

        @Autowired
        private DummyDao dummyDao;

        @Test
        public void createAndFind() {
            Dummy d = new Dummy();
            d.setName("dummy");
            dummyDao.create(d);
            Assert.assertEquals(d, dummyDao.find(d.getId()));
        }

        @Test
        public void update() {
            Dummy d = new Dummy();
            d.setName("dummy");
            dummyDao.create(d);
            d.setName("otherDummy");
            dummyDao.update(d);
            Assert.assertEquals("otherDummy", dummyDao.find(d.getId()).getName());
        }

        @Test
        public void saveOrUpdate() {
            Dummy d = new Dummy();
            d.setName("dummy");
            dummyDao.save(d);
            Assert.assertEquals(d, dummyDao.find(d.getId()));

            d.setName("otherDummy");
            dummyDao.save(d);

            Assert.assertEquals("otherDummy", dummyDao.find(d.getId()).getName());
        }

        @Test
        public void delete() {
            Dummy d = new Dummy();
            dummyDao.create(d);
            dummyDao.delete(d);
            Assert.assertNull(dummyDao.find(d.getId()));
        }

        @Test
        public void findAll() {
            Dummy dummy = new Dummy();
            Dummy otherDummy = new Dummy();
            dummyDao.create(dummy);
            dummyDao.create(otherDummy);

            List<Dummy> expected = new ArrayList<>();
            expected.add(dummy);
            expected.add(otherDummy);

            org.junit.Assert.assertEquals(expected, dummyDao.findAll());
        }
    }