package org.junit doesn't exists in vscode - java

I get this error package org.junit doesn't exists in vscode when I import
import junit.framework.Test; or import static org.junit.Assert.assertEquals;
this is my test file
package com.wezigo.myapp.service;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class MyServiceTest {
#Test
public void testCompute(){
MyService service =new MyService();
//MyService service = new MyService();
double a = 15;
double b = 15;
double expected = 20;
double result = service.compute(a,b);
assertEquals(expected, result,0.001 );
}
}
this is pom.xml file content
<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>com.wezigo</groupId>
<artifactId>myapp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
i try the solution in this address
**Why does vscode not recognize the import org.junit?
** but i use vscode 1.60.0 and this command is not availlable View -> Command Palette -> Java: Clean Java Language Server Workspace
when compile, i get thes error
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.083 s
[INFO] Finished at: 2021-09-24T12:10:31+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/home/rodolphe/javaTraining/work1). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

Try
package com.wezigo.myapp.service;
public class MyServiceTest {
public void testCompute(){
MyService service =new MyService();
double a = 15;
double b = 15;
double expected = 20;
double result = service.compute(a,b);
assert Math.abs(expected - result) <= 0.001;
}
}
A popular fallacy is that you need junit for unit testing.

Related

Running an Eclipse Java Maven project from a Windows (DOS) shell

Summary: How to run an Eclipse Java Maven Project outside of Eclipse
Details:
I am a Maven newbie and I have created a very simple Maven project in Eclipse.
The Package Explorer view of my project is as follows:
The source code for the two simple Java files is as follows:
package Pkg01;
public class Calculator
{
public double add(double number1, double number2)
{
return number1 + number2;
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
System.out.println(calculator.add(100, 200));
}
}
and
package Pkg01;
import junit.framework.TestCase;
public class TestCalculator extends TestCase
{
public void testAdd()
{
Calculator calculator = new Calculator();
double result = calculator.add(10, 50);
assertEquals(60, result, 0);
}
}
From within Eclipse I am able to run the Java Application, the Junit Test and the Maven test, without any issues.
My question is how can I invoke the mvn command from the DOS shell so that I can:
Run the Java Application
Run the Junit Test
Run the Maven test
BTW running mvn exec:java from the parent directory of the project is not executing the Java application for me.
My pom.xml file 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.newbie</groupId>
<artifactId>Stackoverflow</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Stackoverflow</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
mvn exec:java gives the following error:
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< org.newbie:Stackoverflow >----------------------
[INFO] Building Stackoverflow 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) # Stackoverflow ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.957 s
[INFO] Finished at: 2019-11-05T22:47:00-06:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project Stackoverflow: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginParameterException
Here are the reasons why I feel that my question is not a duplicate.
I created a very simple project with maven-archetype-quickstart and my environment is correct because everything {i.e. (1) Run the Java Application, (2) Run the Junit Test and (3) Run the Maven test} works for me within the Eclipse IDE environment. The other poster was having trouble executing, which I don’t.
The comments that the other poster received were mainly for fixing his environment to get it running. I don't have that issue since, as already mentioned, everything works for me within the Eclipse IDE environment.
All I am asking is what I need to do get the following things working from the command shell:
Running the Java Application
Running the Junit Test
Running the Maven test
The error message states parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin.If you look at the exec:java documentation the required parameter is mainClass
I don't see exec-maven-plugin being configured in your pom.xml so you could execute Java programs using below command
mvn exec:java -Dexec.mainClass="com.example.Main"
Make sure you have compiled you code either using mvn compile or mvn install command before running mvn exec.. command
C:\data\development\app_code\my-app>mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) # my-app ---
300.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.960 s
[INFO] Finished at: 2019-11-06T09:55:23+00:00
[INFO] Final Memory: 9M/116M
[INFO] ------------------------------------------------------------------------
C:\Sachin\data\development\app_code\test-maven\my-app>
For Junit testing you could call mvn test which would give you the test results status similar to your Eclipse IDE
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.099 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.198 s
[INFO] Finished at: 2019-11-06T09:55:58+00:00
[INFO] Final Memory: 19M/160M
[INFO] ------------------------------------------------------------------------
I do see the same details being mentioned in the link by Karthikeyan Vaithilingam
Reference:
https://www.mojohaus.org/exec-maven-plugin/usage.html
OP Addendum
You have provided the solution for:
1. Running the Java Application
3. Running the Maven test
From the IDE I can also invoke:
2. Running the Junit Test
Admittedly this is very similar to 3. Running the Maven test but the output looks different.
Is there a way to do that from outside of the IDE environment?

