I am new to ANT and JUnit. As per the requirement i created 'JUnitTestCase' in Eclipse and call the requested test classes as per proper annotation.
While running the 'build.xml' file through 'Ant Build', it throws error as:
D:\ecllipse\eclipse\JunitTestProject>ant compile
Buildfile: D:\ecllipse\eclipse\JunitTestProject\build.xml
setClassPath:
init:
clean:
compile:
[echo] making directory...
[echo] classpath------:
[echo] compiling...
[javac] Compiling 4 source files to D:\ecllipse\eclipse\JunitTestProject\build
[javac] D:\ecllipse\eclipse\JunitTestProject\src\jnuittestPckg\HelloWorldTest.java:3: package org.junit does not exist
[javac] import org.junit.After;
[javac] ^
[javac] D:\ecllipse\eclipse\JunitTestProject\src\jnuittestPckg\HelloWorldTes
t.java:4: package org.junit does not exist
[javac] import org.junit.Before;
[javac] ^
While looking back to JUnitTEstCase, i have imported the annotations like (import org.junit.After;import org.junit.Before and import org.junit.Test;). I, also, set build path for 'junit.jar'. I tried lot but couldn't figure it out. i am also attaching JUnitTest project as:
package jnuittestPckg;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HelloWorldTest {
private HelloWorld hlwrd;
#Before
public void setUp() throws Exception {
hlwrd = new HelloWorld();
System.out.println("I am in Before");
}
#After
public void tearDown() throws Exception {
System.out.println("I am in After");
}
#Test
public void testClass1() {
hlwrd.Class1(null);
}
#Test
public void testClass2() {
hlwrd.Class2(null);
}
}
Please help me out.
You test.classpath property does not include the junit.jar.
Add it to the classpath and this issue will be resolved.
Related
I am trying to run the below code
package JavaBasics;
enter code here
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class OpenBrowserURL {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Pooja\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.makemytrip.com/?ccde=us");
}
}
I am getting error java.lang.NoClassDefFoundError for get as the jar files that i downloaded 3.141.59 has get as void
How can i fix this? Is this an issue with the jar files I downloaded. Please advice
//Eclipse java code:
package openbrowser;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class OpenChrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");
WebDriver driver=new ChromeDriver();
}
}
eclipse error
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\Om Murugaa\Downloads\Testing\selenium-server-standalone-3.141.59.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.eclipse.jetty.http.Http1FieldPreEncoder not in module
Adding maven into eclipse resolved this error.
I tried using TinyLog for a sample application to make sure understanding how it works, so that i can use it for my work application.
But, while using i get an exception to type cast the Writer (Compile time error). If i typecast it, it throws runtime error as below.
Code :
import java.io.FileWriter;
import java.io.IOException;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.writers.ConsoleWriter;
import org.pmw.tinylog.writers.Writer;
public class TestClass {
public static void main(String[] args) throws IOException {
Configurator.defaultConfig().writer(new ConsoleWriter()).addWriter((Writer) new FileWriter("data.txt")).activate();
Logger.info("welcome to tinylog logger.....");
}
}
Error :-
Exception in thread "main" java.lang.ClassCastException: java.io.FileWriter cannot be cast to org.pmw.tinylog.writers.Writer
at TestClass.main(TestClass.java:12)
Please help us :).
You can fix it easily. Just import org.pmw.tinylog.writers.FileWriter instead of java.io.FileWriter. The class java.io.FileWriter is the file writer of the JVM, but you need tinylog's file writer.
Afterwards you can remove the class cast "(Writer)" as org.pmw.tinylog.writers.FileWriter is an implementation of the interface org.pmw.tinylog.writers.Writer. However the class java.io.FileWriter does not implement the interface org.pmw.tinylog.writers.Writer. Therefor the reported ClassCastException has been thrown.
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.writers.ConsoleWriter;
import org.pmw.tinylog.writers.FileWriter;
public class TestClass {
public static void main(String[] args) {
Configurator.defaultConfig().writer(new ConsoleWriter()).addWriter(new FileWriter("data.txt")).activate();
Logger.info("welcome to tinylog logger.....");
}
}
I'm creating a simple web application with a register form, and I have used Hibernate. But i have this kind of error:
this is the code:
AddUser.java
import java.io.IOException;
import javax.imageio.spi.ServiceRegistry;
import javax.security.auth.login.Configuration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
#WebServlet("/CreateUser.do")
public class AddUser extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddUser() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
ServiceRegistry servReg = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory factory = config.buildSessionFactory(servReg);
Session session = factory.openSession();
session.beginTransaction();
User u = new User(request.getParameter("firstname"), request.getParameter("lastname"), request.getParameter("login"), request.getParameter("password"));
session.save(u);
session.getTransaction().commit();
session.close();
RequestDispatcher view = request.getRequestDispatcher("useradd.jsp");
view.forward(request, response);
}
}
This is the error: ERROR
How can I do to solve this problem? Thanks you!
#LuisMuñoz
I add also the error if someone want to see it directly.
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Servlet execution threw an exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type Configuration
The method configure() is undefined for the type Configuration
The method getProperties() is undefined for the type Configuration
The method buildSessionFactory(ServiceRegistry) is undefined for the type Configuration
AddUser.doPost(AddUser.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
My Eclipse Page:
ECLIPSE
From the error it looks like there is no method configure for Configuration object. Check the javadocs https://docs.oracle.com/javase/9/docs/api/index.html?javax/security/auth/login/Configuration.html
Im quite new to all this stuff. I try to launch a webservice via GlassFish. When i try to build this project i get an error.
ant -f /home/philipp/NetBeansProjects/sks3 -DforceRedeploy=false -Ddirectory.deployment.supported=true -Dnb.wait.for.caches=true run
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
check-rest-config-props:
generate-rest-config:
library-inclusion-in-archive:
library-inclusion-in-manifest:
compile:
compile-jsps:
In-place deployment at /home/philipp/NetBeansProjects/sks3/build/web
Initializing...
deploy?DEFAULT=/home/philipp/NetBeansProjects/sks3/build/web&name=sks3&contextroot=/sks3&force=true failed on GlassFish Server 3.1.2
Error occurred during deployment: Exception while deploying the app [sks3] : Invalid TYPE-level #EJB with name() = [] and beanInterface = [class java.lang.Object] in class Webservice.MeasurementResources. Each TYPE-level #EJB must specify both name() and beanInterface().at org.glassfish.apf.AnnotationInfo#3b63118a. Please see server.log for more details.
/home/philipp/NetBeansProjects/sks3/nbproject/build-impl.xml:1028: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 6 seconds)
I dont have a clue what is going wrong but according to the message it has to be in the file MeasurementResurces.java ...
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Webservice;
import Exception.DALException;
import dal.MeasurementDao;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Path;
import repo.Measurement;
/**
*
* #author philipp
*/
//#Stateless
//#inject
#EJB
//#LocalBean
#Named
#Path("Measurement")
public class MeasurementResources {
#Inject
MeasurementDao mDao;
public void add(Measurement arg) throws DALException{
mDao.save(arg);
}
/* public void getAll(Measurement arg) throws DALException{
mDao.getAll();
}
*/
}
Someone has at least a hint whats the problem?
You are using a Type-Level EJB without declaring name and beanInterface.
/**
*
* #author philipp
*/
//#Stateless
//#inject
#EJB(name="MyEjb", beanInterface=RemoteEjb.class)
//#LocalBean
#Named
#Path("Measurement")
public class MeasurementResources {
#Inject
MeasurementDao mDao;
public void add(Measurement arg) throws DALException{
mDao.save(arg);
}
}
#Remote
public interface RemoteEjb {
public void doSomething();
}
#Stateless
public class MyEjb implements RemoteEjb {
...
}
name is the name of the EJB you trying to inject. beanInterface is the Local or Remote interface. It's not a real injection. It is a way to use annotation as a replacement of deployment descriptor ejb-ref element. You should use a JNDI lookup in order to inject the ejb.
I don't know what are you trying to do but the common way to inject an ejb is the following:
#Named
#Path("Measurement")
public class MeasurementResources {
#EJB
private MyEjb myejb;
#Inject
MeasurementDao mDao;
public void add(Measurement arg) throws DALException{
mDao.save(arg);
}
...
}