Annotation Interface SUT


@SUT (Service Under Test) is an annotation used on fields and methods of a Test class to inject a spy of the real service. Note that calling methods on the spy will call the methods of the real service unless they are stubbed with when()/give(). You can disable spying by setting value() to false.

Example:


@Service
public class GreetingService {

  public String greet() {
    return sayHello();
  }

  public String sayHello() {
    return "Hello!";
  }
}

 

@HK2
public class GreetingServiceTest {

  @SUT
  @Inject
  GreetingService sut;

  @BeforeMethod
  public void init() {
    reset(sut);
  }

  @Test
  public void verifyInjection() {
    assertThat(sut)
      .isNotNull()
      .isInstanceOf(MockitoSpy.class);
  }

  @Test
  public void callToGreetShouldReturnHello() {
    String greeting = "Hello!";

    String result = sut.greet();

    assertThat(result).isEqualTo(greeting);
    verify(sut).greet();
    verify(sut).sayHello();
  }

  @Test
  public void callToGreetShouldReturnHola() {
    String greeting = "Hola!";
    when(sut.sayHello()).thenReturn(greeting);

    String result = sut.greet();

    assertThat(result).isEqualTo(greeting);
    verify(sut).greet();
    verify(sut).sayHello();
  }
}
 
 
Author:
Sharmarke Aden
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Indicates whether a spy should be created.
  • Element Details

    • value

      boolean value
      Indicates whether a spy should be created. By default a spy of the real service is created. Note that the spy calls real methods unless they are stubbed.
      Returns:
      true if a spy should be created.
      Default:
      true