Error when implementing springboot with H2db

I am new to coding. I am getting below error when trying to run java application in springboot with H2db.
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 8.777 s <<< FAILURE! - in com.example.demo.DemoApplicationTests
[ERROR] contextLoads(com.example.demo.DemoApplicationTests) Time elapsed: 0.002 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] DemoApplicationTests.contextLoads » IllegalState Failed to load ApplicationCon...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.028 s
[INFO] Finished at: 2019-05-26T22:46:54+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project demo: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Users\v\Desktop\demo\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Here is my 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application :
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.example.demo.StudentRepository;
#SpringBootApplication
#EntityScan("com.example.demo.Student")
#EnableJpaRepositories("com.example.demo.StudentRepository")
public class H2demoApplication implements CommandLineRunner {
// mvn spring-boot:run
private Logger LOG = LoggerFactory.getLogger("H2demoApplication");
StudentRepository studentRepository;
#Autowired
public H2demoApplication(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public static void main(String[] args) {
SpringApplication.run(H2demoApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
LOG.info("Student count in DB: {}", studentRepository.count());
}
}
Entity:
package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
#Entity
public class Student {
#Id
#GeneratedValue
private Long ID;
private String NAME;
private String SECTION;
public Student() {
}
public Student(Long ID, String NAME, String SECTION) {
this.ID = ID;
this.NAME = NAME;
this.SECTION = SECTION;
}
public Long getId() {
return ID;
}
public void setId(Long ID) {
this.ID = ID;
}
public String getName() {
return NAME;
}
public void setName(String NAME) {
this.NAME = NAME;
}
public String getSection() {
return SECTION;
}
public void setSection(String SECTION) {
this.SECTION = SECTION;
}
}
Repository:
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.Student;
#Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
}
O/p should have "Student count in DB: 2"
my java version is :
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
Getting above error when running mvn clean install in command line.
I defined h2db configurations in application.properties file as below
H2 configurarion
spring.h2.console.enabled=true
spring.h2.console.path=/h2
Datasource
spring.datasource.url=jdbc:h2:~/test
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
in data.sql file kept data to be inserted :
insert into STUDENT
values(10001,'Ajay', 'AAA1');
insert into STUDENT
values(10002,'Ajit', 'AAA2');
You need to provide the hibernate dialect to configure hibernate befor connecting
use
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
The error looks like, it can't figure out the dialect to be used with H2.
The dialect specifies the type of database used in hibernate so that hibernate generate appropriate type of SQL statements.
The dialect to be used for h2 is this:
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Also being new to programming might confuse you to write optimal code. It looks like, you are mixing up things from older versions of spring boot and spring. Please use,
https://start.spring.io
to generate your initial spring boot project with the required dependencies. You can follow the below tutorial which explains nicely to get started with spring boot and h2.
https://www.springboottutorial.com/spring-boot-and-h2-in-memory-database

Maven - Package does not exist

I'm trying to build my project and I keep getting an error, saying the package doesn't exist why the IDE says it's fine (no compile error). Obviously, something is not consistent between my pom.xml and the way the IDE compiles.
Here how my project structure looks like:
Here is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.oo</groupId>
<artifactId>employeeservice</artifactId> <!-- Docker complains if the name contains upper case -->
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>EmployeeService</name>
<description>This service takes care of all the employee related operations</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
If I remove Employee (entity) and EmployeeRepository and just have the controller with a dummy "Hello World", it works fine! I tried to put them under the same package, same issue!
Employee class
package com.oo.employeeservice.dao.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String firtName;
private String lastName;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirtName() {
return firtName;
}
public void setFirtName(String firtName) {
this.firtName = firtName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Using default Spring boot configuration:
package com.oo.employeeservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Controller
package com.oo.employeeservice.controller;
import com.oo.employeeservice.dao.EmployeeRepository;
import com.oo.employeeservice.dao.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
#RestController
#RequestMapping("/api")
public class EmployeeController {
#Autowired
private EmployeeRepository repo;
#RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Iterable<Employee>> all() {
return new ResponseEntity<Iterable<Employee>>(repo.findAll(), HttpStatus.OK);
}
#RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> create(#RequestBody Employee employee) {
repo.save(employee);
return new ResponseEntity<String>(HttpStatus.CREATED);
}
}
Maven log:
~/IdeaProjects/EmployeeService$ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building EmployeeService 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) # employeeservice ---
[INFO] Deleting /home/mahdi/IdeaProjects/EmployeeService/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) # employeeservice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) # employeeservice ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/mahdi/IdeaProjects/EmployeeService/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/dao/EmployeeRepository.java:[3,41] package com.oo.employeeservice.dao.entity does not exist
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/dao/EmployeeRepository.java:[9,60] cannot find symbol
symbol: class Employee
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[4,41] package com.oo.employeeservice.dao.entity does not exist
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[18,36] cannot find symbol
symbol: class Employee
location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[23,50] cannot find symbol
symbol: class Employee
location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[19,44] cannot find symbol
symbol: class Employee
location: class com.oo.employeeservice.controller.EmployeeController
[INFO] 6 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.872 s
[INFO] Finished at: 2018-07-15T15:59:38+08:00
[INFO] Final Memory: 31M/308M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project employeeservice: Compilation failure: Compilation failure:
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/dao/EmployeeRepository.java:[3,41] package com.oo.employeeservice.dao.entity does not exist
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/dao/EmployeeRepository.java:[9,60] cannot find symbol
[ERROR] symbol: class Employee
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[4,41] package com.oo.employeeservice.dao.entity does not exist
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[18,36] cannot find symbol
[ERROR] symbol: class Employee
[ERROR] location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[23,50] cannot find symbol
[ERROR] symbol: class Employee
[ERROR] location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] /home/mahdi/IdeaProjects/EmployeeService/src/main/java/com/oo/employeeservice/controller/EmployeeController.java:[19,44] cannot find symbol
[ERROR] symbol: class Employee
[ERROR] location: class com.oo.employeeservice.controller.EmployeeController
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
I found the silliest problem with Intellij. If you look for Employee class in the package explorer in the image attached, you can see the file is marked as a class. When I opened the file location in the terminal, I realized it doesn't have ".java" extension. Obviously, maven won't recognize the file as a java file!
I have faced the same issue once. The problem was I have created the folder structure with name test.demo.exmple.java and it got created in the same way in explorer ( this can be seen if you open same files on explorer) means folder name was test.demo where the system is looking for test/demo/example.java.
So in your case I think path would be com.oo.employeeservice instead of com/oo/employeeservice

