Tartalomjegyzék

< Java tesztelés

Mockito

TestNG

pom.xml
    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-testng</artifactId>
            <version>0.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.10.2</version>
        </dependency>        
    </dependencies>
MockitoTest.java
import static org.mockito.Mockito.when;
 
import java.util.List;
 
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
 
@Listeners(MockitoTestNGListener.class)
public class MockitoTest {
 
  @Mock
  List<String> mockList;
 
  @Test
  void mock_list_returns_correct_elements() {
    when(mockList.get(0)).thenReturn("foo");    
    Assert.assertEquals(mockList.get(0), "foo");
  }
}

Változat 2

MockitoTest.java
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
import java.util.List;
 
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class MockitoTest {
 
  @Test
  void mock_list_returns_correct_elements() {
    @SuppressWarnings("rawtypes")
    List mockList = mock(List.class);
    when(mockList.get(0)).thenReturn("foo");    
    Assert.assertEquals(mockList.get(0), "foo");
  }
}