com.arextest.common.annotation.ArexMock Maven / Gradle / Ivy
Show all versions of arex-common Show documentation
package com.arextest.common.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation indicating that the result of invoking a method (or all methods
* in a class) can be mocked.
*
* Each time an advised method is invoked, mocking behavior will be applied,
* checking whether the method has been already invoked for the given arguments.
* A sensible default simply uses the method parameters to compute the key, but
* a SpEL expression can be provided via the {@link #key} attribute.
*
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface ArexMock {
/**
* Spring Expression Language (SpEL) expression for computing the key dynamically.
*
Default is {@code ""}, meaning all method parameters are considered as a key.
*
The SpEL expression evaluates against a dedicated context that provides the
* following meta-data:
*
* - Method arguments can be accessed by index. For instance the second argument
* can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
* can also be accessed by name if that information is available.
*
* An example is as follows
*
{@code
* @ArexMock(key = "#name + #id")
* FooModel foo(String name, int id) {
* FooModel model = biz codes;
* return model;
* }
*}
*
*/
String key() default "";
/**
* AREX will serialize and save the method returns on record,
* and will deserialize the method returns into object on replay.
* For methods with custom generic return types, AREX cannot get the actual return Type on record,
* so the method returns cannot be deserialized correctly on replay.
* Here need to configure the {@code acutalType}, default is {@code Object.class}, An example is as follows
*
{@code
* Class BaseModel {
* final T data;
* BaseModel(T data) {
* this.data = data;
* }
* T getData() {
* return data;
* }
* }
* @ArexMock(actualType = FooModel.class)
* BaseModel foo(int id) {
* FooModel model = biz codes;
* return new BaseModel<>(model);
* }
*}
*
*/
Class> actualType() default Object.class;
}