Getting NullPointeException while running the Selenium test script - selenium-webdriver

I am getting the NullPointerException after executing the below testng test script. After launching the URL when it comes inside the test script method then, it is throwing the exception. Can you please help me out in this.
(Object Repo) LakesAndMountainsHomePage.java :-
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.actitime.genericlib.WebDriverCommonLib;
public class LakesAndMountainsHomePage extends WebDriverCommonLib{
WebDriver driver;
public LakesAndMountainsHomePage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
}
#FindBy(xpath= "//*[#id='the-main-menu']//*[contains(text() , 'LAKES &
MOUNTAINS')]")
WebElement LandM;
public WebElement LandMHeader()
{
System.out.println("came inside the method");
//LandM.isDisplayed();
return LandM;
}
}
BaseTest.java:-
public abstract class BaseTest {
ExcelLib eLib;
WebDriverCommonLib wLib;
WebDriver driver;
HomePage homepage;
#BeforeClass
public void baseBeforeClass()
{
eLib = new ExcelLib();
wLib = new WebDriverCommonLib();
driver=Driver.getBrowser();
driver.manage().window().maximize();
System.out.println("Browser is launched");
}
#BeforeMethod
public void launchURL()
{
wLib.homePage();
//loginPage.loginToAPP();
System.out.println("Navigated to the URL");
}
}
Testscript:-
public class LakesAndMountainsHomePageTest extends BaseTest{
LakesAndMountainsHomePage lm = new LakesAndMountainsHomePage(this.driver);
//TC TC131409 [New Lakes & Mountains Tab] : Verify New Lakes & Mountains
Tab is displayed in header.
#Test(priority=0)
public void lakesAndMountainsHeader()
{
boolean a= lm.LandMHeader().isDisplayed(); //getting exception here
if(a==true)
{
System.out.println("Lakes And Mountains tab is present in the HomePage");
}
else
{
System.out.println("Lakes And Mountains tab is not present in the
HomePage");
}
lm.LandMHeader().click();
System.out.println("It has clicked the tab");
}
}
And below is the exception stack trace:-
below is the exception stacktrace:-
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy9.isDisplayed(Unknown Source)
at com.acttime.usertest.LakesAndMountainsHomePageTest.lakesAndMountainsHeader(LakesAndMountainsHomePageTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
I am getting the NullPointerException after executing the below testng test script. After launching the URL when it comes inside the test script method then, it is throwing the exception. Can you please help me out in this.

This instantiation is illegal.
LakesAndMountainsHomePage lm = new LakesAndMountainsHomePage(this.driver);
It is the root cause of Null pointer exception.
Move this statement to BaseTest class, create a class field and instantiate inside #BeforeClass method . Also, remove this operator.

Well i'am unable to see the implementation of a driver class specifying the type of driver(firefox,chrome,IE) being initialized.

Related

Junit Testing Framework for google app engine

// My test class
I want to do junit test of the below code but it is not performing and throwing some errors. I have imported all the jar files but still the junit testing is not doing its task.
package com.full.notetakingapplicationtest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import static org.mockito.Mockito.atLeastOnce;
import com.full.notetakingapplication.RegistrationClass;
import com.full.notetakingapplication.UserDetailsClass;
import static org.mockito.Mockito.verify;
import com.google.appengine.tools.development.testing.LocalAppIdentityServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import junit.framework.TestCase;
public class UserDetailsClassTest extends TestCase {
public final LocalServiceTestHelper helper = new LocalServiceTestHelper(
new LocalDatastoreServiceTestConfig(),
new LocalTaskQueueTestConfig().setDisableAutoTaskExecution(true),
new LocalTaskQueueTestConfig().setShouldCopyApiProxyEnvironment(true),
new LocalAppIdentityServiceTestConfig());
#Before
public void setUp(){
helper.setUp();
}
#After
public void tearDown(){
helper.tearDown();
}
#InjectMocks
UserDetailsClass user = Mockito.mock(UserDetailsClass.class);
#InjectMocks
RegistrationClass register = new RegistrationClass();
#SuppressWarnings("static-access")
#Test
public void testRegisterUser_usingMockito(){
String userName = "Pravin";
String userEmailId = "pravin.bala#adaptavantcloud.com";
String userPassword = "success";
register.setUserName(userName);
register.setUserEmailId(userEmailId);
register.setUserPassword(userPassword);
Assert.assertTrue(user.registerUser(register));
verify(user, atLeastOnce()).registerUser(register);
}
}
// My Error on Testing the method.
UserDetailsClassTest.testRegisterUser_usingMockito
testRegisterUser_usingMockito(com.full.notetakingapplicationtest.UserDetailsClassTest)
java.lang.NoSuchMethodError: com.google.appengine.repackaged.com.google.io.protocol.ProtocolSupport.freezeString(Ljava/lang/Object;)Ljava/lang/Object;
at com.google.storage.onestore.v3.OnestoreEntity$Reference.freeze(OnestoreEntity.java:7656)
at com.google.storage.onestore.v3.OnestoreEntity$Reference$1.<init>(OnestoreEntity.java:7555)
at com.google.storage.onestore.v3.OnestoreEntity$Reference.<clinit>(OnestoreEntity.java:7552)
at com.google.appengine.api.datastore.KeyTranslator.convertToPb(KeyTranslator.java:48)
at com.google.appengine.api.datastore.EntityTranslator.convertToPb(EntityTranslator.java:51)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:178)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:155)
at com.google.appengine.api.datastore.Batcher$BatchIterator.<init>(Batcher.java:180)
at com.google.appengine.api.datastore.Batcher$2.<init>(Batcher.java:317)
at com.google.appengine.api.datastore.Batcher.getBatches(Batcher.java:317)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.doBatchPut(AsyncDatastoreServiceImpl.java:365)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:293)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl$4.runInternal(BaseAsyncDatastoreServiceImpl.java:261)
at com.google.appengine.api.datastore.TransactionRunner.runWriteInTransaction(TransactionRunner.java:53)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:263)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:234)
at com.google.appengine.api.datastore.DatastoreServiceImpl.put(DatastoreServiceImpl.java:56)
at com.full.notetakingapplication.UserDetailsClass.registerUser(UserDetailsClass.java:25)
at com.full.notetakingapplicationtest.UserDetailsClassTest.testRegisterUser_usingMockito(UserDetailsClassTest.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Nullpointer Exception in webdriver, driver is null

I am a newbie in selenium java and trying to run test case by calling methods from another class in a framework. Methods are defined in a different class. The issue is that for first instance, the driver is found (Firefox driver) but when a method from another class is called, the driver is null.
Below is the code snippet:
public class ActionDriver {
WebDriver driver;
public ActionDriver(WebDriver driver){
this.driver = driver;
}
public void type(By loc, String value) {
driver.findElement(loc).sendKeys(value);
}
}
NPE comes for the above line of code for driver for second instance when method is called from the WebAction class. driver.findElement(loc).sendKeys(value);
Second class is :
public class WebActions {
WebDriver driver;
ActionDriver ad = new ActionDriver(driver);
SizeChartTemplate sct = new SizeChartTemplate();
public void TypeTemplateName(){
ad.type(jsct.Template_Name, "Men's Vest");
}
}
NPE is for the above line of code at ad.
Test Case:
public class LoginToJcilory extends OpenAndCloseBrowser {
#Test
public void logintojcilory() throws BiffException, IOException, InterruptedException{
WebActions wa = new WebActions();
System.out.println("Entered login to jcilory block");
ActionDriver actdrvr = new ActionDriver(driver);
JciloryLoginPage jclp = new JciloryLoginPage();
JcilorySizeChartTemplate jsct = new JcilorySizeChartTemplate();
String username = actdrvr.readExcel(0, 1);
String password = actdrvr.readExcel(1, 1);
System.out.println(username);
System.out.println(password);
actdrvr.type(jclp.Username, username);
actdrvr.type(jclp.Password, password);
actdrvr.click(jclp.LoginButton);
Thread.sleep(2000);
driver.get("http://qacilory.dewsolutions.in/JCilory/createSizeChartTemplate.jc");
wa.TypeTemplateName();
}
NPE comes for wa element in the above code.
Below is the error:
FAILED: logintojcilory
java.lang.NullPointerException
at Config.ActionDriver.type(ActionDriver.java:40)
at Config.WebActions.TypeTemplateName(WebActions.java:17)
at Test.LoginToJcilory.logintojcilory(LoginToJcilory.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
===============================================
Test run on FIREFOX
Tests run: 1, Failures: 1, Skips: 0
===============================================
OpenAndCloseBrowser:
public class OpenAndCloseBrowser {
protected WebDriver driver;
#Parameters({"browser","baseURL"})
#BeforeClass
public void openBrowser(String browser,String baseURL){
if(browser.equalsIgnoreCase("firefox")){
driver=new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\driver\\chromedriver.exe");
driver=new ChromeDriver();
}
else if(browser.equalsIgnoreCase("ie")){
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\driver\\IEDriverServer.exe");
DesiredCapabilities caps=new DesiredCapabilities();
caps.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver=new InternetExplorerDriver(caps);
}
else{
driver=new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get(baseURL);
}
#AfterClass
public void closeBrowser(){
//driver.quit();
}
}
I guess there is an issue with the way i am defining the driver in these classes. Any help is resolving the issue is appreciated.
Your driver is null in ActionDriver.type() because it was null when it was passed into the constructor from WebActions. OpenAndCloseBrowser is creating a WebDriver instance, but assigning to a DIFFERENT (local) driver variable. You should learn more about variable scoping in Java...this isn't a Selenium/Webdriver issue.
You probably don't need an answer anymore, but this issue has quite the views, so just for completeness.
I usually have a global driver variable in my test class and I instantiate it like this, so it's available for every test.
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver(capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
Just add this to your test class (adapt driver path) and the driver is instantiated before the test.

Selenium Grid running tests in parallel

Currently, I have a Selenium grid setup, with 1 local hub and 2 local nodes. The hub is capable of distributing the tests to run in parallel and distribute it over to the nodes. I am running the tests in parallel.
The following is the base test
public abstract class BaseTest
{
String testFolder;
String testName;
protected String envName;
protected Configuration config;
protected String host;
protected RemoteWebDriver driver;
protected String proxy;
protected SomeData someData;
protected SomeController someController;
public BaseTest() {
}
public BaseTest( String testFolder, String testName)
{
this.testFolder = testFolder;
this.testName = testName;
this.envName = System.getProperty("config");
this.proxy = System.getProperty("proxy");
config = this.envName;
}
#BeforeMethod
public void startTest(Method testMethod) {
LOG.info("Starting test: " + testMethod.getName());
try {
this.someData = new SomeData();
this.driver = WebDriverSetup.getDriver();
this.someController = new someController(this.driver, this.someData);
driver.navigate().to("https://" + this.host);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException");
}
}
#AfterMethod
public void closeWindow() {
driver.close();
driver.quit();
}
}
The following is the class to get the RemoteWebDriver:
public class WebDriverSetup {
public static RemoteWebDriver getDriver() throws MalformedURLException{
String SELENIUM_HUB_URL = "http://localhost:4444/wd/hub";
ThreadLocal<RemoteWebDriver> remoteWebDriver = null;
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
String proxy = System.getProperty("proxy");
if (proxy != null && !proxy.isEmpty()) {
System.out.println("Using proxy: " + proxy);
capabilities.setCapability(CapabilityType.PROXY, proxy);
}
try {
remoteWebDriver = new ThreadLocal<RemoteWebDriver>();
remoteWebDriver.set(new RemoteWebDriver(new URL(SELENIUM_HUB_URL),
capabilities));
} catch (MalformedURLException e) {
System.out.println("Tackle Issue with RemoteDriverSetup");
}
remoteWebDriver.get().manage().window()
.setSize(new Dimension(2880, 1524));
remoteWebDriver.get().manage().timeouts()
.pageLoadTimeout(10, TimeUnit.SECONDS);
remoteWebDriver.get().manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
return remoteWebDriver.get();
}
}
My test suite is like :
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Multiple Tests Suite" verbose="1" parallel="methods">
<test name="Test1">
<classes>
<class name="com.itesteverything.qa.Tests"></class>
</classes>
</test>
</suite>
Tests are like :
public class Tests extends BaseTest {
#Parameters({"testName", "env" })
public Tests( #Optional String testName, #Optional String env ) {
super( null, testName, null, env );
}
#BeforeMethod
public void setup() throws Exception {
//setSomeData
}
public void test1() throws Exception {
use driver from super
use someData from super
use someController is using the driver from super
}
public void test2() throws Exception {
use driver from super
use someData from super
use someController is using the driver from super
}
While running these tests, I get the following errors
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
Driver info: driver.version: RemoteWebDriver
org.openqa.selenium.remote.SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:393)
at org.openqa.selenium.By$ById.findElement(By.java:214)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
at com.sun.proxy.$Proxy25.sendKeys(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:842)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1166)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
TEST FAILED: test2
FAILED REASON: Session ID is null. Using WebDriver after calling quit()?
Is it something anyone aware of?
Thanks in advance!
Do not set driver in the base class, do not have driver property at all. The same instance is being overridden by different threadlocal drivers.
Any time you want to run your test, refer to WebDriverSetup.getDriver() in your test method itself and in your after/before methods.
#AfterMethod
Is running after each method.
And U run's only one setup. So after first method U close and it is closed as shows stack trace.

cameltest case with bean

In my java class i have something like below:
from("direct:dcrm:input")
.routeId("dcrm-route"
.convertBodyTo(Message.class)
.setHeader(
"recipientListHeader",
simple("activemq:"+rdvrQueueName+ ".${body.controllerID}?jmsMessageType=Text&timeToLive="
+ rdvrMessageTimeToLive
+ "&deliveryPersistent=" + deliveryPersistent))
.choice()
.when(simple("${body.messageType} == 'rdvr'"))
.bean(new DcrmMessageHandler(), "process")
.marshal(requestMessage)
.recipientList(header("recipientListHeader"))
.onPrepare(new MesssageProcessor()).parallelProcessing()
.end()
.setBody(constant(StatusCode.SUCCESS))
.otherwise()
.endChoice();
while writing Junit i am doint like this :
#Test
public void testSendMatchingMessage() throws Exception {
String expectedBody = "<matched/>";
resultEndpoint.expectedBodiesReceived(expectedBody);
template.sendBodyAndHeader(expectedBody, "recipientListHeader", 0);
resultEndpoint.assertIsSatisfied();
}
#Override
protected RouteBuilder createRouteBuilder() throws Exception {
//return new DcrmOutboundRoute();
return new RouteBuilder() {
#Override
public void configure() throws Exception {
// TODO Auto-generated method stub
from("direct:dcrm:input")
.filter(header("recipientListHeader").isEqualTo(constant(StatusCode.SUCCESS)))
.bean(new DcrmMessageHandler(), "process").to("mock:result");
}
};
}
But I am getting the bellow error:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/NoResultException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Check your classpath or your Maven dependencies, in particular the one referencing JPA (javax.persistence). You should have a dependency with groupId javax.persistence and probably artifactId persistence-api, replace it by org.hibernate.javax.persistence or org.eclipse.persistence dependency.

Error while connecting to MySql database using mapreduce job

I was trying the following code for connecting to a mysql database using a map-reduce job. I am facing the following error which is posted below. I have placed checkpoints in my code
which indicate that the part of the job till the job is actually run is run correctly, afterwards the job fails...
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.SequenceFileOutputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.mapred.lib.db.DBConfiguration;
//import org.apache.hadoop.mapred.lib.db.DBInputFormat;
import org.apache.hadoop.mapred.lib.db.DBInputFormat;
import org.apache.hadoop.mapred.lib.db.DBOutputFormat;
import org.apache.hadoop.mapred.lib.db.DBWritable;
public class TweetWordCount {
public static class TweetWordCountMapper extends MapReduceBase implements
Mapper<LongWritable, GetTweets, Text, IntWritable> {
private final static IntWritable intTwordsCount = new IntWritable(1);
private Text strTwoken = new Text();
public void map(LongWritable key, GetTweets value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
System.out.println("checkpoint4");
GetTweets tweets = new GetTweets();
tweets.strTweet = value.strTweet;
//TwitterTokenizer twokenizer = new TwitterTokenizer();
//List<String> twokens = twokenizer.twokenize(value.strTweet.toString());
output.collect(new Text(value.strTweet.toString()), intTwordsCount);
System.out.println("checkpoint5");
}
}
public static class TweetWordCountReducer extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
System.out.println("checkpoint6");
int intTwokenCount = 0;
while (values.hasNext()) {
intTwokenCount += values.next().get();
}
output.collect(key, new IntWritable(intTwokenCount));
System.out.println("checkpoint6");
}
}
public static void main(String[] args) throws Exception {
System.out.println("checkpoint1");
JobConf twokenJobConf = new JobConf(new Configuration(),TweetWordCount.class);
//JobConf twokenJobConf = new JobConf(TweetWordCount.class);
twokenJobConf.setJobName("twoken_count");
twokenJobConf.setInputFormat(DBInputFormat.class); //Set input format here
twokenJobConf.setOutputFormat(TextOutputFormat.class);// Sets the output format
Object out = new Path("twokens");
twokenJobConf.setMapperClass(TweetWordCountMapper.class);
twokenJobConf.setCombinerClass(TweetWordCountReducer.class);
twokenJobConf.setReducerClass(TweetWordCountReducer.class);
twokenJobConf.setOutputKeyClass(Text.class);
twokenJobConf.setOutputValueClass(IntWritable.class);
DBConfiguration.configureDB(twokenJobConf, "com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/test", "root", "root"); //Specifies the DB configuration
String[] fields = {"Tweet"}; //Specifies the Fields to be fetched from DB
DBInputFormat.setInput(twokenJobConf, GetTweets.class, "NewGamil",
null /* conditions */, "Tweet", fields); // Specifies the DB table and fields
//SequenceFileOutputFormat.setOutputPath(twokenJobConf, (Path) out);
FileOutputFormat.setOutputPath(twokenJobConf, (Path) out);
System.out.println("checkpoint2");
JobClient.runJob(twokenJobConf);
System.out.println("checkpoint3");
}
public static class GetTweets implements Writable, DBWritable {
String strTweet;
public GetTweets() {
}
public void readFields(DataInput in) throws IOException {
System.out.println("checkpoint 2a");
this.strTweet = Text.readString(in);
}
public void readFields(ResultSet resultSet) throws SQLException {
System.out.println("checkpoint 3a");
// this.id = resultSet.getLong(1);
this.strTweet = resultSet.getString(1);
}
public void write(DataOutput out) throws IOException {
}
public void write(PreparedStatement stmt) throws SQLException {
}
}
}
rv#ramanujan:~$ hadoop jar Twit.jar
Warning: $HADOOP_HOME is deprecated.
checkpoint1
checkpoint2
13/03/22 17:16:12 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
13/03/22 17:16:12 INFO mapred.JobClient: Cleaning up the staging area hdfs://localhost:54310/home/rv/hadoopfiles/mapred/staging/rv/.staging/job_201303221600_0008
Exception in thread "main" java.lang.RuntimeException: Error in configuring object
at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:93)
at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:64)
at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:117)
at org.apache.hadoop.mapred.JobConf.getInputFormat(JobConf.java:575)
at org.apache.hadoop.mapred.JobClient.writeOldSplits(JobClient.java:981)
at org.apache.hadoop.mapred.JobClient.writeSplits(JobClient.java:973)
at org.apache.hadoop.mapred.JobClient.access$600(JobClient.java:172)
at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:889)
at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:842)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:416)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1059)
at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:842)
at org.apache.hadoop.mapred.JobClient.submitJob(JobClient.java:816)
at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:1253)
at TweetWordCount.main(TweetWordCount.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.hadoop.util.RunJar.main(RunJar.java:156)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:88)
... 20 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.hadoop.mapred.lib.db.DBInputFormat.configure(DBInputFormat.java:271)
... 25 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at org.apache.hadoop.mapred.lib.db.DBConfiguration.getConnection(DBConfiguration.java:123)
at org.apache.hadoop.mapred.lib.db.DBInputFormat.configure(DBInputFormat.java:266)
... 25 more
Make sure you use -libjars in commands. like the following :
bin/hadoop jar wordcount.jar org.myorg.DBCountPageView -libjars mysql-connector-java-5.1.26-bin.jar
Please reference http://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html's usage part
Not sure what the scope of your app is (learning, dev, etc.) but I would suggest to use Sqoop to interact with a relational database such as MySQL.

Resources