Hot questions for Using Cucumber in junit4

Question:

I have declare 2 methods @after annotation but it will execute only first method, it is not allow me to execute second method. Please have a look in below code

I want to execute 1 method every time for exit of browser. I want to execute second method for failed test cases.

enter code here

@Before
public void databaseLoading(Scenario scenario) {
    //System.out.println("Test Environment Set Up");
    System.out.println("\n------------------------------------------------     TEST ENVIRONMENT SET UP      ------------------------------------------------------------------------------\n");
    System.out.println("Executing Scenario :-> " + scenario.getName());
}

@After
public void browserTearDown()
{
    System.out.println("End the browser");
    driver.close();
 }

public void Screenshot(Scenario scenario) {
    // take the screenshot at the end of every test
    String location = ".\\target\\TakeScreenshot";
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh-mm-ss", Locale.ENGLISH);
    Date date = new Date();
    File scrFile =
            ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    // now save the screenshto to a file some place
    if (scenario.isFailed()) {
        try {
            FileUtils.copyFile(scrFile, new File(location + "\\" + dateFormat.format(date) + ".png"));
            System.out.println("Screenshot saved");
        } catch (IOException e) {
            System.out.println("Error in taking Screenshot --> " + e);
        }
    }
}

Method 'Screenshot(cucumber.api.Scenario)' is never used. this error message comes for second method.


Answer:

You only tagged browserTearDown() method.

Add the @After tag to the Screenshot() method, as well:

@After
public void browserTearDown()
{
    System.out.println("End the browser");
    driver.close();
}

@After
public void Screenshot(Scenario scenario) {
   ...

}

Question:

I am writing cucumber tests that query an SQL Server database.

The below query should fail, as the record does not exist in the table. But, the test passes.

try { 
        Class.forName(JDBC_DRIVER); 
        System.out.println("Connecting to database..."); 
        conn = DriverManager.getConnection(DB_URL,USER,PASS);  

        System.out.println("Connected database successfully..."); 
        stmt = conn.createStatement(); 
        String sql = "SELECT id FROM tclientlink WHERE id = '0000060115123'"; 
        ResultSet rs = stmt.executeQuery(sql);

        while(rs.next()) { 
            int id  = rs.getInt("goald_client_id"); 
            System.out.print("ID IS HERE: " + id); 
            assertEquals(60115, id);
        } 
        rs.close(); 
    } catch(SQLException se) { 
        se.printStackTrace(); 
    } catch(Exception e) { 
        e.printStackTrace(); 
    } finally { 
        try { 
            if(stmt!=null) stmt.close();  
        } catch(SQLException se2) { 
        }
        try { 
            if(conn!=null) conn.close(); 
        } catch(SQLException se) { 
            se.printStackTrace(); 
        }
    }
    System.out.println("Goodbye!"); 
} 

Answer:

you have NullPointerException on the following line :

int id  = rs.getInt("goald_client_id"); 

and it goes to the exception block, just add the following statement in all exception blocks

assertEquals(true, false);