24
loading...
This website collects cookies to deliver better user experience
public static void assertEquals(String message, long expected, long actual)
public static void assertEquals(long expected, long actual, String message)
JUnit 4 | JUnit 5 |
---|---|
@ Before | @ BeforeEach |
@ After | @ AfterEach |
@ BeforeClass | @ BeforeAll |
@ AfterClass | @ AfterAll |
@ Ignore | @ Disabled |
@ Category | @ Tag |
@Test(expected = Exception.class)
public void testThrowsException() throws Exception {
}
@Test
void testThrowsException() throws Exception {
Assertions.assertThrows(Exception.class, () -> {
});
}
@RepeatedTest(5)
public void print(){
// write some code to be executed
}
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RunJUnitTest</groupId>
<artifactId>RunJUnitTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.28</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.28</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot -->
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
</project>
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
/* JUnit related imports */
import org.junit.*;
public class JUnit4Demo
{
static WebDriver driver = null;
static String URL = "https://lambdatest.github.io/sample-todo-app/";
public static String status = "passed";
static String username = "user-name";
static String access_key = "access-key";
@Before
public void beforeMethod() throws MalformedURLException {
System.out.println("=======Running @Before for JUnit 4=======");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "[Java] Demonstration of running JUnit4 tests on LambdaTest Grid");
capabilities.setCapability("name", "[Java] Demonstration of running JUnit4 tests on LambdaTest Grid");
capabilities.setCapability("platform", "Windows 10");
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("version","latest");
capabilities.setCapability("tunnel",false);
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("https://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"), capabilities);
/* driver = new ChromeDriver(); */
System.out.println("Started session");
}
@Test
public void simpleJunit4Test() throws InterruptedException
{
System.out.println("Running a simple Junit 4 test.");
driver.navigate().to(URL);
driver.manage().window().maximize();
try
{
/* Let's mark the first two items in the list. */
driver.findElement(By.name("li1")).click();
driver.findElement(By.name("li2")).click();
/* Let's add an item to the list. */
driver.findElement(By.id("sampletodotext")).sendKeys("Happy Testing at LambdaTest");
driver.findElement(By.id("addbutton")).click();
/* Let's check that the item we added is added in the list. */
String enteredText = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[6]/span")).getText();
if (enteredText.equals("Happy Testing at LambdaTest"))
{
System.out.println("Demonstration of running JUnit tests is complete");
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
@After
public void afterMethod()
{
System.out.println("=======Running @After for JUnit 4=======");
if (driver != null)
{
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
/* JUnit related imports */
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class RunUnitTests
{
static WebDriver driver = null;
static String URL = "https://lambdatest.github.io/sample-todo-app/";
public static String status = "passed";
static String username = "user-name";
static String access_key = "access-key";
@BeforeEach
public void beforeEachMethod() throws MalformedURLException {
System.out.println("=======Running @BeforeEach for JUnit 5=======");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "[Java] Demonstration of running JUnit5 tests on LambdaTest Grid");
capabilities.setCapability("name", "[Java] Demonstration of running JUnit5 tests on LambdaTest Grid");
capabilities.setCapability("platform", "Windows 10");
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("version","latest");
capabilities.setCapability("tunnel",false);
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("https://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"), capabilities);
/* driver = new ChromeDriver(); */
System.out.println("Started session");
}
@Test
public void simpleJunit5Test() throws InterruptedException
{
System.out.println("Running a simple Junit 5 test.");
driver.navigate().to(URL);
driver.manage().window().maximize();
try
{
/* Let's mark the first two items in the list. */
driver.findElement(By.name("li1")).click();
driver.findElement(By.name("li2")).click();
/* Let's add an item to the list. */
driver.findElement(By.id("sampletodotext")).sendKeys("Happy Testing at LambdaTest");
driver.findElement(By.id("addbutton")).click();
/* Let's check that the item we added is added in the list. */
String enteredText = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[6]/span")).getText();
if (enteredText.equals("Happy Testing at LambdaTest"))
{
System.out.println("Demonstration of running JUnit tests is complete");
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
@AfterEach
public void afterEachMethod()
{
System.out.println("=======Running @AfterEach for JUnit 5=======");
if (driver != null)
{
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}
}
}