When I connect to the OracleDriver in the application, everything is fine. But when I want to connect to run the JUnit Tests, I got a ClassNotFoundException. And I do exactly the same!
I have the ojbc added to the library and the testlibrary.
public JDBCDataStorage(boolean production) throws DataStorageException {
this.production = production;
try {
rb = (PropertyResourceBundle) PropertyResourceBundle.getBundle("app.control.database.JDBCconfig");
Class.forName(rb.getString("driver"));
} catch (ClassNotFoundException e) {
throw new DataStorageException("Something went wrong in new JDBCDataStorage()" + ": " + e.getMessage());
}
DriverManager.setLoginTimeout(3);
}
Check for two things
That rb.getString("driver") actually returns the FQCN of your driver.
That the driver JAR is in classpath of your test application
Try adding the Oracle JDBC driver jarfile to the classpath of the JUnit test. If you're running the unit test in Eclipse, add the driver jarfile to the User Entries in the Run Configuration of the JUnit test.
Related
On Mac, installed Java 12.0.2, postgresql-42.2.8.jar, PostgreSQL 11.5. From what I can tell, that version of the JDBC driver should be compatible with that version of PostgreSQL.
I've tried putting the PG jar file in my /Java/Extensions directory, and in the same directory as the following test program, and even set CLASSPATH to my test program directory, but I'm still getting ClassNotFound at runtime for the line Class.forName("org.postgresql.Driver"); in my test program:
import java.sql.Connection;
import java.sql.DriverManager;
public class PostgreSQLJDBC {
public static void main(String args[]) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/testdb",
"postgres", "123");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
The test program is taken directly from: https://www.tutorialspoint.com/postgresql/postgresql_java.htm
Any help/suggestions much appreciated... thanks.
Your classpath probably needs to explicitly reference the postgresql jar file. That's what I've always done. There might be some magic associated with putting jars in a particular directory, but I'm 99% sure that referencing a random directory in the classpath won't traverse .jar files, only .class files.
I am using TestRunner class which extends AbstractTestNGCucumberTests to run cucumber feature files in Testng. I have a Issue in the last step where I am using ApprovalTests to verify the response I get from database. I am seeing an exception
"com.spun.util.FormattedException: Didn't find cucumber.api.testng.AbstractTestNGCucumberTests under C:\Users\nsyed\Awris2\LearnDatabase"
Does anyone know why is this exception coming??
My Runner class is:
#CucumberOptions(features = {"src/test/java/Features/ValidateStagingDB.feature"}
,glue={"StepDefinition"}
,dryRun=false
,strict=true
,public class TestRunner extends AbstractTestNGCucumberTests{}`
And Exceptions is here at Approvals.VerifyAsJso
#Then("^the response should match the golden copy$")
public void the_response_should_match_the_golden_copy() throws Throwable {
try {
Approvals.verifyAsJson(DbHelper2.getJsonRepresentationFor(TableData));
}
catch(Exception e) {
System.err.println(e);
}
finally {
System.out.println("Closing the database connection");
connection.close();
}
Can you try to include the cucumber-jvm.jar in your path folder. Here is the location to download: https://mvnrepository.com/artifact/info.cukes/cucumber-jvm
If you maven based project please add this dependency.
If the issue is not fixed, can you try pasting the cucumber-jvm in the path which is shown in the error message. That way we will be sure that it is the missing binaries in the path. If the succeeds you have to figure out the right path where all you jars should go in.
Following is the main Code:
private WebDriver driver = null;
try
{
System.setProperty("webdriver.chrome.driver",
"C:\\Jars\\chromedriver.exe");
driver = new ChromeDriver();
System.out.println("after chrome");
}
catch(Exception e){
System.out.println("error");
e.printStackTrace();
}
When the above code is executed as stand alone java, the driver was initialized and I was able to use for automation.
But when above code is packaged into a jar file,
print statement after driver = new ChromeDriver(); is not called.
Note: chromedriver.exe is outside the jar file
Could anyone help me understand this problem
Too silly of me!. I missed http client jar files while building. This created the problem
I guess you are fogot to upload jars.. Add them
I'm sure I'm doing something wrong here with dependency management, but can't quite figure it out.
My Maven project "A" is dependent on project "B" (a gradle managed project). Both "A" and "B" also have a dependency on Selenium for UI automation. "B" is a collection of some selenium processing libraries. The selenium webDriver object is initialized in project B and returned to A, like this:
Some class in project A:
public WebDriver myDriver;
myDriver = B.initializeWebDriver(myDriver, various selenium related parameters...);
Then "A" can utilize the initialized myDriver object in a testNG test.
The place where it crashes in "B" with no stack trace is:
initializeWebDriver method in project B:
public WebDriver initializeWebDriver(WebDriver webDriver, ...) {
webDriver = new RemoteWebDriver(new URL(url), cap); // <-- crashes here
return webDriver
}
I would have thought that "A" and "B" would simply need to have a dependency on "selenium-java". That's all "B" has as a selenium dependency, and can create the driver and use it in various unit tests wholly contained in project B. If project A has a dependency on selenium-java, and I run a test from A, then Java immediately ends the testNG test when it reaches the line of code in "B" that instantiates the remoteWebDriver. I have a try-catch in both A and B, and nothing ever happens. No stack trace. No log message. Just immediately dies inside the test method in A, not jumping to a 'catch', but jumping to the 'finally' and ending.
What makes me pretty sure it's a dependency management problem, is that if I change the dependencies on "A" to selenium-api and selenium-chrome-driver and selenium-remote-driver, it works fine. But we shouldn't need to do that -- selenium-java should be fine, and selenium-java contains those other objects (selenium-api, etc) anyway! I'm guessing it's some weird CLASSDEF problem and I'm not doing dependency management correctly. I've also never seen java just blatantly give up with no stack trace or errors like this, unless it's some really weird classdef thing.
Any ideas?
**
[Edit: additional code provided as requested]
From B:
public WebDriver getWebDriver(WebDriver webDriver,...) {
try {
System.out.println("inside B try");
webDriver = new RemoteWebDriver(new URL(hubUrl), cap);
System.out.println("B instantiated webdriver");
} catch (Exception e) {
System.out.println("Caught in B");
e.printStackTrace();
}
return webDriver;
}
From class in A:
try {
System.out.println("inside A try");
webDriver = B.getWebDriver(webDriver);
System.out.println("webdriver A successful");
} catch (Exception e) {
System.out.println("caught in A");
e.printStackTrace();
} finally {
closeBrowser(webDriver);
}
Output from this is:
inside A try
inside B try
then output from the closeBrowser routine from the 'finally' in A.
No evidence of any exceptions or anything caught. As soon as it hits the line in B to create the remoteWebDriver, it just dies somehow with no output, and goes to the 'finally' in A.
Edit: Fixed the problem, I had the gradle project 'B' ensure that it was including the selenium dependency in the .jar it was exporting for project B:
from sourceSets.main.output
from sourceSets.main.allJava
... and in Maven project A, I defined the dependency on selenium-java (removed all the other various selenium jars like selenium-api, etc as this works now) as scope:provided.. So it's ensuring that the selenium-java dependency is in fact truly acquired from project 'B'.
But I don't understand why I couldn't have A and B both be dependent on selenium-java. They were specifying the exact same version, and I looked at the dependency resolution and they both were truly using the same version, as opposed to using a different version due to some conflict of some kind.
[edit]
The order of the dependency listed in project A's POM file is what made it work, not the gradle change or the 'provided' change. Simply listing selenium-java first in the pom before project B.
I don't know why you are not getting a stacktrace. Perhaps you are terminating the program in your closeBrowser method? That would short-circuit the error and would explain why you are not getting uncaught exception stacktrace.
Anyhow, for debug purpose, try the following:
You are catching Exception, so Errors are ignored.
Change your try-catch to catch Throwable. This will ensure you catch any possible errors.
try{
...
} catch (Throwable e) {
System.out.println("caught in A");
e.printStackTrace();
}
I use netbeans 6.9 IDE and its features to connect my java class to Mysql database.
as you know, Mysql-connector driver is embedded in netbeans. I make new connection to Mysql database using Mysql(connector/j) driver, and every thing is okay, it displays all databases in Mysql and all tables in these databases, but when I create my java class to test the connetion and start manipulation, ClassNotFoundException is thrown when I call Class.forName("com.mysql.jdbc.Driver").newInstance();
ie. there is no driver ? why i got that exception ? the same happened when I connect to derby embedded db? can you help?
here my test class
public static void main(String[] args) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("driver true");
con = DriverManager.getConnection("jdbc:mysql:///test",
"root", "123456");
if (!con.isClosed()) {
System.out.println("Successfully connected to "
+ "MySQL server using TCP/IP...");
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
}
}
}
You have to add the mysql connector .jar file (Libraries + Add Library + Mysql JDBC Driver) to your project.
On the left side Right-Click "libraries"->"Add Library..."-Select->"MySQL JDBC Driver" then rebuild the project.
You need to add the MySQL Connector/J library to the classpath of your project, otherwise Java will not be able to find the driver class file.
The answer is with your question. Driver is with your netbeans IDE and not with your java project. Add the required dirver (Jar file) to the project and run.