JUnit etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
JUnit etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

1 Aralık 2022 Perşembe

JUnit4 @ClassRule Anotasyonu

Giriş
Açıklaması şöyle. Yani JUnit 4'teki ClassRule gibidir.
Both JUnit4 and JUnit5 offer a solution to manage external resources as part of the test class definition. JUnit4 exposes this functionality via ClassRule, while JUnit5 provides a more powerful approach called Extension.

ClassRule exposes two functions: before and after, the former is used to set up the rule, and the latter is used to clean it up. If a test class contains a ClassRule definition, JUnit will invoke the before function before running any tests; after every test run, independently from their results, JUnit will invoke the function after. Extensions in JUnit5 follow the same pattern with an enriched semantic.
Birden fazla ClassRule varsa açıklaması şöyle
If there are multiple annotated ClassRules on a class, they will be applied in an order that depends on your JVM's implementation of the reflection API, which is undefined, in general. However, Rules defined by fields will always be applied before Rules defined by methods.
Örnek
Şöyle yaparız
@ClassRule
public static final SharedKafkaTestResource kafka = new SharedKafkaTestResource().withBrokers(1); private static final SharedSchemaRegistryTestResource schemaRegistry = new SharedSchemaRegistryTestResource() .withBootstrapServers(kafka::getKafkaConnectString); @ClassRule public static TestRule schemaRegistry() { return schemaRegistry; }




15 Şubat 2021 Pazartesi

JUnit5 @Execution Anotasyonu

Örnek
Şöyle yaparız
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
class PostTweetTextCT {
  ...
}

28 Mayıs 2019 Salı

JUnit4 @Test Anotasyonu - Kullanmayın

Giriş
Şu satırları dahil ederiz.
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Tanımlama 
Şöyle yaparız.
public class MyTest {

 
  @Before
  public void setUp () {
    ...  
  }

  @After
  public void cleanUp() {
    ...
  }

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

}
@Test Anotasyonu
Metodların başına yazılır. Şöyle yaparız.
@Test
public void testCompareAgents() {
  ...
}
exptected Alanı
Test kodunun belli bir exception tipini fırlatmasını bekler.
Örnek
Şöyle yaparız.
@Test(expected = IllegalStateException.class)
public void testKey() {
  ...
}


6 Aralık 2018 Perşembe

JUnit4 Assert Sınıfı

Giriş
Şu satırı dahil ederiz.
import static org.junit.Assert.*;
Eski kodlarda junit.framework.Assert import ediliyordu ancak bu sınıf artık kullanılmıyor.

assertEquals metodu - double + double
İmzası şöyle.
@Deprecated
public static void assertEquals(double expected, double actual)
Açıklaması şöyle.
Deprecated. Use assertEquals(double expected, double actual, double epsilon) instead
assertEquals metodu - int + int
Örnek
Şöyle yaparız.
int answer = ...;
Assert.assertEquals(answer, 42);
Örnek
Çoklu parametre kullanmak için şöyle yaparız.
assertEquals("Agent.getId", a1.getId(), a2.getId());
assertNotNull metodu
İstersek şu satırı dahil ederiz.
import static org.junit.Assert.assertNotNull;
Şöyle yaparız.
Assert.assertNotNull(someFunction(), "This should not be null");
assertNull metodu
Şöyle yaparız.
Assert.assertNull(someFunction());
assertTrue metodu
Şöyle yaparız.
assertTrue(foo == bar);

3 Nisan 2018 Salı

JUnit @RunWith Anotasyonu

Giriş
Açıklaması şöyle.
When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.
JUnit içindeki varsayılan Runner'dan farklı bir Runner kullanabilmeyi sağlar. Suite olarak koşmak için @Suite.SuiteClasses yazısına bakabilirsiniz.

JUnit 5 ile bu anotasyon yerine @ExtendWith kullanılır.

Örnek
Kendi Runner sınıfım için şöyle yaparız.
@RunWith(MyRunner.class)
public class FooTest {

}

2 Nisan 2018 Pazartesi

JUnit4 @Category Anotasyonu

Giriş
Tümleşim testlerini (Integration Test), birim testleri (Unit Test) ayırmak için kullanılır. Böylece testler farklı maven aşamlarında koşabilirler.

Örnek
Şöyle yaparız.
@Category(IntegrationTest.class)
public class MyIntegrationTest {
  ...
}

22 Mart 2018 Perşembe

JUnit4 @FixMethodOrder Anotasyonu

Giriş
Şu satırı dahil ederiz
import org.junit.FixMethodOrder;
Örnek
Şöyle yaparız.
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FooTest

  @Test
  public void testA() throws InterruptedException {
    ...
  }

  @Test
  public void testB() throws InterruptedException {
    ...
  }
}

27 Şubat 2018 Salı

JUnit @Parameterized.Parameters Anotasyonu

Giriş
Testin her parametre için ayrı ayrı koşmasını sağlar. Test sınıfının her parametreyi alan bir constructor metodu bulunur.

Örnek
Elimizde şöyle bir kod olsun
public class CalculatorImpl implements Calculator {
 
  @Override
  public double process(String expression) {
    ...
  }
}
Test verisi için şöyle yaparız. Birinci değer girdi string, ikinci değer sonuç, üçündü değer eper varsa exception'dır
@RunWith(Parameterized.class)
public class CalculatorTest {
  @Parameters(name = "{index}: CalculatorTest({0})={1}, throws {2}")
  public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][]{
      {"1 + 1", 2, null},
      {"1 + 1 + 1", 3, null},
      {"1–1", 0, null},
      {"1 * 1", 1, null},
      {"1 / 1", 1, null},
      {"( 1 + 1 )", 2, null},
      {"+", 0, new CalculatorException("Invalid expression: +")},
      {"1 1", 0, new CalculatorException("Invalid expression: 1 1")}
    });
  }

  private final String input;
  private final double expected;
  private final Exception exception;
  
  public CalculatorTest(String input, double expected, Exception exception) {
    this.input = input;
    this.expected = expected;
    this.exception = exception;
  }
...
}
Test için şöyle yaparız
@Test
public void testProcess() {
  Calculator c = new CalculatorImpl();
  try {
    double result = c.process(input);
    if (exception != null) {
      fail("should have thrown an exception: " + exception);
    }
    // shouldn't compare doubles without a delta, because FP math isn't accurate
    assertEquals(expected, result, 0.000001);
  } catch (Exception e) {
   if (exception == null) {
     fail("should not have thrown an exception, but threw " + e);
   }
   if (!exception.getClass().equals(e.getClass()) ||
!exception.getMessage().equals(e.getMessage())) {
     fail("expected exception " + exception + "; got exception " + e);
   }
  }
}
Örnek

Bu örnekte her test 1 ve 2 değerleri için koşar. Şöyle yaparız.
@RunWith(value = Parameterized.class)
public class MyTest {

  private int value;

  @Parameterized.Parameters
  public static List<Object[]> data() {
    return Arrays.asList(new Object[][]{
        {1},
        {2},
    });
  }

  public MyTest(int value) {
    this.value = value;
  }

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

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