18 Nisan 2019 Perşembe

SpringBoot Test @SpyBean Anotasyonu - Hem Bean Hem Mockito Spy

Giriş
Şu satırı dahil ederiz.
import org.springframework.boot.test.mock.mockito.SpyBean;
Örnek
Elimizde şöyle bir kod yaparız
@Repository
public interface DomainRepository extends MongoRepository<Foo, String> {
  ...
}
Şöyle yaparız
@SpringBootTest
@Testcontainers
@Slf4j
public class CachingServiceIntegrationTest {

  @Autowired
  private CachingService cachingService;    

  @SpyBean
  private DomainRepository repository;

  @BeforeEach
  public void setUp() {
    repository.save(...);
  }

  @AfterEach
  public void tearDown() {
    repository.deleteAll();
    ...
  }
  ...
}
Örnek
Şöyle yaparız
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApp.class)
public class MyServiceTest {

  @SpyBean
  private MyService myService;

  @Test
  public void testMyMethod() {
    ...
  }
}

Hiç yorum yok:

Yorum Gönder