Read data using excel file data driven approach using TestNG (Data Provider). Reading Login details from External Excel file using…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
//Downaload and Add ref to JDBC 6.0. jar location=> // ../Microsoft JDBC Driver 6.0 for SQL Server\sqljdbc_6.0\enu\jre8\sqljdbc42.jar public static LinkedHashMap<Integer,LinkedHashMap<Integer,String>> f_get_sql_result_map(String s_query){ String s_result; String connectionUrl; String DB_LOG_SERVER_NAME = "<Server name>"; Map<String,String> map_result = new HashMap<String,String>(); LinkedHashMap<Integer,String> o_clm_data; LinkedHashMap<Integer,LinkedHashMap<Integer,String>> tbl_map_result = new LinkedHashMap<Integer,LinkedHashMap<Integer,String>>(); String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; connectionUrl = "jdbc:sqlserver://" + DB_LOG_SERVER_NAME+";" + "user=<username>;password=<password>;"; Connection conn = null; Statement stmt = null; ResultSet rs =null; try{ //STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(connectionUrl); stmt = conn.createStatement(); rs = stmt.executeQuery(s_query); ResultSetMetaData rsmd = rs.getMetaData(); int i_clm_count = rsmd.getColumnCount(); int i_map_row_counter=0; while(rs.next()){ o_clm_data = new LinkedHashMap<Integer,String>(); for(int i=1;i<=i_clm_count;i++){ o_clm_data.put(i-1,rs.getString(i)); }//For closed tbl_map_result.put(i_map_row_counter, o_clm_data); i_map_row_counter = i_map_row_counter + 1; }//While Closed //System.out.println("Inside f_get_sql_result_multiple_records: " + map_result_query.get("ROW_1")); if(tbl_map_result.isEmpty()) { System.out.println("warn: Recordset is Empty. i.e. no records have been returned by the query. Connection Url: " + connectionUrl + " and Query: " + s_query); }else{ System.out.println("info: Recordset has been stored in HashMap and returned. Connection Url: " + connectionUrl + " and Query: " + s_query); } return tbl_map_result; }catch(SQLException se){ //Handle errors for JDBC System.out.println("warn:SQl Exception: " + se.toString() + " Query used: " + s_query); //se.printStackTrace(); return tbl_map_result; }catch(Exception e){ //Handle errors for Class.forName //e.printStackTrace(); System.out.println("warn:Other General Exception: " + e.getMessage() + " Query used: " + s_query); return tbl_map_result; }finally{ //finally block used to close resources try{ if(stmt!=null) rs.close(); conn.close(); }catch(SQLException se){ System.out.println("warn:Unable to close connection f_get_sql_result_multiple_records: " + se.getMessage()); }// do nothing try{ if(conn!=null) rs.close(); conn.close(); }catch(SQLException se){ //se.printStackTrace(); System.out.println("warn:Unable to close the Connection f_get_sql_result_multiple_records" + se.getMessage() + " Query used: " + s_query); }//end finally try }//end try }//End of Function |
1 2 3 4 5 6 7 8 9 |
public static String f_generate_random_numeric_string(long i_min, long i_max){ Random rand = new Random(); long random = (int )(Math.random() * i_max + i_min); String result = Long.toString(random); System.out.println("info", "Random numeric string generated. Min and Max mentioned as: " + i_min + " max : " + i_max + " result: " + result); return result; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public static String f_generate_time_based_unique_integer(){ String result; String y,mon,d,h,min,s,mil; Calendar now = Calendar.getInstance(); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) + 1; // Note: zero based! int day = now.get(Calendar.DAY_OF_MONTH); int hour = now.get(Calendar.HOUR_OF_DAY); int minute = now.get(Calendar.MINUTE); int second = now.get(Calendar.SECOND); int millis = now.get(Calendar.MILLISECOND); y = Integer.toString(year); mon = Integer.toString(month); d = Integer.toString(day); h = Integer.toString(hour); min = Integer.toString(minute); s = Integer.toString(second); mil = Integer.toString(millis); result = y+mon+d+h+min+s; System.out.println("info", "Unique Time based String Generated: " + result); return result; } |
Java Programming Tutorial Credits: Practice problems taken from below Site. http://www.ntu.edu.sg/home/ehchua/programming/java/j2a_basicsexercises.html#zz-5 Exercises on Java Basics You need to do these…
Program: List of JUnit annotations. Annotations are introduced in JUnit4. Here are the list of annotations and its descriptions…
Understanding JUnit Annotations: Try running below Program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package JUnitFW; import static org.junit.Assert.*; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class JUnitTest { @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("Inside Before Class"); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("Inside After Class"); } @Before public void setUp() throws Exception { System.out.println("Inside Before "); } @After public void tearDown() throws Exception { System.out.println("Inside After"); } @Test public void test1() { //fail("Not yet implemented"); System.out.println("Inside 1st Test"); } @Test public void test2() { //fail("Not yet implemented"); System.out.println("Inside 2nd Test"); } @Test public void test3() { //fail("Not yet implemented"); System.out.println("Inside 3rd Test"); } } |
Result: Inside Before Class Inside Before Inside 1st Test Inside After Inside…
Program: Simple JUnit test using @Test annotation. JUnit is a simple framework to write repeatable tests. It is an…
Below Program is a Demonstrate usage of Class, Objects, variables and Methods. Calculator Class Source File:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.mypacakage.demo; class CalculatorClass { int val1; int val2; int result; String operation; //Call Below Function after Object creation with arguments //These function will set Result instance variable and display the result //Addition Function public void addition(int arg1, int arg2){ result = arg1+arg2; } //Subtraction Function public void subtraction(int arg1, int arg2){ result = arg1-arg2; } //Multiplication Function public void multiplication(int arg1, int arg2){ result = arg1*arg2; } //Division Function public void division(int arg1, int arg2){ result = arg1/arg2; } //Call any Operation //Function to Demonstrate that while Object is created from other Class Function, //we can set the Class instance variable val1 and val2 and just call the this function public void call_any_ops(char ops){ switch(ops){ case 'A': addition(val1,val2); break; case 'S' : subtraction(val1,val2); break; case 'D' : multiplication(val1,val2); break; case 'M' : division(val1,val2); break; } //Call Show Result Method showresult(); } public void showresult(){ System.out.println("Result of the Operation: " + result); } } |
CalculatorTest Class Source…