CE103 Algorithms and Programming I

CE103 Algorithms and Programming I

Week-4

Fall Semester, 2021-2022

Download DOC, SLIDE, PPTX

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Introduction to Code Reusability and Automated Testing

During this course we will use entry level of shared library development and their tests and test automations. Also we will see TDD(Test Driven Development) approach.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

During this course we will use Windows OS, Eclipse and Visual Studio Commuity Edition environments for examples.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Each example will include two function

"Hello " printing function with name sayHelloTo(name) and

sum of two variable function for basic, sum = sum(a,b).

This sum function will add a to b and return result to sum variable.

We will locate them in library and use them from a console application, also we will create unit tests for testing their functionalities and return variables

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Shared Library Development

C Programming (Static Library)

Visual Studio Community Edition

In this sample we will create c-lib-sample project that contains library, executable, unit tests and unit test runners.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

First of all you install Visual Studio Community Edition from website

Visual Studio 2019 Community Edition - Son Ücretsiz Sürümü İndir

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Open visual studio community edition and select create a new project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select create a new project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select C++ static library from project list

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Name static library project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Default configuration come with C++ project types and setting

RTEU CE103 Week-4
CE103 Algorithms and Programming I

In the c-sample-lib.cpp you will sample function

void fncsamplelib()
{

}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

Delete pch.h and pch.c files. Also disable use precompiled header settings from configurations and change to "Not Using Precomplied Headers", also you can delete precomplied Header File.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Customize library header name and update "framework.h" to "samplelib.h"

Insert your functions inside the c-sample-lib.c and update header files also.

// c-sample-lib.cpp : Defines the functions for the static library.
//

#include "samplelib.h"
#include "stdio.h"

/// <summary>
/// 
/// </summary>
/// <param name="name"></param>
void sayHelloTo(char* name){

    if (name != NULL){
        printf("Hello %s \n",name);
    }
    else {
        printf("Hello There\n");
    }
}

