1

file not found exception

Following are the logs:

TestNG] INVOKING: "test" - testscripts.LoginTest.loginWithValidCredentialsTest()
[Invoker 1915058446] Invoking testscripts.LoginTest.loginWithValidCredentialsTest
2018-01-22 15:09:25 INFO  testpages.LoginPage:46 - clicked on skip button
2018-01-22 15:09:27 INFO  testpages.LoginPage:50 - clicked on log in button
2018-01-22 15:09:27 INFO  testpages.LoginPage:96 - Enter user name and password
java.io.FileNotFoundException: src/test/resources/LoginPage.json (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
Suraj Jogdand
  • 308
  • 2
  • 17
  • can anyone help on this issue, i am also facing the same issue. – Suraj Jogdand Feb 23 '18 at 11:00
  • I tried following solution : ' java.net.URL resource = ClassLoader.getSystemResource("testdata/test.properties"); File file = new File(resource.toURI()); FileInputStream input = new FileInputStream(file);' Locally the solution works fine but when i create a dependency jar form by command mvn clean package -DskipTests=true, and then if i execute it on AWS then i am getting the error as "[TestNG] java.lang.IllegalArgumentException: URI is not hierarchical" – Suraj Jogdand Feb 26 '18 at 07:32

1 Answers1

0

I was able to solve above problem. Simply we have to keep the files under resources directory. like in the below image

enter image description here

Now add the following dependency to the pom.xml :

<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

Use following code and you are ready to test it on AWS ;

 Test test = method.getAnnotation(Test.class);
    URL resource = ClassLoader.getSystemResource("test.json");
    InputStream stream = resource.openStream();
    String data_all = IOUtils.toString(stream, "UTF-8");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(data_all);
    JsonNode testNode = rootNode.get(test.testName());
    if (!testNode.get("Execute").asText().equals("Yes")) {
        throw new SkipException("Skipping the test as per Configuration");
    }
    if (testNode.get("Obsolete").asText().equals("Yes")) {
        throw new SkipException("Skipping the test as test is obsolete.");
    }
    userName = testNode.get("testData").get("userName").toString();
    password = testNode.get("testData").get("password").toString();

My test.json file is structured as below :

{
  "test_case_name": {
                     "testName": "test_case_name",
                     "testDescription": "test description goes here",
                     "testDataType": "test data type",
                     "Execute": "Yes",
                     "Obsolete": "No",
                     "testData": {
                                 "userName": "usernmae",
                                 "password": "Password"
                      },
                      "validations": {
                      "validation_value1": "NA",
                      "validation_value2": "NA"
   }
}
martin
  • 2,150
  • 1
  • 34
  • 48
Suraj Jogdand
  • 308
  • 2
  • 17