Lab Programs 3,4,5
Lab Programs 3,4,5
Dependency Management
Program-3:
Create a Java program to create a class called Person with properties
name and age. Display the person details in json format using Gson
libraray. Show how to configure the pom file in the Maven project to
automatically download the Gson dependencies .
Step 1: Create a New Maven Project:
Open IntelliJ IDEA.
Go to File > New > Project .
Select Maven from the project types.
Set the project name and location, then click Finish .
Step 3: Open the Main.java file under the directory MAVEN_DEMO\src\
main\java\com.example and write the following code
/* Main.java */
package com.example;
import com.google.gson.Gson;
public class Main
{
public static void main(String args[])
{
Gson gson= new Gson();
String json= gson.toJson(new Person("john",30) );
System.out.println(json);
}
}
class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name=name;
this.age=age;
}
}
This will tell Maven to include the Main-Class in the JAR manifest and
specify the main class that should be executed when the JAR is run.
Step-2: Create a Main Class
In your src/main/java/com.example/Main class, write this code.
package com.example;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello, this is a simple output from the main
class!");
}
}
This class contains a simple main method that prints output when run.
/* style.css */
body
{
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
}
header img
{
width: 100px;
}
/* WebsiteTitleTest.java */
package org.test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
public class WebPageTest
{
private static WebDriver driver;
@BeforeTest
public void openBrowser() throws InterruptedException
{
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
driver.get("https://sarvarbegum-coder.github.io/LAB_1/");
}
@Test
public void titleValidationTest()
{
String actualTitle = driver.getTitle();
String expectedTitle = "My simple website";
Assert.assertEquals(actualTitle, expectedTitle);
assertTrue(true, "Title should contain 'simple'");
}
@AfterTest
public void closeBrowser() throws InterruptedException
{
Thread.sleep(1000);
driver.quit();
}
}
Step 3: Run the Test Using TestNG
Option 1: Run TestNG from IntelliJ IDEA
1. Right-click the WebpageTest.java file.
2. Select Run WebpageTest.
IntelliJ IDEA will execute the TestNG test and show the results in the
output console.
Option 2: Run TestNG via Command Line
If you want to run the tests from the command line, use the following
Maven command:
mvn test