使用另一个类中的结果?

时间:2014-06-24 06:28:53

标签: java

我在Java中有2个类,我试图将一个类的结果用作第二个类的值,这就是我所拥有的。

DBCall.java -

package com.example.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

public class DBCall {

public void generateid(){

    int count;


    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://IP:PORT;DatabaseName=SubscriberManager;integratedSecurity=true;");
        System.out.println("Connection Successfull");
        System.out.println(conn); 

        //--------------------------------------------------------------------
        Statement stmt = conn.createStatement();
        String query1= "SELECT COUNT(*) from dbo.vSubscriberReporting where [SubscriberEmailAddressStatusID] in (5,1,6,7) and [SubscriberStatusID] = 1 AND [SubscriberAddresseeStatusID] = 1 and CompanySizeCodeID IN (4)";
        ResultSet rs= stmt.executeQuery(query1);

        if(rs.next()) //Expecting one row.
        {
    count = rs.getInt(1);
             System.out.println("Value: " + count);

        }
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }

public static void main(String[] args) {
    DBCall gen = new DBCall();
    gen.generateid();

}}

QuickJavaTest.java

package com.example.tests;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class QucikJavaTest {
 private WebDriver driver;
 private String baseUrl;
 private boolean acceptNextAlert = true;
 private StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
 driver = new FirefoxDriver();
baseUrl = "https://testurl";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void ConfirmValue7() throws Exception {
driver.get(baseUrl + "/web/SubscriberManagement/Admin");
new Select(driver.findElement(By.id("SelectedFilter"))).selectByVisibleText("Job Title");
driver.findElement(By.id("Filters_0__Values_GB")).click();
driver.findElement(By.id("Filters_0__Values_AF")).click();
driver.findElement(By.cssSelector("button")).click();
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.id("Filters_1__Value"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
}

driver.findElement(By.xpath("(//a[contains(text(),'Delete')])[2]")).click();
new Select(driver.findElement(By.id("SelectedFilter"))).selectByVisibleText("Job Function");
driver.findElement(By.cssSelector("button")).click();
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.id("Filters_1__Values"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
}

driver.findElement(By.id("Filters_1__Values_28")).click();
driver.findElement(By.cssSelector("button.floatRight")).click();
driver.findElement(By.id("sqlDetailsHeading")).click();
try {
  assertEquals("7", driver.findElement(By.cssSelector("span.count")).getText());
} catch (Error e) {
  verificationErrors.append(e.toString());
}
 }

@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
  fail(verificationErrorString);
}
 }

 private boolean isElementPresent(By by) {
try {
  driver.findElement(by);
  return true;
} catch (NoSuchElementException e) {
  return false;
}
 }

public boolean isAcceptNextAlert() {
return acceptNextAlert;
}

public void setAcceptNextAlert(boolean acceptNextAlert) {
this.acceptNextAlert = acceptNextAlert;
}
}

我想要做的是取" count"来自DBCall.java并使用它作为值来验证QuickJavaTest.java中的assertEquals(" 7"),我确定它很简单,我只是无法找到最佳方法。

1 个答案:

答案 0 :(得分:0)

您需要返回从数据库中generateid方法获取的值,然后您可以根据需要在其他方法中使用它。

将方法从返回void更改为返回int

public int generateid() {
    int count = 0;
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://IP:PORT;DatabaseName=SubscriberManager;integratedSecurity=true;");
        System.out.println("Connection Successfull");
        System.out.println(conn); 

        Statement stmt = conn.createStatement();
        String query1= "SELECT COUNT(*) from dbo.vSubscriberReporting where [SubscriberEmailAddressStatusID] in (5,1,6,7) and [SubscriberStatusID] = 1 AND [SubscriberAddresseeStatusID] = 1 and CompanySizeCodeID IN (4)";
        ResultSet rs= stmt.executeQuery(query1);

        if(rs.next()) //Expecting one row.
        {
            count = rs.getInt(1);
            System.out.println("Value: " + count);
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return count;
}

然后,以所需方法检索数据:

@Test
public void ConfirmValue7() throws Exception {
    DBCall dbCall = new DBCall();
    int count = dbCall.generateid();
    //use count as you want/need
    //rest of your code...
}