Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用BufferedReader和逗号拆分从csv列表返回两个参数_Java_Csv_Junit4_Bufferedreader - Fatal编程技术网

Java 使用BufferedReader和逗号拆分从csv列表返回两个参数

Java 使用BufferedReader和逗号拆分从csv列表返回两个参数,java,csv,junit4,bufferedreader,Java,Csv,Junit4,Bufferedreader,请原谅初学者的问题,但我首先是一名测试人员,我正在努力理解如何解决这个问题。我只是尝试通过使用缓冲输入流读取器读取部分网站url及其对应的syndicator登录ID,并从每行返回两个值(用逗号分隔),来填充一些测试数据 以下是我的csv: website1.uk, website1syndicator website2.uk, website2syndicator website3.uk, website3syndicator 下面是我的课堂,阅读csv并用一个字符串元素填充列表: p

请原谅初学者的问题,但我首先是一名测试人员,我正在努力理解如何解决这个问题。我只是尝试通过使用缓冲输入流读取器读取部分网站url及其对应的syndicator登录ID,并从每行返回两个值(用逗号分隔),来填充一些测试数据

以下是我的csv:

 website1.uk, website1syndicator
 website2.uk, website2syndicator
 website3.uk, website3syndicator
下面是我的课堂,阅读csv并用一个字符串元素填充列表:

public class AbstractTestAllSites extends PageBase {

private static Logger log = LoggerFactory.getLogger(AbstractTestAllSites.class);

private static List<String> allWebsiteNames;

static {
    try (InputStream websiteListInputStream = AbstractTestAllSites.class.getResourceAsStream("/websites/my_sites.csv")) {
        readAllWebsiteNamesFrom(websiteListInputStream);
    } catch (IOException e) {
        log.error("Failed to read websitelist!", e);
    }
}

private static void readAllWebsiteNamesFrom(InputStream input) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
    List<String> websites = new ArrayList<String>();
    String listLine;
    while ((listLine = reader.readLine()) != null) {
        listLine = listLine.trim();
        if (!(listLine.startsWith("#") || isBlank(listLine))) {
            websites.add(listLine);
        }
    }
    allWebsiteNames = unmodifiableList(websites);
}

@Parameterized.Parameters
public static final List<String> data() {
    return allWebsiteNames;
}

…并对其进行迭代,直到完成。但是如何传入两个参数以便访问syndicator变量?可能需要一个HashMap或类似的,然后用逗号拆分,但有点困难

如果您想知道如何分割csv文件的每一行,以便创建类
TestAllSiteSt
的对象(该类对象有一个构造函数,它包含一个网站和一个联合体),您可以按如下方式操作(在代码中所需的位置,这只是显示示例的主要方法):

publicstaticvoidmain(字符串[]args){
//创建两个ArrayList,第一个包含行,第二个包含所需的对象
列出网站=新建ArrayList();
List testAllSitesTests=new ArrayList();
//将csv行添加到第一个ArrayList
添加(“website1.uk,website1syndicator”);
添加(“website2.uk,website2syndicator”);
添加(“website3.uk,website3syndicator”);
//迭代包含csv行的列表
网站。forEach((字符串网站)->{
//将一行拆分为所需的两部分,消除逗号和空格
字符串[]splitWebsite=website.split(“,”);
//创建一个新对象,将分割线的各个部分作为构造函数参数传递
TestAllSiteST测试=新约TestAllSiteST(拆分网站[0],拆分网站[1]);
testAllSitesTests.add(测试);
});
//打印生成的对象
TestAllSitesTest.forEach((TestAllSitesTest t)->{
System.out.println(“网站:+t.getWebsite()
+,联合体:“+t.getSyndicator());
});
}

我希望这有帮助……

我不明白。你的问题是什么?你能换一种说法吗?因为我很难理解你的问题是什么。
private static final String url = "http://mydomain.";
private String website;
private String syndicator;
public static WebDriver driver;

public TestAllSitesTest(String website, String syndicator){
    this.website = website;
    this.syndicator = syndicator;
}

@Before
public void getNextWebsite(){
    driver.get(url + this.website);
}
//run my tests here...
public static void main(String[] args) {
    // create two ArrayLists, first one containing lines, second containing desired objects
    List<String> websites = new ArrayList<String>();
    List<TestAllSitesTest> testAllSitesTests = new ArrayList<TestAllSitesTest>();

    // add csv lines to the first ArrayList
    websites.add("website1.uk, website1syndicator");
    websites.add("website2.uk, website2syndicator");
    websites.add("website3.uk, website3syndicator");

    // iterate the list containing the csv lines
    websites.forEach((String website) -> {
        // split one line into the desired two parts, eliminating comma and space
        String[] splitWebsite = website.split(", ");
        // create a new object passing the parts of the split line as constructor parameters
        TestAllSitesTest test = new TestAllSitesTest(splitWebsite[0], splitWebsite[1]);
        testAllSitesTests.add(test);
    });

    // print the resulting objects
    testAllSitesTests.forEach((TestAllSitesTest t) -> {
        System.out.println("Website: " + t.getWebsite()
                + ", Syndicator: " + t.getSyndicator());
    });
}