Getting "No tests were found error" when executing Test class in Junit5 - intellij-idea

Following is the pom.xml file I am using in my Intellij Idea maven project :
<?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>groupId</groupId>
<artifactId>code</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
Everything compiles fine. But when I run the Test file containing #Test annotations.
I get No tests were found error
Here is the test class :
package com.lab1;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PascalTriangleTests {
#Test
private void test(){
PascalTriangle pascalTriangle = new PascalTriangle();
pascalTriangle.generate(1);
assertEquals(pascalTriangle.getPascalTriangle().size(),1,"It should be one");
}
}
This is the class that if run it for some reason doesn't run the test function

Remove private modifier - it is not allowed in tests methods.

Related

selenium-maven : how to load Firefox or browser driver from resource folder

I am working in Selenium. It is demo project. I am using maven for dependency management. I have downloaded the Gecko driver and has kept to folder location in local drive. But it will create a dependency to with local machine.
We have have folder in maven, called resource. I want to use that. I want to keep the Firefox driver there and want to load it from here.
Maven Dependency
<?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>org.example</groupId>
<artifactId>maven-selemium</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Add Following Lines in Your POM File -->
<properties>
<selenium.version>2.53.1</selenium.version>
<testng.version>6.9.10</testng.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Java Code
public class PG1 {
public static void main(String[] args) throws Exception {
String baseUrl = "https://www.google.co/";
// System.setProperty("webdriver.gecko.driver", "C:/Users/Dell/Downloads/geckodriver-v0.29.1-win64/geckodriver.exe");
System.setProperty("webdriver.gecko.driver", "E:/Drive_H/projects/selenium/src/main/resources/geckodriver-firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.close();
}
}
I have tried with this line but error : unable to load driver.
System.setProperty("webdriver.gecko.driver", "geckodriver-firefox.exe");
Use System.getProperty("user.dir") to get the project user directory path and then access the desired executable.
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\src\\main\\resources\\geckodriver-firefox.exe");

Karate : Visual Studio Code: java.lang.ClassNotFoundException: com.intuit.karate.cli.Main

Error when I run the file.
An exception occured while executing the Java class. Using visual studio code for api tets
com.intuit.karate.cli.Main ->
java.lang.ClassNotFoundException: com.intuit.karate.cli.Main
at java.net.URLClassLoader.findClass (URLClassLoader.java:471)
at java.lang.ClassLoader.loadClass (ClassLoader.java:588)
at java.lang.ClassLoader.loadClass (ClassLoader.java:521)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:270)
at java.lang.Thread.run (Thread.java:834)
Running karate feature file in visual studio code.
This is how my pom.xml file looks
<?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>com.allegion.engage</groupId>
<artifactId>Engage</artifactId>
<version>0.0.1</version>
<name>Engage</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>0.9.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>0.9.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package com.allegion.engage.Login;
import com.intuit.karate.KarateOptions;
#KarateOptions(features = "classpath:Login/Login.feature")
public class LoginRunner{
}
Feature: Login and get the token
Scenario: Login and get the token
Given url loginUrl
And request {username: 'someusername', password: 'test' }
When method POST
Then status 200
And def authToken = response
Then print authToken
Kindly helo if I have missed any run configurations. I am using vscode in mac.
com.intuit.karate.cli.Main is only in 0.9.5 onwards, so can you point your dependencies to karate version 0.9.5.RC3
This is mentioned in the docs BTW: https://marketplace.visualstudio.com/items?itemName=kirkslota.karate-runner

Unable to resolve class - Groovy junit testing using maven

