Java Spring boot and Travis CI - java

I am trying to automatically run my test with Travis CI. I just can't find a good tutorial on how to do this in java. When I push to GitHub I keep getting the following error in Travis :
The command "./mvnw clean install" exited with 127.
My .travis.yml file looks like this:
language: java
jdk:
- openjdk11
script: ./mvnw clean install
This is my Test class
package server;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#WebMvcTest(CoffeeController.class)
#RunWith(SpringRunner.class)
class CoffeeControllerTest {
#Autowired
private MockMvc mockMvc;
#Test
void getAllCoffee() {
try {
mockMvc.perform(get("/coffee/all")).andExpect(status().isOk());
} catch (Exception e) {
e.printStackTrace();
}
}
}
I searched several tutorials on Youtube and on the web but I can't seem to get the answer. I hope any of you can help me. I appreciate it very much! I also tried to remove the script but also no success.

The return code 127 is telling you that it cannot find the command. In this case mvnw.
Please take a look, that your mvnw (mvn wrapper) is in the PATH, or try the absolute path. Also the mvnw should be part of the project, if it isn't pushed you can push it, or use a openjdk with mvn to build your app.

It works for me:
sudo: required
language: java
before_install:
- mvn clean install

I think you should change the script value to this:
script: ./mvn clean install
I'm not sure but you could try it

Related

Encryption of simple string using jasypt through terminal

I am trying to encrypt a simple string using jasypt. It is working correctly when I use eclipse IDE but has some problem when I try through the terminal.
Output through Eclipse IDE Screenshot
Below is the code which I use.
package com.jasypt.encryption.demo;
import org.jasypt.util.text.BasicTextEncryptor;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class BasicDemo {
public static void main(String[] args) throws IOException {
String secretkey = "home#123";
String message = "This is a confidential message. Be Careful !!";
BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
basicTextEncryptor.setPassword(secretkey);
String encrMess = basicTextEncryptor.encrypt(message);
System.out.println(encrMess);
String decrMess =basicTextEncryptor.decrypt(encrMess);
System.out.println(decrMess);
}
}
I navigate to the folder which contains pom.xml file and enter following commands in terminal
1) mvn package
2) mvn install
3) java -cp target/demo-0.0.1-SNAPSHOT.jar com.jasypt.encryption.demo.BasicDemo
I get BUILD SUCCESS message and jar file is successfully created but I get some error when I run 3rd command.
Error Screenshot
Please excuse and suggest something if I am making some very basic mistake or using redundant lines of code as I am new to java.
Welcome to StackOverflow!
When you compile your program with Maven (which is actually not a compiler but a package manager that can also call the Java compiler behind the scenes) Maven takes care of downloading and managing the dependencies that your program uses, in this case it is Jasypt.
When you then try to start the program with plain java the information about the dependecies that are necessary to run your program is lost, just because Maven is no longer part of the game. Therefore you have to give the Java runtime a hint where to find the Jasypt dependency, just as you did with your demo-jar. During the compilation process Maven stored the Jasypt jar on your drive, in a folder called local Maven repository.
You now can simply add the path to this jar to your classpath and everything will run:
java -cp target/demo-0.0.1-SNAPSHOT.jar:<path to your Maven repository>/org/jasypt/jasypt/1.9.3/jasypt-1.9.3.jar com.jasypt.encryption.demo.BasicDemo
(The version of the Jasypt library may differ on your machine.)
If you have a lot of dependencies it will become cumbersome to add them all manually to the classpath. Maven can also take care for you of this task, with the help of the Exec-plugin. Instead of starting java directly let Maven do the plumbing for you:
mvn exec:java -Dexec.mainClass="com.jasypt.encryption.demo.BasicDemo"
You could also check this thread for more details about this plugin and its options

Stanford CoreNLP doesn't exist

