Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class TestRestTemplate

  1. Object
  2. TestRestTemplate

An HTTP client for a BackendTest served on a port – webEnvironment = RANDOM_PORT – through the backend’s own outbound client, so a compiled test sends real requests from the native binary too.

A path is relative to the server; an absolute URL is sent as it is. A 4xx or 5xx is a response to assert on, not an exception. Bodies convert to and from String, byte[], and Map or List as JSON.

The Java SE client behind cn1:backend cannot send PATCH (its HTTP client has a fixed set of verbs); use MockMvc for a PATCH route, or a compiled run.

Constructors

public TestRestTemplate(String rootUri)A client for the server at rootUri, such as http://127.0.0.1:8080.

Methods

public String getRootUri()
public TestRestTemplate withBasicAuth(String username, String password)This client, sending HTTP Basic credentials with every request.
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws IOException
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws IOException
public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables) throws IOException
public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) throws IOException
public void put(String url, Object request, Object... uriVariables) throws IOException
public void delete(String url, Object... uriVariables) throws IOException
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws IOExceptionSends any method with any body and headers.

Inherited methods

Constructor details

TestRestTemplate

public TestRestTemplate(String rootUri)
A client for the server at rootUri, such as http://127.0.0.1:8080.

Method details

getRootUri

public String getRootUri()

withBasicAuth

public TestRestTemplate withBasicAuth(String username, String password)
This client, sending HTTP Basic credentials with every request.

getForEntity

public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws IOException

getForObject

public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws IOException

postForEntity

public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables) throws IOException

postForObject

public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) throws IOException

put

public void put(String url, Object request, Object... uriVariables) throws IOException

delete

public void delete(String url, Object... uriVariables) throws IOException

exchange

public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws IOException
Sends any method with any body and headers.