Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8728691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:41:54+00:00 2026-06-13T08:41:54+00:00

I have a Rest Service Client which I am trying to test. This is

  • 0

I have a Rest Service Client which I am trying to test.
This is the client method I’m trying to test:

public RestPriceRow getPriceRow(String customer, String product, String qt)
    throws Exception {
    // Prepare acceptable media type
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    acceptableMediaTypes.add(MediaType.APPLICATION_XML);

    // Prepare header
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(acceptableMediaTypes);
    HttpEntity<PriceRowDto> entity = new HttpEntity<PriceRowDto>(headers);

    // Prepare response
    RestPriceRow res = new RestPriceRow();

    // Send the request as GET
    try {
        ResponseEntity<PriceRowDto> result =
          restTemplate.exchange(baseURL + "{customerId}/{productId}/{quantity}",
          HttpMethod.GET, entity, PriceRowDto.class, customer,product,qt);
        PriceRowDto response = result.getBody();
        return response;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And this is my test class:

@Before
public void setUp() {
    RestTemplate r = mock(RestTemplate.class);
    restPriceService = new RestPriceServiceImpl();
    restPriceService.setRestTemplate(r);
    restPriceService.setBaseURL("url");
}

@Test
public void prueba() {
    PriceRowDto r = new PriceRowDto("100","200","9","10","0","19/11/2012");
    try {
        when(restPriceService.getPriceRow("100","200","16")).thenReturn(r);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

My problem is that when I run this test, it throws me a NullPointerException when executing result.getBody(). (result is null)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:41:55+00:00Added an answer on June 13, 2026 at 8:41 am

    You mixed up the SUT (System Under Test) with the mocked Collaborators. The SUT is the thing who’s method you test. The Collaborators play along and return data as needed for the SUT to work.

    1. Call the instance of RestPriceService that you want to test sut: TODO
    2. Mock restTemplate: OK
    3. Give this mock a name that makes it stick out like restTemplateMock: TODO
    4. Set it on sut: OK
    5. Use when(restTemplateMock.exchange(...).thenReturn(r): TODO
    6. Call sut.getPriceRow: TODO
    7. Verify that the RestPriceRow returned by this call is correct: TODO
    8. Verify restTemplateMock: TODO

    Edit:

    If you want to read a very good book about the different forms of unit tests, I can recommend XUnit Test Patterns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF-REST service with one method which returns a string: [ServiceContract] public
I have a REST web service which exposes 2 methods- [POST method] Client will
I have a WCF REST Service which accepts a JSON string One of the
I have a java REST service which works fine, I'm trying to access to
While creating a simple client for a REST service which I have stubbed out,
I'm trying to apply a client certificate to a WCF REST Service. I have
I have a REST service consumed by a .Net WCF client. When an error
I have created a REST web service and successfully used it with a client.
I have a REST service that returns a JSON like this: [{@id:123,name:Name}] and I'm
I have a REST service for which I have a WADL file. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.