How to use NoDriverFoundException method of org.testcontainers.containers.JdbcDatabaseContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.JdbcDatabaseContainer.NoDriverFoundException

Source:JdbcDatabaseContainer.java Github

copy

Full Screen

...108 if (testQuerySucceeded) {109 break;110 }111 }112 } catch (NoDriverFoundException e) {113 // we explicitly want this exception to fail fast without retries114 throw e;115 } catch (Exception e) {116 // ignore so that we can try again117 logger().debug("Failure when trying test query", e);118 Thread.sleep(100L);119 }120 }121 } catch (InterruptedException e) {122 Thread.currentThread().interrupt();123 throw new ContainerLaunchException("Container startup wait was interrupted", e);124 }125 logger().info("Container is started (JDBC URL: {})", JdbcDatabaseContainer.this.getJdbcUrl());126 }127 @Override128 protected void containerIsStarted(InspectContainerResponse containerInfo) {129 runInitScriptIfRequired();130 }131 /**132 * Obtain an instance of the correct JDBC driver for this particular database container type133 *134 * @return a JDBC Driver135 */136 public Driver getJdbcDriverInstance() throws NoDriverFoundException {137 synchronized (DRIVER_LOAD_MUTEX) {138 if (driver == null) {139 try {140 driver = (Driver) Class.forName(this.getDriverClassName()).newInstance();141 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {142 throw new NoDriverFoundException("Could not get Driver", e);143 }144 }145 }146 return driver;147 }148 /**149 * Creates a connection to the underlying containerized database instance.150 *151 * @param queryString query string parameters that should be appended to the JDBC connection URL.152 * The '?' character must be included153 * @return a Connection154 * @throws SQLException if there is a repeated failure to create the connection155 */156 public Connection createConnection(String queryString) throws SQLException, NoDriverFoundException {157 final Properties info = new Properties();158 info.put("user", this.getUsername());159 info.put("password", this.getPassword());160 final String url = constructUrlForConnection(queryString);161 final Driver jdbcDriverInstance = getJdbcDriverInstance();162 SQLException lastException = null;163 try {164 long start = System.currentTimeMillis();165 // give up if we hit the time limit or the container stops running for some reason166 while (System.currentTimeMillis() < start + (1000 * connectTimeoutSeconds) && isRunning()) {167 try {168 logger().debug("Trying to create JDBC connection using {} to {} with properties: {}", driver.getClass().getName(), url, info);169 return jdbcDriverInstance.connect(url, info);170 } catch (SQLException e) {171 lastException = e;172 Thread.sleep(100L);173 }174 }175 } catch (InterruptedException e) {176 Thread.currentThread().interrupt();177 }178 throw new SQLException("Could not create new connection", lastException);179 }180 /**181 * Template method for constructing the JDBC URL to be used for creating {@link Connection}s.182 * This should be overridden if the JDBC URL and query string concatenation or URL string183 * construction needs to be different to normal.184 *185 * @param queryString query string parameters that should be appended to the JDBC connection URL.186 * The '?' character must be included187 * @return a full JDBC URL including queryString188 */189 protected String constructUrlForConnection(String queryString) {190 return getJdbcUrl() + queryString;191 }192 protected void optionallyMapResourceParameterAsVolume(@NotNull String paramName, @NotNull String pathNameInContainer, @NotNull String defaultResource) {193 String resourceName = parameters.getOrDefault(paramName, defaultResource);194 if (resourceName != null) {195 final MountableFile mountableFile = MountableFile.forClasspathResource(resourceName);196 withCopyFileToContainer(mountableFile, pathNameInContainer);197 }198 }199 /**200 * Load init script content and apply it to the database if initScriptPath is set201 */202 protected void runInitScriptIfRequired() {203 if (initScriptPath != null) {204 ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath);205 }206 }207 public void setParameters(Map<String, String> parameters) {208 this.parameters = parameters;209 }210 @SuppressWarnings("unused")211 public void addParameter(String paramName, String value) {212 this.parameters.put(paramName, value);213 }214 /**215 * @return startup time to allow, including image pull time, in seconds216 * @deprecated should not be overridden anymore, use {@link #withStartupTimeoutSeconds(int)} in constructor instead217 */218 @Deprecated219 protected int getStartupTimeoutSeconds() {220 return startupTimeoutSeconds;221 }222 /**223 * @return time to allow for the database to start and establish an initial connection, in seconds224 * @deprecated should not be overridden anymore, use {@link #withConnectTimeoutSeconds(int)} in constructor instead225 */226 @Deprecated227 protected int getConnectTimeoutSeconds() {228 return connectTimeoutSeconds;229 }230 protected DatabaseDelegate getDatabaseDelegate() {231 return new JdbcDatabaseDelegate(this, "");232 }233 public static class NoDriverFoundException extends RuntimeException {234 public NoDriverFoundException(String message, Throwable e) {235 super(message, e);236 }237 }238}...

Full Screen

Full Screen

NoDriverFoundException

Using AI Code Generation

copy

Full Screen

1public static void main(String[] args) {2 try {3 try (final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>()) {4 postgres.start();5 Connection connection = DriverManager.getConnection(postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword());6 System.out.println(connection);7 }8 } catch (NoDriverFoundException e) {9 e.printStackTrace();10 } catch (SQLException e) {11 e.printStackTrace();12 }13}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful