Need help on Itegration Testing using STUBS and/or MOCKS! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help on Itegration Testing using STUBS and/or MOCKS!

dr_itondr_iton Posts: 4Member
edited February 2014 in C#

I created a three layer application with BO (Business Objects), BLL (Business Logic Layer) and DAL (Data Access Layer).
I had to do some unit testing using nUnit tool. I've done that like following and works perfectly:
SQL Tables:
`
USE [Projekti_TI_1]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[KLASA](
[ID_Klasa] [int] IDENTITY(1,1) NOT NULL,
[KlasaViti] [int] NOT NULL,
CONSTRAINT [PK_KLASA] PRIMARY KEY CLUSTERED
(
[ID_Klasa] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
`

And
`USE [Projekti_TI_1]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[PARALELJA](
[ID_Paralelja] [int] IDENTITY(1,1) NOT NULL,
[Paralelja] [int] NOT NULL,
CONSTRAINT [PK_PARALELJA_1] PRIMARY KEY CLUSTERED
(
[ID_Paralelja] ASC
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
`

Unit testing I've don as following:
BO
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BO
{
public class Klasa
{
private int k_ID_Klasa;
private int k_KlasaViti;
private int k_Paralelja;

    public Klasa(int a, int b)
    {

    }

    public Klasa() { }

    public int ID_Klasa
    {
        get { return k_ID_Klasa; }
        set { k_ID_Klasa = value; }
    }

    public int KlasaViti
    {
        get { return k_KlasaViti; }
        set { k_KlasaViti = value; }
    }

    public int Paralelja
    {
        get { return k_Paralelja; }
        set { k_Paralelja = value; }
    }
}

}
`

BLL
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using DAL;

namespace BLL
{
public class KlasaIURD
{
public KlasaIURD()
{

        }

        public static void Shto(Klasa obj)
        {
            KlasaDB.Shto(obj);

        }
 }

}
`

DAL
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
public class KlasaDB
{
public KlasaDB()
{

    }

    public static bool Shto(Klasa obj)
    {
        SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
        SqlCommand sqlCmd = new SqlCommand("procShtoKlasa", sqlConn);
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("@KlasaViti", obj.KlasaViti);
        sqlCmd.Parameters.AddWithValue("@Paralelja", obj.Paralelja);
        sqlConn.Open();
        if (sqlCmd.ExecuteNonQuery() > 0)
        {
            sqlConn.Close();
            return true;

        }
        else
        {
            return false;
        }

    }


}

}
`

And at last the form i created at ScoMan_TI namespace
`using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BO;
using BLL;
using DAL;

namespace ScoMan_TI
{
public partial class Forma_RegjKlasa : Form
{
KlasaDB klasa = new KlasaDB();
public Forma_RegjKlasa()
{
InitializeComponent();
this.klasatToolStripMenuItem.Enabled = false;

    }


    private void btnAzhuro_Click(object sender, EventArgs e)
    {

    }

    private void Forma_RegjKlasa_Load(object sender, EventArgs e)
    {


    }

    private void lendetToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Hide();
        Forma_Lendet frmLen = new Forma_Lendet();
        frmLen.ShowDialog();
    }

    private void nxenesitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Hide();
        Forma_NxenRegj frmNxRegj = new Forma_NxenRegj();
        frmNxRegj.ShowDialog();

    }

    private void mesimdhenesitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Hide();
        Forma_MesimRegj frmMesRegj = new Forma_MesimRegj();
        frmMesRegj.ShowDialog();
    }

    private void btnShto_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txtKlasaViti.Text))
            {
                errorKlasa.SetError(txtKlasaViti, "Ju lutemi plotësoni fushën: KLASA!");
                return;
            }
            if (string.IsNullOrEmpty(txtParalelja.Text))
            {
                errorKlasa.SetError(txtParalelja, "Ju lutemi plotësoni fushën: PARALELJA!");
                return;
            }
            Klasa objKlasa = new Klasa();
            objKlasa.KlasaViti = Convert.ToInt32(txtKlasaViti.Text);
            KlasaIURD.Shto(objKlasa);

            Paralelja objParalelja = new Paralelja();
            objParalelja.Paralelet = Convert.ToInt32(txtParalelja.Text);
            ParaleljaIURD.Shto(objParalelja);

        }
        catch { MessageBox.Show("Keni problem ne databazë"); }
    }    
}

}
`

The testing class for that is as follows:
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using BO;
using DAL;
using NUnit.Framework;

namespace ScoMan_TI.NUnit_Testing
{
[TestFixture]
class Klasa_Testuese
{

    Klasa klasa = new Klasa();
    KlasaDB klasaDB = new KlasaDB();



    [SetUp]   
    public void initKlasa()
    {

        klasa.KlasaViti = 2013;
        klasa.Paralelja = 7;


    }

    [Test]

    public void Test_ShtoKlasa()
    {
        Assert.AreEqual(true, DAL.KlasaDB.Shto(klasa));    
    }

}      

}
`

This works perfectly, my question is how to do an Integration Testing using STUBS and MOCKERY because I don't have a clue how to do that?
Any suggestion or help from your side will be welcome.
Thank you in advance for your time and reply.
Cheers.

Tagged:

Comments

Sign In or Register to comment.