20 Ekim 2019 Pazar

PowerMockito Static Metodlar

Giriş
Şu satırı dahil ederiz.
import org.powermock.api.mockito.PowerMockito;
PowerMockito Nedir
Açıklaması şöyle. Yani PowerMockito ismi PowerMock kütüphanesinin altta Mockito kullanan hali.
PowerMock consists of two extension API’s. One for EasyMock and one for Mockito.
Not : Bu yazıda PowerMockito anlatılıyor. Eğer EasyMock kullanıyorsak PowerMockito sınıfı yerine PowerMock sınıfını kullanmak gerekir.

PowerMock ve Mockito Sürümü
PowerMock sadece belirtilen Mocktio sürümü ile çalşıyor. Sürümler burada.

Static Metodlar
Not : Yeni Mockito ile static metodları da mock'lamak artık mümkün. Mockito ve Static Metodlar yazısına bakabilirsiniz.
static metodlar, private metodlar ve final metodlar PowerMock ile mocklanabilir. Açıklaması şöyle.
PowerMock uses custom classloader and bytecode manipulation to allow mocking of static methods, builders, final classes, private methods and more. So we can test our code without modifying it, thanks to the fact that the mock objects are hooked to the code to be tested in execution time. By using reflection this framework allows you to modify even private attributes of the class.
Maven
Şu satırı dahil ederiz.
<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4</artifactId>
  <version>1.7.0</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-api-mockito</artifactId>
  <version>1.7.0</version>
  <scope>test</scope>
</dependency>
Şöyle yaparız.
@RunWith(PowerMockRunner.class)
// tell PowerMock about (a) the class you are going to test
@PrepareForTest({Math.class })
public class RandomerTest {

  @Test
  public void shouldAddUpDieRollsCorrectly() throws Exception {
    ...
  }
}
mockStatic metodu
Belirtilen sınıfın tüm static metodlarını mock'layabilmeyi sağlar. Açıklaması şöyle.
Hint: the moment you go mockStatic(SecurityUtils.class) all static methods on that class are "erased".
1. Test sınıfını başına @PrepareForTest anotasyonu ile byte code instrumentation'a tabi tutulacak static metoda sahip sınıf geçilir.

2. PowerMockito.mockStatic() metodu çağrılır. Parametre olarak Sınıfİsmi.class gibi bir şey geçeriz.

Daha sonra static metoda davranış belirtmek için
 - PowerMockito.when() + thenReturn
 - PowerMockito.when() + thenCallRealMethod
gibi bir örüntü kullanılır.

3. verifyStatic(times(1)) gibi bir metod ile static metodun çağrıldığı test edilir.

Örnek
Şöyle yaparız.
// prepare PowerMock for mocking statics on Math
PowerMockito.mockStatic(Math.class);
// establish an expectation for what Match.random() should return
PowerMockito.when(Math.random()).thenReturn(2.0);
Örnek
Şöyle yaparız.
PowerMockito.mockStatic(Class2.class);
PowerMockito.when(Class2.class, "write", Mockito.anyObject(), Mockito.anyString())
  .thenCallRealMethod();
  
Class2.write(...);
PowerMockito.verifyStatic(Class2.class, VerificationModeFactory.times(1));
mockStaticPartial metodu
Belirtilen sınıfın belirtilen static metodunu mock'layabilmeyi sağlar.

verifyStatic metodu
Static metodun kaç kere çağrıldığını doğrular.

Hiç yorum yok:

Yorum Gönder