0

I am trying to read JSON file from test/resources package in my play application. I am getting com.couchbase.client.java.error.DocumentDoesNotExistException. I believe my path is not correct, can anyone suggest how to take absolute path?

public class AppControllerTest extends WithApplication {

    @Inject
    AppDaoServiceImpl appDaoServiceImpl;

    private CouchbaseEnvironment env;
    private static Cluster cluster = null;
    private static Bucket bucket = null;
    private String testResources = System.getProperty("java.class.path") + "/test/resources/";

    private static final ALogger logger = Logger.of(AppControllerTest.class);

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Override
    protected Application provideApplication() {
        return new GuiceApplicationBuilder().build();
    }

    @Before
    public void init() {
        env = DefaultCouchbaseEnvironment.create();
        cluster = CouchbaseCluster.create(env, "127.0.0.1:8091");
        bucket = cluster.openBucket("CLUSTER", "admin123");

        try {
            String docId = "ABEBV_common";
            File testResource = new File(testResources + "ABEBV_common.json");
            FileInputStream is = new FileInputStream(testResource);
            JsonNode testData = Json.parse(is);
            RawJsonDocument rawJsonDocument = RawJsonDocument.create(docId, testData.asText());
            bucket.upsert(rawJsonDocument);

        } catch (Exception e) {
        }

    }

    @Test
    public void testGenericData() {
        Http.RequestBuilder request = new Http.RequestBuilder().method(GET).uri("/app/ms/genericdata/ABEBV")
                .header("client_id", "chase");

        Result result = route(app, request);
        assertEquals(OK, result.status());
        assertEquals("application/json", result.contentType().get());
        assertTrue(contentAsString(result).contains("141-GYCVZY"));
    }

    @After
    public void deleteDocuments() {
        bucket.remove("ABEBV_common");
        bucket.close();
        cluster.disconnect();
    }

}
kulsin
  • 398
  • 4
  • 18
  • If you're trying to read the file from the class path, see https://stackoverflow.com/a/1464366/611819 – dnault Nov 20 '19 at 17:37

1 Answers1

0

Yes your path is not correct, System.getProperty("java.class.path") will return all the java class path the jvm is referring to You have to, instead use "user.dir".

private String testResources = System.getProperty("user.dir") + "/test/resources/";

hagarwal
  • 1,153
  • 11
  • 27