A
A
Andrey Shishkin2016-04-11 17:57:29
Java
Andrey Shishkin, 2016-04-11 17:57:29

How to check value as number?

I am writing a rest test. Rest returns xml, in one of the elements of which the value has a numeric form.
How can I check that this number is non-zero? I'm trying to defeat this issue with org.hamcrest.xml.HasXPath.hasXPath and org.hamcrest.Matchers.greaterThan, but nothing comes out yet. I need an example, but I did not find anything suitable by Google.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Shishkin, 2016-04-11
@compiler

Here is a piece of working code

@Test
    public void myTest() throws Exception {

        final HttpURLConnection urlConnection = getConnection(HOST_URL + MY_URL);

        prepareConnection(urlConnection);
        urlConnection.setRequestMethod("POST");

        BufferedReader br = new BufferedReader(new InputStreamReader((urlConnection.getInputStream())));
        StringBuilder sb = new StringBuilder();
        String output;
        while ((output = br.readLine()) != null) {
            sb.append(output);
        }

        assertThat("Incorrect fields or values in the response xml.", parse(sb.toString()), hasXPath("ParentNode/ChildNode", containsString("42")));

    }

But I need to check that ChildNode has a non-zero number and not a string that has "42" in it. There is an org.hamcrest.Matchers.greaterThan matcher for this, but the problem is that it can't be used here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question