retrofit-mock / retrofit2.mock / NetworkBehavior

NetworkBehavior

class NetworkBehavior

A simple emulation of the behavior of network calls.

This class models three properties of a network:

Behavior can be applied to a Retrofit interface with MockRetrofit. Behavior can also be applied elsewhere using #calculateDelay(TimeUnit) and #calculateIsFailure().

By default, instances of this class will use a 2 second delay with 40% variance. Failures will occur 3% of the time. HTTP errors will occur 0% of the time.

Functions

calculateDelay

fun calculateDelay(unit: TimeUnit!): Long

Get the delay that should be used for delaying a response in accordance with configured behavior.

calculateIsError

fun calculateIsError(): Boolean

Randomly determine whether this call should result in an HTTP error in accordance with configured behavior. When true, #createErrorResponse() should be returned.

calculateIsFailure

fun calculateIsFailure(): Boolean

Randomly determine whether this call should result in a network failure in accordance with configured behavior. When true, #failureException() should be thrown.

create

static fun create(): NetworkBehavior!

Create an instance with default behavior.

static fun create(random: Random!): NetworkBehavior!

Create an instance with default behavior which uses random to control variance and failure calculation.

createErrorResponse

fun createErrorResponse(): Response<*>!

The HTTP error to be used when an error is triggered.

delay

fun delay(unit: TimeUnit!): Long

The network round trip delay.

errorPercent

fun errorPercent(): Int

The percentage of calls to #calculateIsError() that return true.

failureException

fun failureException(): Throwable!

The exception to be used when a failure is triggered.

failurePercent

fun failurePercent(): Int

The percentage of calls to #calculateIsFailure() that return true.

setDelay

fun setDelay(amount: Long, unit: TimeUnit!): Unit

Set the network round trip delay.

setErrorFactory

fun setErrorFactory(errorFactory: Callable<Response<*>!>!): Unit

Set the error response factory to be used when an error is triggered. This factory may only return responses for which Response#isSuccessful() returns false.

setErrorPercent

fun setErrorPercent(errorPercent: Int): Unit

Set the percentage of calls to #calculateIsError() that return true.

setFailureException

fun setFailureException(exception: Throwable!): Unit

Set the exception to be used when a failure is triggered.

setFailurePercent

fun setFailurePercent(failurePercent: Int): Unit

Set the percentage of calls to #calculateIsFailure() that return true.

setVariancePercent

fun setVariancePercent(variancePercent: Int): Unit

Set the plus-or-minus variance percentage of the network round trip delay.

variancePercent

fun variancePercent(): Int

The plus-or-minus variance percentage of the network round trip delay.