I am trying to create groovy tests where i use other methods from other Groovy scripts. Whenever i run mvn test i get unable to resolve class error, referring to the illegal import from another groovy script.
This is the testclass that I've written :
import jenkins_methods.gitMethods
import org.junit.Test
class GitMethodsTest extends GroovyTestCase{
GitMethodsTest(){
}
def gitMethods = new gitMethods()
#Test
void testGetDvhBranches(){
try{
gitMethods.getDVH_branches()
}
catch(Exception e){
println e
}
}
}
gitMethods.groovy
package jenkins_methods
// src/jobMethods
import dvh.util.AnsiColorDefinition
import jenkins_methods.fileMethods
String getGitDVH_BranchPath_flash() { return '/git-repository1/git/dvh/branches/' }
String dvhBranchNull() { return 'feature/DV-null-repo' }
String getDVH_branches() {
gitURL_DVH= "censored_url"
String response = sh returnStdout: true, script: "git ls-remote -h ${gitURL_DVH()}"
def branches = response.replaceAll(/[a-z0-9]*\trefs\/heads\//, ',')
branches = branches.substring(1, branches.length()).replaceAll("\n", "").replaceAll("\r", "")
return branches
}
}
pom.xml
<?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>groupId</groupId>
<artifactId>jenkins-jobs</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<configuration>
<providerSelection>2.0</providerSelection>
</configuration>
<goals>
<goal>addSource</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The jenkins_methods package contains various groovy scripts that are necessary for the Tests to become relevant.
Structure of the project is as following :
-src
-test
-groovy
-GitMethodsTest.groovy
-jenkins_methods
-gitMethods.groovy
This is the error that i get when i run mvn test
[ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.5:testCompile (default) on project jenkins-jobs: startup failed:
[ERROR] /C:/Users/jenkins-jobs/src/test/groovy/GitMethodsTest.groovy: 3: unable to resolve class jenkins_methods.gitMethods
[ERROR] # line 3, column 1.
[ERROR] import jenkins_methods.gitMethods
[ERROR] ^
[ERROR]
[ERROR] 1 error
How do i solve this problem?

Jersey 2.X Rest project with maven without web.xml Eclipse Photon

I am trying to implement a simple Jersey project using Jersey 2.16, and referring Javabrain Kaushik's tutorial of advanced JaxRs. Here the application invocation will be without web.xml but via #ApplicationPath annotation of javax. I am getting 404 while running the application
MyApp.java
package org.javabrains.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("webapi")
public class MyApp extends Application {
}
MyResource.java
package org.javabrains.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("test")
public class MyResource {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String testMethod() {
return "It works!";
}
}
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.javabrains</groupId>
<artifactId>advanced-jaxrs-01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>advanced-jaxrs-01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.16</version>
</dependency>
</dependencies>
<build>
<finalName>advanced-jaxrs-01</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I have tried to refer to old stackoverflow questions, but no luck.
I am running it in tomcat 8 and application url localhost:8082/advanced-jaxrs-01/webapi/test
Below is my project facet details

maven custom lifecycle is not recognized

I am trying to test a simple custom plugin with custom lifecycle.
Here is my EchoMojo.java file:
package org.sonatype.mavenbook.plugins;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
/**
* Echos an object string to the output screen.
* #goal echo
* #execute lifecycle="custom-echo" phase="package"
*/
public class EchoMojo extends AbstractMojo
{
/**
* Any Object to print out.
* #parameter expression="${echo.message}" default-value="Hello World..."
*/
private Object message;
public void execute()
throws MojoExecutionException, MojoFailureException
{
getLog().info( message.toString() );
}
}
My pom.xml looks like this:
<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/maven- v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.plugins</groupId>
<artifactId>echotest-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0-SNAPSHOT</version>
<name>echotest-plugin Maven Mojo</name>
<url>http://maven.apache.org</url>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I added this in my settings.xml:
<pluginGroups>
<pluginGroup>org.sonatype.mavenbook.plugins</pluginGroup>
</pluginGroups>
I created META-INF/maven/lifecycle.xml
<lifecycles>
<lifecycle>
<id>custom-echo</id>
<phases>
<phase>
<id>package</id>
<executions>
<execution>
<goals>
<goal>echo</goal>
</goals>
</execution>
</executions>
</phase>
</phases>
</lifecycle>
</lifecycles>
I have created META-INF/plexus/components.xml
<component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>custom-echo</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
<configuration>
<phases>
<package>org.sonatype.mavenbook.plugins:echotest-maven-plugin:echo</package>
</phases>
</configuration>
</component>
</components>
</component-set>
When i do mvn echotest:echo, I get "Build Sucessfull".
When i do mvn custom-echo, I get "[ERROR] Unknown lifecycle phase "custom-echo"."
All i wanted is to active my custom plugin when i execute "mvn custom-echo", where custom-echo is a custom lifecycle name
Can someone help me out please.