Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why does my java unit test keep failing?

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does my

    This is my code:

    HttpURLConnection connection = null;
    URL url = new URL(AUTHORITY);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");

    connection.setDoOutput(true);
    connection.connect();

    BufferedWriter out
    = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
    out.write(requestBody);
    out.close();


    BufferedReader in = null;
    StringBuffer response = null;
    String access_token = null;

    // handle response

    int code = -1;
    try {
    code = connection.getResponseCode();
    // bad response
    if (code != OAuthConstants.HTTP_OK ) {
    logger.error("Access token is expired " + code);
    throw new RuntimeException(
    "Unable to get connection to Azure Directory");
    }else{
    in = new BufferedReader(
    new InputStreamReader(connection.getInputStream()));
    response = new StringBuffer();

    String temp;
    while ((temp = in.readLine()) != null) {
    response.append(temp);
    }
    Object jResponse;
    jResponse = JSONValue.parse(response.toString());
    JSONObject jObject = (JSONObject) jResponse;
    access_token = jObject.get("access_token").toString();
    }

    } catch (IOException e) {
    logger.error("Problem with getting response from Azure");
    e.printStackTrace();
    } finally {
    connection.disconnect();
    in.close();
    }

    return access_token;




    and this is the TEST CODE:


    HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
    URL url = PowerMockito.mock(URL.class);
    PowerMockito.whenNew(URL.class).withAnyArguments() .thenReturn(url);

    Mockito.when(url.openConnection()).thenReturn(conn ection);
    Mockito.when(connection.getOutputStream()).thenRet urn(new ByteArrayOutputStream(100));
    Mockito.when(connection.getResponseCode()).thenRet urn(200);
    Mockito.when(connection.getInputStream()).thenRetu rn(new ByteArrayInputStream("TOKEN".getBytes("UTF-8")));


    PowerMockito.mockStatic(Authenticate.class);
    Mockito.mock(Authenticate.class);
    String result = Authenticate.handleAuthRequest(Mockito.anyString() , Mockito.anyString());

    Assert.assertNotNull(result);
    Assert.assertEquals(result, "TOKEN");





    Everything looks ok to me in both codes, however my test keeps failing during assertion:
    Assert.assertNotNull(result);
    Assert.assertEquals(result, "TOKEN");

    ..... Is it because i've written the test incorrectly or becauase there is something wrong with my code. please help !

  2. #2
    Junior Member
    Join Date
    Jul 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does my java unit test keep failing?

    This is my code:

    HttpURLConnection connection = null;
    URL url = new URL(AUTHORITY);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");

    connection.setDoOutput(true);
    connection.connect();

    BufferedWriter out
    = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
    out.write(requestBody);
    out.close();


    BufferedReader in = null;
    StringBuffer response = null;
    String access_token = null;

    // handle response

    int code = -1;
    try {
    code = connection.getResponseCode();
    // bad response
    if (code != OAuthConstants.HTTP_OK ) {
    logger.error("Access token is expired " + code);
    throw new RuntimeException(
    "Unable to get connection to Azure Directory");
    }else{
    in = new BufferedReader(
    new InputStreamReader(connection.getInputStream()));
    response = new StringBuffer();

    String temp;
    while ((temp = in.readLine()) != null) {
    response.append(temp);
    }
    Object jResponse;
    jResponse = JSONValue.parse(response.toString());
    JSONObject jObject = (JSONObject) jResponse;
    access_token = jObject.get("access_token").toString();
    }

    } catch (IOException e) {
    logger.error("Problem with getting response from Azure");
    e.printStackTrace();
    } finally {
    connection.disconnect();
    in.close();
    }

    return access_token;




    and this is the TEST CODE:


    HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
    URL url = PowerMockito.mock(URL.class);
    PowerMockito.whenNew(URL.class).withAnyArguments() .thenReturn(url);

    Mockito.when(url.openConnection()).thenReturn(conn ection);
    Mockito.when(connection.getOutputStream()).thenRet urn(new ByteArrayOutputStream(100));
    Mockito.when(connection.getResponseCode()).thenRet urn(200);
    Mockito.when(connection.getInputStream()).thenRetu rn(new ByteArrayInputStream("TOKEN".getBytes("UTF-8")));


    PowerMockito.mockStatic(Authenticate.class);
    Mockito.mock(Authenticate.class);
    String result = Authenticate.handleAuthRequest(Mockito.anyString() , Mockito.anyString());

    Assert.assertNotNull(result);
    Assert.assertEquals(result, "TOKEN");





    Everything looks ok to me in both codes, however my test keeps failing during assertion:
    Assert.assertNotNull(result);
    Assert.assertEquals(result, "TOKEN");

    ..... Is it because i've written the test incorrectly or becauase there is something wrong with my code. please help !

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,290
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Why does my java unit test keep failing?

    Threads merged.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Are there any error messages? Please copy the full text and paste it here.

    What package is the Mockito class in? The posted code is missing the import statements.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. IPv6 validation is failing with java.net.InetAddress
    By maganti in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2014, 02:45 AM
  2. Java School Project Help; Project #2, In Danger of Failing.....
    By john++ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2014, 02:35 AM
  3. Intro. to Java Help.... In Danger of Failing Class
    By john++ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2014, 02:32 AM
  4. My Java program is failing to run in command prompt
    By George Mawire in forum What's Wrong With My Code?
    Replies: 26
    Last Post: January 24th, 2014, 06:54 AM
  5. Java Unit Testing How-To Gettin' started
    By Massaslayer in forum Java Theory & Questions
    Replies: 2
    Last Post: May 1st, 2012, 07:58 AM

Tags for this Thread