com.startup.musicstore.test.repository.AlbumRepositoryTest.java Source code

Java tutorial

Introduction

Here is the source code for com.startup.musicstore.test.repository.AlbumRepositoryTest.java

Source

/*
 * 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 com.startup.musicstore.test.repository;

import com.startup.musicstore.domain.Album;
import com.startup.musicstore.domain.CustomerAddress;
import com.startup.musicstore.respository.AddressRepository;
import com.startup.musicstore.respository.AlbumRepository;
import com.startup.musicstore.test.ConnectionConfigTest;
import java.math.BigDecimal;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 *
 * @author hashcode
 */
public class AlbumRepositoryTest {

    public static ApplicationContext ctx;
    private Long id;
    private AlbumRepository repo;

    public AlbumRepositoryTest() {
    }

    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    @Test(enabled = true)
    public void create() {
        repo = ctx.getBean(AlbumRepository.class);
        Album album = new Album.Builder("Jazz").inventory(10).unitPrice(BigDecimal.valueOf(100.00)).build();
        repo.save(album);
        id = album.getId();
        Assert.assertNotNull(id);
    }

    @Test(dependsOnMethods = "create", enabled = true)
    public void read() {
        repo = ctx.getBean(AlbumRepository.class);
        Assert.assertNotNull(repo.findOne(id));
    }

    @Test(dependsOnMethods = "read", enabled = true)
    private void update() {
        repo = ctx.getBean(AlbumRepository.class);
        Album albumNew = new Album.Builder("Jazz").clone(repo.findOne(id)).sales(1231).build();
        repo.save(albumNew);
        Assert.assertEquals(repo.findOne(id).getSales(), 1231);

    }

    @Test(dependsOnMethods = "update", enabled = true)
    private void delete() {
        repo = ctx.getBean(AlbumRepository.class);
        repo.delete(id);
        Assert.assertNull(repo.findOne(id));
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
        // INITIALISE THE TEST CONTEST
        ctx = new AnnotationConfigApplicationContext(ConnectionConfigTest.class);
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @BeforeMethod
    public void setUpMethod() throws Exception {
    }

    @AfterMethod
    public void tearDownMethod() throws Exception {
    }
}