/// <summary>
/// 
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
int sum(int a, int b){

    int c = 0;
    c = a + b;
    return c;
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

also update samplelib.h

#pragma once

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers

void sayHelloTo(char* name);
int sum(int a, int b);
RTEU CE103 Week-4
CE103 Algorithms and Programming I

If you check configuration you will see that for C complier we are using Microsoft Environment and Toolkits

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now we can compile our library

RTEU CE103 Week-4
CE103 Algorithms and Programming I

You can follow operation from output window

RTEU CE103 Week-4
CE103 Algorithms and Programming I

in debug folder we will see our output

RTEU CE103 Week-4
CE103 Algorithms and Programming I

now we will add a console application c-sample-app and use our library

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select C++ Windows Console Application from list

RTEU CE103 Week-4
CE103 Algorithms and Programming I

C++ Console Application Selection will generate a C++ console project we can change extension to C to compile our application as C application.

we will convert c-sample-app.c to following code

#include <stdio.h>

/// <summary>
/// 
/// </summary>
/// <returns></returns>
int main()
{
    printf("Hello World!\n");
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

after conversion set c-sample-app as startup project and build it

this will create c-sample-app.exe in the same folder with c-sample-lib.lib library

if we run application we will see only   "Hello World"

RTEU CE103 Week-4
CE103 Algorithms and Programming I

now we will see two options to add library as references in our application and use its functions.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

First option

right click references for c-sample-app and add current library as reference

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select Add Reference

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Browse for solution and select c-sample-lib

RTEU CE103 Week-4
CE103 Algorithms and Programming I

You can check added reference from references section

RTEU CE103 Week-4
CE103 Algorithms and Programming I

now we can include required headers from c-sample-lib folder and use it.

we can include required header with relative path as follow or with configuration

#include <stdio.h>
#include "..\c-sample-lib\samplelib.h"
/// <summary>
/// 
/// </summary>
/// <returns></returns>
int main()
{
    printf("Hello World!\n");
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

we can build our c-sample-app

RTEU CE103 Week-4
CE103 Algorithms and Programming I

also we can only write header name

#include <samplelib.h>
RTEU CE103 Week-4
CE103 Algorithms and Programming I

for this we need to configure include directories

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select c-sample-lib header file location

browse for folder

RTEU CE103 Week-4
CE103 Algorithms and Programming I

your full path will be added to your configuration

RTEU CE103 Week-4
CE103 Algorithms and Programming I

if you add header file paths to your configuration you can use header files by name in your source code

#include <stdio.h>
#include <samplelib.h>
/// <summary>
/// 
/// </summary>
/// <returns></returns>
int main()
{
    printf("Hello World!\n");
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

we can compile the following we don't have problems but here we need to configure relative paths for configuration open include library settings and update with relative path

..\c-sample-lib

RTEU CE103 Week-4
CE103 Algorithms and Programming I

now we have portable source code configuration. we can call our functions and then we can update header and library folder configurations.

#include <stdio.h>
#include <samplelib.h>
/// <summary>
/// 
/// </summary>
/// <returns></returns>
int main()
{
    int result = 0;
    //printf("Hello World!\n");
    result = sum(5, 4);
    sayHelloTo("Computer");
    printf("Result is %d \n",result);
    printf("Press any key to continue...\n");
    getchar();
    return 0;
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

when you run you will see the following outputs, that mean we called library functions.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

static library is a code sharing approach if you want to share your source code with your customers then you can share static libraries and header files together. Another case you can use a precompiled static library with you or this library can be part of any installation then if there is a installed app and static libraries are placed on system folder or any different location then you can use configuration files to set library path and included header paths

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now we can remove project from c-sample-app references but we will set library file in configuration

Before this copy static library and header files to a folder like that

DebugStaticLibDeployment
  • Set C/C++ -> General -> Additional Include Directories

There is a bug in configurations and relative path not finding headers so for this reason we will set full path but this is not a good practice for team working

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Not Working

..\c-sample-lib\DebugStaticLibDeployment

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Working

E:\...\c-lib-sample\c-sample-lib\DebugStaticLibDeployment

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now we will set library folder that our static library placed

we will set VC++ Directories -> Library Directories

Here is the same issue if we use relative path it doesn't work we need to set full path for library folder

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Working

E:\...\c-lib-sample\c-sample-lib\DebugStaticLibDeployment

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Not Working

..\c-sample-lib\DebugStaticLibDeployment

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

If we set full path for both libraries and headers then we need to set library name for project

Linker->Input->Additional Dependencies

In this case we will compile c-sample-app and we do not need to compile c-sample-lib because we copied output files to different location and they are ready to use.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

current source code will be like that nothing changed

#include <stdio.h>
#include <samplelib.h>

/// <summary>
/// 
/// </summary>
/// <returns></returns>
int main()
{
    int result = 0;
    //printf("Hello World!\n");
    result = sum(5, 4);
    sayHelloTo("Computer");
    printf("Result is %d \n",result);
    printf("Press any key to continue...\n");
    getchar();
    return 0;
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

and output

RTEU CE103 Week-4
CE103 Algorithms and Programming I

There is a option about portability that we can set for team works

We will remove all library related settings from configurations and we will write them in source code

Clear linker->general->additional library directories

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Clear C/C++ -> General -> Additional Include Directories

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Clear Linker->Input->Additional Dependencies

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now we can set this configurations in source code as follow

#pragma comment(lib, "..\\DebugStaticLibDeployment\\c-sample-lib.lib")
#include "..\DebugStaticLibDeployment\samplelib.h"

#include <stdio.h>

/// <summary>
/// 
/// </summary>
/// <returns></returns>
int main()
{
    int result = 0;
    //printf("Hello World!\n");
    result = sum(5, 4);
    sayHelloTo("Computer");
    printf("Result is %d \n",result);
    printf("Press any key to continue...\n");
    getchar();
    return 0;
}

with this configuration if your friends download this code then they can run them with their environment without setting a path.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

C++ Programming (Static Library)

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Visual Studio Community Edition

All steps are similar with C programming above, but you do not need to delete pch.h

You should take care about compiled source codes

for example if your code is compiled for x86 then your application also should use the x86 configuration else x64 then library should be x64 complied version.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Source will look like the following

// cpp-sample-app.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#pragma comment(lib, "..\\DebugStaticLibDeployment\\cpp-sample-lib.lib")

#include "..\DebugStaticLibDeployment\samplelib.h"

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";

    int result = 0;
    //printf("Hello World!\n");
    result = sum(5, 4);
    sayHelloTo("Computer");
    printf("Result is %d \n", result);
    printf("Press any key to continue...\n");
    getchar();
    return 0;

}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

C/C++ WSL Option

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Install WSL

GitHub - ucoruh/ns3-wsl-win10-setup: ns3 windows 10 WSL2 setup and usage

Create a Linux project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Configure Platform Toolset to WSL

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select GCC for Windows Subsystem for Linux

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Put a breakpoint and run debugger

RTEU CE103 Week-4
CE103 Algorithms and Programming I

In the debugger for WSL you can use local WSL installation but if you want to run it on Release setting it require a SSH connection.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Configure SSH parameters

RTEU CE103 Week-4
CE103 Algorithms and Programming I

so you have to complete the following steps.

C/C++ Remote Linux Option over SSH

Enable SSH

SSH on Windows Subsystem for Linux (WSL) | Illuminia Studios

Connect to Remote WSL Environment

Bağlan hedef Linux sisteminize Visual Studio | Microsoft Docs

RTEU CE103 Week-4
CE103 Algorithms and Programming I

C# Programming (Dinamik Library)

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Visual Studio Community Edition

In C# project we will create class library we have several options

for this sample we will select .NET core that we can build cross platform library

RTEU CE103 Week-4
CE103 Algorithms and Programming I

There is no static library option

RTEU CE103 Week-4
CE103 Algorithms and Programming I

We will select .Net Core 3.1

RTEU CE103 Week-4
CE103 Algorithms and Programming I

You will have default empty class library file

RTEU CE103 Week-4
CE103 Algorithms and Programming I

In the project you can see .NETcore reference

RTEU CE103 Week-4
CE103 Algorithms and Programming I

We can build empty class library that generate dll for our application

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now we will add Console Application but this will also use .NETCore

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select New Project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Name the project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select .NETCore framework

RTEU CE103 Week-4
CE103 Algorithms and Programming I

You will have the following sample main.cs file

using System;

namespace csharp_sample_app
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now we can link projects with adding references open reference section

RTEU CE103 Week-4
CE103 Algorithms and Programming I

browse for class library project output folder and select output dll file for console application

RTEU CE103 Week-4
CE103 Algorithms and Programming I

now we can update our library code and use it in console application

copy following sample to sampleLibClass file in the library

using System;

namespace csharp_sample_lib
{
    public class sampleLibClass
    {
        public static void sayHelloTo(string name)
        {
            if (!String.IsNullOrEmpty(name))
            {
                Console.WriteLine("Hello " + name);
            }
            else
            {
                Console.WriteLine("Hello There");
            }

        }

        public static int sum(int a, int b)
        {
            int c = 0;
            c = a + b;
            return c;
        }
    }
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

after this operation copy following sample to console application and build app then you can run

using csharp_sample_lib;
using System;

namespace csharp_sample_app
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            sampleLibClass.sayHelloTo("Computer");
            int result = sampleLibClass.sum(5, 4);
            Console.WriteLine("Results is" + result);
            Console.WriteLine("Results is {0}", result);
            Console.Read();
        }
    }
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

You will see following output that mean we called DLL functions

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Also we can publish this console application with dll for linux environment or others

for linux environment we should install .NETCore

RTEU CE103 Week-4
CE103 Algorithms and Programming I

follow the link below or commands that I shared with you as below for deployment

How to Install Dotnet Core on Ubuntu 20.04 – TecAdmin

Step 1 – Enable Microsoft PPA

 wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb 
sudo dpkg -i packages-microsoft-prod.deb
RTEU CE103 Week-4
CE103 Algorithms and Programming I

Step 2 – Installing Dotnet Core SDK

sudo apt update 
sudo apt install apt-transport-https 
sudo apt install dotnet-sdk-3.1 
RTEU CE103 Week-4
CE103 Algorithms and Programming I

Step 3 – Install Dotnet Core Runtime Only

To install .NET Core Runtime on Ubuntu 20.04 LTS system, execute the commands:

sudo apt update 
RTEU CE103 Week-4
CE103 Algorithms and Programming I

To install the previous version of .Net core runtime 2.1, type:

sudo apt install dotnet-runtime-2.1

Press “y” for any input prompted by the installer.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Step 4 – (Optional) Check .NET Core Version

You can use dotnet command line utility to check installed version of .NET Core on your system. To check dotnet version, type:

dotnet --version

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now we will publish our application as single executable

Open publish menu

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select netcoreapp3.1 and Release for linux-x64

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select produce single file

RTEU CE103 Week-4
CE103 Algorithms and Programming I

After succesfull publish you will have linux binary that you can run with WSL

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Open WSL and enter the path where this folder located

and run application as follow

RTEU CE103 Week-4
CE103 Algorithms and Programming I

check dotnet --version and then run application

you will see similar output

RTEU CE103 Week-4
CE103 Algorithms and Programming I

In this sample we created single application from settings lets try with shared library located option uncheck the "produce single file" option and publish again.

Then you will have the following outputs

RTEU CE103 Week-4
CE103 Algorithms and Programming I

If you run csharp-sample-app

you will have the same output

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Java Programming

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Eclipse IDE

You should download and install eclipse installer and then you should select Eclipse IDE for Java Developers

Eclipse Installer 2021-09 R | Eclipse Packages

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select create a project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select java project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

give project name

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select finish

RTEU CE103 Week-4
CE103 Algorithms and Programming I

first we need to add a default package to keep everything organized

RTEU CE103 Week-4
CE103 Algorithms and Programming I

then we can create our class that includes our functions

RTEU CE103 Week-4
CE103 Algorithms and Programming I

give class a name

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you will have following class with main

RTEU CE103 Week-4
CE103 Algorithms and Programming I

We will create sample java library with static functions as below.

package ce103;

import java.io.IOException;

public class JavaSampleLib {

    public static void sayHelloTo(String name) {
        if(name.isBlank() || name.isEmpty())
        {
            System.out.println("Hello "+name);
        }else {
            System.out.println("Hello There");
        }
    }

    public static int sum(int a,int b)
    {
        int c = 0;
        c = a+b;
        return c;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Hello World!");

        JavaSampleLib.sayHelloTo("Computer");
        int result = JavaSampleLib.sum(5, 4);
        System.out.println("Results is" + result);
        System.out.printf("Results is %d \n", result);


        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

also we can add main method to run our library functions. If we run this file its process main function

RTEU CE103 Week-4
CE103 Algorithms and Programming I

we can see output from console as below

RTEU CE103 Week-4
CE103 Algorithms and Programming I

There is no exe files java runtime environment run class files but we can export this as an executable.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select Java->Runnable JAR File

RTEU CE103 Week-4
CE103 Algorithms and Programming I

click next and set output path for jar file

RTEU CE103 Week-4
CE103 Algorithms and Programming I

If our project has several external dependecy then we can extract this required files (jar, so, dll) in seperated folder or we can combine them and generate a single executable jar

Lets pack everthing together, Select launch configuration that has main function

RTEU CE103 Week-4
CE103 Algorithms and Programming I

end of this operation we will have the following jar that we can by click

RTEU CE103 Week-4
CE103 Algorithms and Programming I

When you click application if cannot run then try command line to see problem

enter jar folder and run the following command

java -jar JavaSampleLibExecutable.jar

In my case eclipse build JDK is newer than that I installed and set for my OS

If we check version we can see problem Java version 1.8.0_231

RTEU CE103 Week-4
CE103 Algorithms and Programming I

We can found installed and builded JDK for our application from Eclipse setting

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select environments

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select installed JRE or JDK

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you can see installed JRE or JDK home

C:\Program Files\Java\jdk-16.0.1

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Open system environment to fix this problem

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Check user settings first

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Check system settings

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

we will move up the JDK 16 configuration then command line will run first java

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Also in system setting check JAVA_HOME

RTEU CE103 Week-4
CE103 Algorithms and Programming I

After this settings close current command line and open new one

write

java --version

if you see java version updated and 16.0.1 then settings are correct

RTEU CE103 Week-4
CE103 Algorithms and Programming I

and now if we enter and run application as follow we will see output

RTEU CE103 Week-4
CE103 Algorithms and Programming I

But when you click this jar its not running as you see so we have options to provide a clickable application there

Launch4j is an option here

Launch4j - Cross-platform Java executable wrapper

How to use Launch4

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you can watch this tutorial also

How to convert jar to exe using Launch4J Full explanation - YouTube

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Download and install launch4j and open application

RTEU CE103 Week-4
CE103 Algorithms and Programming I

configure your application settings similar to below select jar file and exe output path

RTEU CE103 Week-4
CE103 Algorithms and Programming I

we can customize main class if have multiple main class

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select console from setting for this application

RTEU CE103 Week-4
CE103 Algorithms and Programming I

we can provide a single running application, this setting avoid to run multiple instances

RTEU CE103 Week-4
CE103 Algorithms and Programming I

we need to set runtime environment versions

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you can set system parameters before running application

RTEU CE103 Week-4
CE103 Algorithms and Programming I

with splash screen you can show a splash screen image for your application

RTEU CE103 Week-4
CE103 Algorithms and Programming I

File attributes such as version product information is configured from version info tab

RTEU CE103 Week-4
CE103 Algorithms and Programming I

if your application runtime condition has an error then you can show this customized messages also

RTEU CE103 Week-4
CE103 Algorithms and Programming I

with this options save configuration file xml

RTEU CE103 Week-4
CE103 Algorithms and Programming I

and compile settings

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you will see generated output file in log screen

Compiling resources
Linking
Wrapping
WARNING: Sign the executable to minimize antivirus false positives or use launching instead of wrapping.
Successfully created C:\Users\ugur.coruh\Desktop\java-export-sample\JavaSampleLibExecutable.exe
RTEU CE103 Week-4
CE103 Algorithms and Programming I

now we can run exe by click

RTEU CE103 Week-4
CE103 Algorithms and Programming I

another option here adding a bat file to run current jar file

RTEU CE103 Week-4
CE103 Algorithms and Programming I

JavaSampleLibExecutable.bat

java -jar JavaSampleLibExecutable.jar

if we click bat file then we will automate command line task for current jar file

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now return back to our java library and create another console application that use library functions

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you can set libraries in this step from but our library should exported for our solution

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select Add External JARs...

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Open Exported jar folder and select

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Or we can select by Add jar from current workspace

RTEU CE103 Week-4
CE103 Algorithms and Programming I

but in this step I won't add anything I'll add references later

RTEU CE103 Week-4
CE103 Algorithms and Programming I

we will have the following project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

lets create a package

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

and lets create a main class for our application

RTEU CE103 Week-4
CE103 Algorithms and Programming I

check create main function

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

right click to project and add reference

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you can enter same configurations from project properties

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Lets export our library as a JAR file and then add to our classpath

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select JAR file

RTEU CE103 Week-4
CE103 Algorithms and Programming I

we configured output as

C:\Users\ugur.coruh\Desktop\java-export-sample\JavaSampleLib.jar

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

In the same export folder now we have JavaSampleLib.jar

RTEU CE103 Week-4
CE103 Algorithms and Programming I

return back to java-sample-app and then add this jar file to our project

Build Path->Add External Archives

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you will see its added to reference libraries

RTEU CE103 Week-4
CE103 Algorithms and Programming I

in our JavaSampleApp.java we can use the following source codes

package ce103;

import java.io.IOException;

public class JavaSampleApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("Hello World!");

        JavaSampleLib.sayHelloTo("Computer");
        int result = JavaSampleLib.sum(5, 4);
        System.out.println("Results is" + result);
        System.out.printf("Results is %d \n", result);


        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

When we run application we will see similar output

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Lets export this application with its dependent library

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select runnable jar

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Set Launch configuration and Export destination

C:\Users\ugur.coruh\Desktop\java-export-sample\JavaSampleAppSingle.jar

RTEU CE103 Week-4
CE103 Algorithms and Programming I

In this option we will have single jar file

In the export folder we do not see reference libraries

and we can run with command line

RTEU CE103 Week-4
CE103 Algorithms and Programming I

only change copy required libraries setting and then give a new name for new jar file and export

C:\Users\ugur.coruh\Desktop\java-export-sample\JavaSampleAppMultiple.jar

RTEU CE103 Week-4
CE103 Algorithms and Programming I

now we have a folder that contains our libraries referenced

RTEU CE103 Week-4
CE103 Algorithms and Programming I

in this file we can find our library

RTEU CE103 Week-4
CE103 Algorithms and Programming I

if we test our application we will see it will work

if we delete JavaSampleLib.jar and then try running application we will get error

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Program Testing

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Unit Test Development

RTEU CE103 Week-4
CE103 Algorithms and Programming I

C Unit Tests

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Visual Studio Community Edition

RTEU CE103 Week-4
CE103 Algorithms and Programming I

C++ Unit Tests

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Visual Studio Community Edition

C/C++ için birim testleri yazma - Visual Studio (Windows) | Microsoft Docs

Use cpp-sample-lib project and add

RTEU CE103 Week-4
CE103 Algorithms and Programming I

select Native Unit Test

RTEU CE103 Week-4
CE103 Algorithms and Programming I

set project path and name

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you will have cpp-sample-test project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

add library project from references

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Add cpp-sample-lib to cpp-sample-test project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

cpp-sample-test.cpp

#include "pch.h"
#include "CppUnitTest.h"
#include "..\cpp-sample-lib\samplelib.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace cppsampletest
{
    TEST_CLASS(cppsampletest)
    {
    public:

        TEST_METHOD(TestSumCorrect)
        {
            Assert::AreEqual(9, sum(4, 5));
        }

        TEST_METHOD(TestSumInCorrect)
        {
            Assert::AreEqual(10, sum(4, 5));
        }
    };
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

C# Unit Tests

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Visual Studio Community Edition (MSTestV2+.Net)

Install extension fine code coverage

https://marketplace.visualstudio.com/items?itemName=FortuneNgwenya.FineCodeCoverage

Create a .Net Framework Library

RTEU CE103 Week-4
CE103 Algorithms and Programming I

set project framework and path

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Create library functions

RTEU CE103 Week-4
CE103 Algorithms and Programming I
using System;
using System.Collections.Generic;
using System.Text;

namespace cs_lib_sample
{
    public class SampleLibClass
    {
        public static string sayHelloTo(string name)
        {
            string result = String.Empty;

            if (!String.IsNullOrEmpty(name))
            {
                result = "Hello " + name;
            }
            else
            {
                result = "Hello There";
            }

            Console.WriteLine(result);

            return result;
        }

        public static int sum(int a, int b)
        {
            int c = 0;
            c = a + b;
            return c;
        }

        public int multiply(int a, int b)
        {
            return a * b;
        }
    }
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

right click and then create unit test project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

press OK

RTEU CE103 Week-4
CE103 Algorithms and Programming I

enter test code

using Microsoft.VisualStudio.TestTools.UnitTesting;
using cs_lib_sample;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cs_lib_sample.Tests
{
    [TestClass()]
    public class SampleLibClassTests
    {

        [TestMethod()]
        public void testSayHelloTo()
        {


            Assert.AreEqual("Hello Computer", SampleLibClass.sayHelloTo("Computer"), "Regular say hello should work");
        }
        [TestMethod()]
        public void testSayHelloToWrong()
        {
            Assert.AreEqual("Hello All", SampleLibClass.sayHelloTo("Computer"), "Regular say hello won't work");
        }


        [TestMethod()]
        public void testSumCorrect()
        {
            Assert.AreEqual(9, SampleLibClass.sum(4, 5), "Regular sum should work");
        }

        [TestMethod()]
        public void testSumWrong()
        {
            Assert.AreEqual(10, SampleLibClass.sum(4, 5), "Regular sum shouldn't work");
        }

        [TestMethod()]
        public void testMultiply()
        {
            SampleLibClass sampleLib = new SampleLibClass();

            Assert.AreEqual(20, sampleLib.multiply(4, 5), "Regular multiplication should work");
        }


    }
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

Run tests

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you will code coverage and entered or passed branches

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Visual Studio Community Edition (NUnit+.NETCore)

use cshar-sample-lib for this example

create and add a unit test project to solution

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Add project reference

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

SampleLibraryTestClasss in NUnit Project

using csharp_sample_lib;
using NUnit.Framework;

namespace csharp_sample_lib_test
{
    public class SampleLibraryTestClass
    {
        sampleLibClass sampleLib;

        [SetUp]
        public void Setup()
        {
            sampleLib = new sampleLibClass();
        }

        [Test]
        public void testSayHelloTo()
        {
            Assert.AreEqual("Hello Computer", sampleLibClass.sayHelloTo("Computer"), "Regular say hello should work");
        }

        [Test]
        public void testSayHelloToWrong()
        {
            Assert.AreEqual("Hello All", sampleLibClass.sayHelloTo("Computer"), "Regular say hello won't work");
        }


        [Test]
        public void testSumCorrect()
        {
            Assert.AreEqual(9, sampleLibClass.sum(4, 5), "Regular sum should work");
        }

        [Test]
        public void testSumWrong()
        {
            Assert.AreEqual(10, sampleLibClass.sum(4, 5), "Regular sum shouldn't work");
        }

        [Test]
        public void testMultiply()
        {
            Assert.AreEqual(20, sampleLib.multiply(4, 5), "Regular multiplication should work");
        }
    }
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

sample class library

using System;

namespace csharp_sample_lib
{
    public class sampleLibClass
    {
        public static string sayHelloTo(string name)
        {
            string result = String.Empty;

            if (!String.IsNullOrEmpty(name))
            {
                result = "Hello " + name;
            }
            else
            {
                result = "Hello There";
            }

            Console.WriteLine(result);

            return result;
        }

        public static int sum(int a, int b)
        {
            int c = 0;
            c = a + b;
            return c;
        }

        public int multiply(int a, int b)
        {
            return a * b;
        }
    }
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

Open test explorer and run tests

RTEU CE103 Week-4
CE103 Algorithms and Programming I

or you can run from project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Also we can create unit test from library class,

right click the sampleLibClass and select create unit tests but this option do not provide nunit tests.

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I
using Microsoft.VisualStudio.TestTools.UnitTesting;
using csharp_sample_lib;
using System;
using System.Collections.Generic;
using System.Text;

namespace csharp_sample_lib.Tests
{
    [TestClass()]
    public class sampleLibClassTests
    {
        [TestMethod()]
        public void sayHelloToTest()
        {
            Assert.Fail();
        }

        [TestMethod()]
        public void sumTest()
        {
            Assert.Fail();
        }

        [TestMethod()]
        public void multiplyTest()
        {
            Assert.Fail();
        }
    }
}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

we will not commit this changes and continue from nunit test project, the fine code

coverage also work for nunit test but not provide inline highlighting

if we run tests we will have the following outputs

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Inline code highlight is part of enterprise visual studio edition

Analyzing code coverage in Visual Studio - DEV Community

RTEU CE103 Week-4
CE103 Algorithms and Programming I

TL;DR

Additional information you can use OpenCover + Nunit Runner + Report Generator together to setup a code coverage report but it has complex batch running process. After a few try I decided to use fine code coverage but here is the usage not tested well.

First unit test runner tool doesn't support .Net Core

c# - The NUnit 3 driver encountered an error while executing reflected code (NUnit.Engine.NUnitEngineException) - Stack Overflow

Follow the instructions on the link

CMD OpenCover · sukhoi1/Useful-Notes Wiki · GitHub

Install OpenCover, ReportGenerator, Nunit,Runners packages then use the package installation folder to get tools that you need

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Here is a sample for open cover, select package and copy path

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Goto path and tools

C:\Users\ugur.coruh\.nuget\packages\opencover\4.7.1221

You need to setup some batch similar with following

run-test-coverage.bat

set pathA=C:\Users\ugur.coruh\.nuget\packages\opencover\4.7.1221\tools
set pathB=C:\Users\ugur.coruh\.nuget\packages\nunit.consolerunner\3.12.0\tools
set pathC=C:\Users\ugur.coruh\.nuget\packages\reportgenerator\4.8.13\tools\netcoreapp3.0
set dllpath=C:\Users\ugur.coruh\Desktop\csharp-sample-lib\csharp-sample-lib-test\bin\Debug\netcoreapp3.1

"%pathA%\OpenCover.Console.exe" ^
-targetargs:"%dllpath%\csharp-sample-lib-test.dll" ^
-filter:"+[csharp-sample-lib*]* -[*test]*" ^
-target:"%pathB%\nunit3-console.exe" ^
-output:"%dllpath%\coverReport.xml" ^
-skipautoprops -register:user && "%pathC%\ReportGenerator.exe" -reports:"%dllpath%\coverReport.xml" -targetdir:""%dllpath%\coverage"
pause
RTEU CE103 Week-4
CE103 Algorithms and Programming I

but nunit3-console.exe gives error

RTEU CE103 Week-4
CE103 Algorithms and Programming I

for this compatibility issues I prefer to use fine code coverage extension.

OpenCover related studies

Code coverage of manual or automated tests with OpenCover for .NET applications – Automation Rhapsody

Code coverage of .NET Core unit tests with OpenCover – Automation Rhapsody

Sample OpenCover report

Summary - Coverage Report

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Download and Setup OpenCover, NUnit Console, Report Generator without Package Manager

You can also download the tools from github project pages and install on your operating system,

RTEU CE103 Week-4
CE103 Algorithms and Programming I

OpenCover

Releases · OpenCover/opencover · GitHub

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Select advanced and then install for all users

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

ReportGenerator

Release ReportGenerator_4.8.13 · danielpalme/ReportGenerator · GitHub

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

NUnit Console

Downloads

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

NUnit + MSTest Batch Report Generation (Not Tested)

OpenCover and ReportGenerator Unit Test Coverage in Visual Studio 2013 and 2015 – CodeHelper.Net

OpenCover and ReportGenerator Unit Test Coverage in Visual Studio 2013 and 2015 - CodeProject

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Java Unit Tests

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Eclipse IDE (JUnit4 , JUnit5)

In this sample we will create two example for similar library

Please check the following links

JUnit 5 tutorial - Learn how to write unit tests

JUnit 5

JUnit 5 User Guide

https://www.eclemma.org/

JUnit Hello World Example - Examples Java Code Geeks - 2021

https://yasinmemic.medium.com/java-ile-unit-test-yazmak-birim-test-ca15cf0d024b

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Java Application + JUnit

In normal java application we can right click the project java-sample-lib and add Junit case

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

and you will have the following test class

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now we will create tests that check our function flowchart and return values

We need to cover all code branches that we coded

I have updated JavaSampleLib.java as follow to check outputs

JavaSampleLib.java

package ce103;

public class JavaSampleLib {

    public static String sayHelloTo(String name) {

        String output = "";

        if(!name.isBlank() && !name.isEmpty()){
            output = "Hello "+name;
        }else {
            output = "Hello There";
        }

        System.out.println(output);

        return output;
    }

    public static int sum(int a,int b)
    {
        int c = 0;
        c = a+b;
        return c;
    }

    public int multiply(int a, int b) {
        return a * b;
    }

//    public static void main(String[] args) {
//        // TODO Auto-generated method stub
//        System.out.println("Hello World!");
//
//        JavaSampleLib.sayHelloTo("Computer");
//        int result = JavaSampleLib.sum(5, 4);
//        System.out.println("Results is" + result);
//        System.out.printf("Results is %d \n", result);
//        
//        
//        try {
//            System.in.read();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//
//    }

}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

and JavaSampleLibTest.java

package ce103;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

class JavaSampleLibTest {

    JavaSampleLib sampleLib;

    @BeforeAll
    static void setUpBeforeClass() throws Exception {
    }

    @AfterAll
    static void tearDownAfterClass() throws Exception {
    }

    @BeforeEach
    void setUp() throws Exception {
        sampleLib = new JavaSampleLib();
    }

    @AfterEach
    void tearDown() throws Exception {
    }

    @Test
    @DisplayName("Simple Say Hello should work")
    void testSayHelloTo() {
        assertEquals("Hello Computer", JavaSampleLib.sayHelloTo("Computer"), "Regular say hello should work");
    }

    @Test
    @DisplayName("Simple Say Hello shouldn' work")
    void testSayHelloToWrong() {
        assertEquals("Hello All", JavaSampleLib.sayHelloTo("Computer"), "Regular say hello won't work");
    }


    @Test
    @DisplayName("Simple sum should work")
    void testSumCorrect() {
        assertEquals(9, JavaSampleLib.sum(4, 5), "Regular sum should work");
    }

    @Test
    @DisplayName("Simple sum shouldn't work")
    void testSumWrong() {
        assertEquals(10, JavaSampleLib.sum(4, 5), "Regular sum shouldn't work");
    }

    @Test
    @DisplayName("Simple multiplication should work")
    void testMultiply() {
        assertEquals(20, sampleLib.multiply(4, 5), "Regular multiplication should work");
    }

    @RepeatedTest(5)
    @DisplayName("Ensure correct handling of zero")
    void testMultiplyWithZero() {
        assertEquals(0, sampleLib.multiply(0, 5), "Multiple with zero should be zero");
        assertEquals(0, sampleLib.multiply(5, 0), "Multiple with zero should be zero");
    }

    public static int[][] data() {
        return new int[][] { { 1, 2, 2 }, { 5, 3, 15 }, { 121, 4, 484 },{ 2, 2, 2 } };
    }

    @ParameterizedTest
    @MethodSource(value = "data")
    void testWithStringParameter(int[] data) {
        JavaSampleLib tester = new JavaSampleLib();
        int m1 = data[0];
        int m2 = data[1];
        int expected = data[2];
        assertEquals(expected, tester.multiply(m1, m2));
    }

}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

if we run tests

RTEU CE103 Week-4
CE103 Algorithms and Programming I

we will see all results there

RTEU CE103 Week-4
CE103 Algorithms and Programming I

also we can see the code coverage of tests

RTEU CE103 Week-4
CE103 Algorithms and Programming I

when we open our source code (just close and open again another case highlighting will not work) you will see tested part of your codes

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Maven Java Application + JUnit

Lets create Maven project with tests

Create a maven project

File -> New -> Maven Project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Lets convert our sample java-sample-lib directories to standard folder structure for test and app division

Maven – Introduction to the Standard Directory Layout

Also for intro you can use this

JUnit Hello World Example - Examples Java Code Geeks - 2021

Eclipse
Maven
Java
JUnit 4.12 (pulled by Maven automatically)

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Lets give new sample java-sample-lib-mvnbut in this time we will create a maven project

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ce103</groupId>
  <artifactId>java-sample-lib-ext</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Java Sample Lib</name>
  <description>Java Sample with Unit Test</description>
</project>
RTEU CE103 Week-4
CE103 Algorithms and Programming I

we will add JUnit 5 for our project

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ce103</groupId>
    <artifactId>java-sample-lib-ext</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Java Sample Lib</name>
    <description>Java Sample with Unit Test</description>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
RTEU CE103 Week-4
CE103 Algorithms and Programming I

it will automatically download libraries

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Create java sample library in ce103 package, first create java package

RTEU CE103 Week-4
CE103 Algorithms and Programming I

In this package create library class

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

copy content from other library

package ce103;

public class JavaSampleLib {

public static String sayHelloTo(String name) {

        String output = "";

        if(!name.isBlank() && !name.isEmpty()){
            output = "Hello "+name;
        }else {
            output = "Hello There";
        }

        System.out.println(output);

        return output;
    }

    public static int sum(int a,int b)
    {
        int c = 0;
        c = a+b;
        return c;
    }

    public int multiply(int a, int b) {
        return a * b;
    }


}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

Now lets create tests inf src/test/java

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

create a JUnit Case

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

you will simple template

package ce103;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class JavaSampleLibTest {

    @BeforeAll
    static void setUpBeforeClass() throws Exception {
    }

    @AfterAll
    static void tearDownAfterClass() throws Exception {
    }

    @BeforeEach
    void setUp() throws Exception {
    }

    @AfterEach
    void tearDown() throws Exception {
    }

    @Test
    void testSayHelloTo() {
        fail("Not yet implemented");
    }

    @Test
    void testSum() {
        fail("Not yet implemented");
    }

    @Test
    void testMultiply() {
        fail("Not yet implemented");
    }

}
RTEU CE103 Week-4
CE103 Algorithms and Programming I

now lets copy tests from other projects


RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

RTEU CE103 Week-4
CE103 Algorithms and Programming I

That's a part of java unit testing...

RTEU CE103 Week-4
CE103 Algorithms and Programming I

TDD (Test Driven Development)

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Test and Deployment Automation Management

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Travis-CI + C

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Travis-CI + Cpp

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Travis-CI + C#

RTEU CE103 Week-4
CE103 Algorithms and Programming I

Travis-CI + Java

RTEU CE103 Week-4
CE103 Algorithms and Programming I

References

GitHub - MicrosoftDocs/cpp-docs: C++ Documentation

RTEU CE103 Week-4