Can't run maven tests via terminal | Failed to execute goal

I am trying to use maven to run my unit tests in terminal.
When I run mvn clean test or mvn test I get the following output:
user#user:~/Documents/git/projectname$ mvn test
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project name 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # projectname ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # projectname ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # projectname ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # projectname ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # projectname ---
[INFO] Surefire report directory: /home/user/Documents/git/QA-projectname/target/surefire-reports
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom
[INFO] Failure detected.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.258 s
[INFO] Finished at: 2018-06-13T15:36:31-06:00
[INFO] Final Memory: 13M/54M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project projectname: Unable to generate classpath: org.apache.maven.artifact.resolver.ArtifactResolutionException: Unable to get dependency information for org.apache.maven.surefire:surefire-junit4:jar:2.12.4: Failed to retrieve POM for org.apache.maven.surefire:surefire-junit4:jar:2.12.4: Could not transfer artifact org.apache.maven.surefire:surefire-junit4:pom:2.12.4 from/to central (https://repo.maven.apache.org/maven2): java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
[ERROR] org.apache.maven.surefire:surefire-junit4:jar:2.12.4
[ERROR]
[ERROR] from the specified remote repositories:
[ERROR] central (https://repo.maven.apache.org/maven2, releases=true, snapshots=false)
[ERROR] Path to dependency:
[ERROR] 1) dummy:dummy:jar:1.0
[ERROR]
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
I have also tried to run a single test with mvn test -Dtest=testname but I get the same error.
Here is my 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupidhere</groupId>
<artifactId>artifactidhere</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire</artifactId>
<version>2.21.0</version>
<type>pom</type>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<name>Small Store Regression</name>
<description>description here</description>
</project>
Here is an example of one of my tests:
package loginPurchase;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import PageObjects.CartScreen;
import PageObjects.HomeScreen;
import testBase.TestBase;
import java.math.BigDecimal;
import static org.junit.Assert.assertTrue;
public class ProductTaxPurchases extends TestBase {
#Before
public void setUp() throws Exception {
mobileSetup();
}
#AfterClass
public static void tearDown() throws Exception {
mdriver.quit();
mdriver = null;
}
//The purpose of this test is to add a taxed product to a cart and verify the taxes are correct | and purchase by CC
#Test
public void purchaseTaxedProductByCredit() throws InterruptedException {
HomeScreen hs = new HomeScreen(mdriver);
CartScreen cs = new CartScreen(mdriver);
hs.clickMenu("TestMenuB")
.clickMenuItem("ChrisTaxProduct");
String subTotal = cs.getSubTotal();
String taxAmount = cs.getTax();
String grandTotal = cs.getGrandTotal();
BigDecimal currentSubTotal = new BigDecimal(subTotal);
BigDecimal currentTaxTotal = new BigDecimal(taxAmount);
BigDecimal currentGrandTotal = new BigDecimal(grandTotal);
BigDecimal expectedGrandTotal = currentSubTotal.add(currentTaxTotal);
assertTrue(currentGrandTotal.compareTo(expectedGrandTotal) == 0);
cs.clickPayByCreditCard()
.swipeApprovedCard()
.clickNoReceipt();
}
Second Edit:
Here is the output of mvn dependency::tree
[ERROR] No plugin found for prefix 'dependency' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/home/chris/.m2/repository), central (https://repo.maven.apache.org/maven2)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
Third edit:
I can successfully run mvn clean test on my windows 10 machine and it works as expected. This problem exists on my Ubuntu 18.04 machine.
Any ideas of what I can do to fix this? I normally run my tests in intellij idea and they run just fine. But I am needing to get these tests ready for upload in Microsoft App Center.
The problem exists with Ubuntu 18.04.
I ran sudo rm /etc/ssl/certs/java/cacerts and then sudo update-ca-certificates -f and this fixed my issue in kubuntu 18.04.

Rest assured with TestNG ins not working with Maven project

I added the following dependency list in pom.xml for my Rest assured project with maven:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
Now I am trying to run following sample code:
import static com.jayway.restassured.RestAssured.*;
import static com.jayway.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.*;
public class TestNGTest1 {
private Collection collection;
#Test
public void testGetSingleUserProgrammatic() {
Response res = get("/service/single-user");
assertEquals(200, res.getStatusCode());
String json = res.asString();
JsonPath jp = new JsonPath(json);
assertEquals("test#hascode.com", jp.get("email"));
assertEquals("Tim", jp.get("firstName"));
assertEquals("Testerman", jp.get("lastName"));
assertEquals("1", jp.get("id"));
}
}
But this is throwing error while doing mvn test
Error is:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project GBAppAuomation: Compilation failure: Compilation failure:
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,2] error: cannot find symbol
[ERROR] class TestNGTest1
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,2] error: cannot find symbol
Corresponding symbols are get etc, which rest assured uses.
As asked here is complete error message::
Here is the complete error message:: **
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) # GBAppAuomation ---
[INFO] Compiling 1 source file to /home/jay/temp/GB_MVN_Proj/GBAppAuomation/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,1] error: cannot find symbol
[ERROR] class TestNGTest1
/home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,17] error: cannot find symbol
[ERROR] class TestNGTest1
/home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,2] error: cannot find symbol
[ERROR] class TestNGTest1
/home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,20] error: cannot find symbol
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.384s
[INFO] Finished at: Sat Jan 04 15:26:57 IST 2014
[INFO] Final Memory: 16M/202M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project GBAppAuomation: Compilation failure: Compilation failure:
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,1] error: cannot find symbol
[ERROR] class TestNGTest1
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,17] error: cannot find symbol
[ERROR] class TestNGTest1
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,2] error: cannot find symbol
[ERROR] class TestNGTest1
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,20] error: cannot find symbol
The problem seems to be related with dependencies.
Were there any compilation errors in the IDE?
Because the interface Response from the line below (probably line 50 from the error)
Response res = get("/service/single-user");
belongs to package com.jayway.restassured.response, whereas the only imports in the class related to restassured are
import static com.jayway.restassured.RestAssured.*;
import static com.jayway.restassured.matcher.RestAssuredMatchers.*;
And the JsonPath class from (probably line 53 from the error)
JsonPath jp = new JsonPath(json);
belongs to package com.jayway.jsonpath and also not imported.
This package should be also included into the POM as a dependency:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.9.1</version>
</dependency>
Please pay attention to the groupId. There are two of them available. The one you need for the Restassured jars is: io.rest-assured.
Currently, this is the latest version :
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
If a newer version becomes available, all you need to do is to change the version number in the dependency from 3.0.3 to the new one.
For your information, there is no longer a need to add a standalone JsonPath dependency as it is now fully embedded in the rest-assured artifact as given as above

Categories

Resources