I have the following at the start of my code:
import twitter4j.*;
import java.util.List;
import java.util.Properties;
import java.io.*;
import java.util.*;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;
The problem is, I get the errors like this below:
Twitter_Project.java:8: error: package edu.stanford.nlp.ling does not exist
import edu.stanford.nlp.ling.CoreAnnotations;
It seems like I didn't place my files in the same folder as I should have, but I don't understand how to rearrange my folder...
The only problem is, I don't really understand where to relocate my java files so that I don't return this error anymore.
And this is everything inside the stanford corenlp folder:
Please help—this is my first time using stanford corenlp and also using these kinds of integrated programs (if that's the right word), and I'm very confused.
Hi you need to make sure all of the jar files from the distribution are on your CLASSPATH. How you do that will vary depending on if you are running from the command line or in an IDE.
This link explains the idea of CLASSPATH: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
I had much difficulty installing Stanford CoreNLP until I installed it inside a Docker container. Now you can install and run it in two steps:
Pull the image from Dockerhub:
$ docker pull anwala/stanfordcorenlp
To run the CoreNLP server:
$ docker run --rm -d -p 9000:9000 --name stanfordcorenlp anwala/stanfordcorenlp
To use the server via Browser/command-line/custom python script etc:
https://ws-dl.blogspot.com/2018/03/2018-03-04-installing-stanford-corenlp.html

Add a main method to some classes for simple testing

I have some classes throughout a maven project. I would like to add a main method to some of those classes for basic testing while developing.
I tried declaring the class to run with:
mvn exec:java -Dexec.mainClass="huru.util.Async"
but that command looked in my pom.xml file and it ran some pre-configured setup and started my server up as usual. How can I run a specific file (not my regular main class), but still load up the necessary dependencies?
note that for testing I need most of the dependencies in pom.xml, so I will probably need mvn to run the class that I need to test, I can't run it directly with javac.
update sadly, I may need to create a profile in pom.xml since maven can't seem to do very much from the command line. I don't know much about profiles and since I have none in my pom.xml file right now, I am a bit scared of adding that section.
As suggested in the comments, one solution is to skip putting a main method in the class I want to test, but instead create a junit test...this works at the command line:
mvn -Dtest=AsyncTest test
where my test looks like:
package huru;
import huru.util.Async;
import io.vertx.core.Vertx;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Arrays;
#RunWith(VertxUnitRunner.class)
public class AsyncTest {
#Test
public void test(TestContext tc) {
Async.Parallel(Arrays.asList(
v -> {
v.done(null, null);
}
), (e, results) -> {
});
}
}

Cannot find class in classpath: (ClassName)

I have started TestNG and wrote the first program in it but I cannot be able to run because of an error "Cannot find class in classpath: FirstTestNGFile" (here FirstTestNGFile is a class name). Here is my code.
package firsttestngpackage;
import org.openqa.selenium.*;
import org.testng.annotations.*;
import org.testng.Assert;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTestNGFile {
public String baseURL = "https://www.google.com/";
String driverpath = "D:\\Ashish\\Setup\\chrome\\chromedriver.exe";
public WebDriver driver;
#Test
public void verifyHomepageTitle() {
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.get(baseURL);
String expectedtitle = "Google";
String Actualtitle = driver.getTitle();
Assert.assertEquals(expectedtitle, Actualtitle);
driver.close();
}
}
Here is the Exception:
Cannot find class in classpath: FirstTestNGFile
at org.testng.xml.XmlClass.loadClass(XmlClass.java:77)
at org.testng.xml.XmlClass.init(XmlClass.java:69)
at org.testng.xml.XmlClass.<init>(XmlClass.java:55)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:575)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
Note: I already cleaned my project and that did not resolve my problem.
If you are working with Maven and executing testng.xml from pom.xml then below statement will help you solve your problem
"By default, Surefire-plugin runs all tests that matches with filename pattern such as *Test.java in test source directory src/test/java . To use a different naming scheme, we can configure Surefire Plugin which includes parameter and specify the tests that we want to include."
I have reinstalled TestNG and it works for me now.
The error says it all :
Cannot find class in classpath: FirstTestNGFile
at org.testng.xml.XmlClass.loadClass(XmlClass.java:77)
at org.testng.xml.XmlClass.init(XmlClass.java:69)
at org.testng.xml.XmlClass.<init>(XmlClass.java:55)
The error stack trace clearly indicates TestNG is unable to initialize properly.
You code looks fine to me but you have to take care a couple of points as follows :
You need to be specific with the set of imports you are using :
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import org.testng.Assert;
import org.openqa.selenium.chrome.ChromeDriver;
To end your script invoke driver.quit(); to kill the WebDriver and Web Browser instance.
Clean the Project Workspace from your IDE to build it afresh.
You may consider to close your IDE and take a System Reboot.
Execute your Test.
If problem still persists you may consider to update the question with the testng.xml which you are using.
So this worked for me :
Got "cannot find class path" even after reinstalling TestNg, Updating Maven , Cleaning projects, changing JRE multiple times.
But this worked:
Go to your .m2 repository. (Mine c:/users/.m2/repository)
Delete entire repository folder.
Rebuild your project, it will reinstall the dependencies again. And now run your files.
This thing worked for me, hope it will work for you guys as well.
Just clear your project space from PROJECT---> CLEAR
Then run again .

How to use maven project as backend?

I know this is basic stuff, but I couldn't figure it out with Google so here I am. I have a backend that allows for the use of web sockets on my website using jetty/java. When I want the backend to work, I have to cd into the project's directory and then run the command 'mvn jetty:run.' Before that, I have to add maven's bin file to the PATH variable. This is all done on an AWS instance. So what I'm wondering is, how do I have the maven package run on startup of the server (after running mvn package of course). Is there something I need to add to init.d? I'm really not sure.
You need to add an entry point, something like this:
package com.yourapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class YourApp {
public static void main(String[] args) {
SpringApplication.run(YourApp.class,args);
}
}

Categories