2021-08-02
This commit is contained in:
5
mockito/README.md
Normal file
5
mockito/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Mockito
|
||||
|
||||
## mock()
|
||||
|
||||
## when()
|
||||
8
mockito/build.gradle.kts
Normal file
8
mockito/build.gradle.kts
Normal file
@@ -0,0 +1,8 @@
|
||||
plugins {
|
||||
id("elex-java")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// https://mvnrepository.com/artifact/org.mockito/mockito-core
|
||||
testImplementation("org.mockito:mockito-core:3.11.2")
|
||||
}
|
||||
12
mockito/src/main/java/kr/pe/elex/examples/Person.java
Normal file
12
mockito/src/main/java/kr/pe/elex/examples/Person.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
private String email;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package kr.pe.elex.examples;
|
||||
62
mockito/src/test/java/kr/pe/elex/examples/MockTest.java
Normal file
62
mockito/src/test/java/kr/pe/elex/examples/MockTest.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class MockTest {
|
||||
@Test
|
||||
void mock() {
|
||||
Person jane = Mockito.mock(Person.class);
|
||||
jane.setName("Jane");
|
||||
|
||||
assertNull(jane.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mock2() {
|
||||
Person jane = Mockito.mock(Person.class);
|
||||
when(jane.getName()).thenReturn("Jane");
|
||||
when(jane.getAge()).thenReturn(22);
|
||||
|
||||
assertEquals("Jane", jane.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mock3() {
|
||||
ArrayList list = Mockito.mock(ArrayList.class);
|
||||
|
||||
when(list.get(anyInt())).thenReturn("Apple");
|
||||
|
||||
assertEquals("Apple", list.get(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mock4() {
|
||||
Person jane = Mockito.mock(Person.class);
|
||||
when(jane.getName()).thenReturn("Jane");
|
||||
when(jane.getAge()).thenReturn(22);
|
||||
|
||||
assertEquals("Jane", jane.getName());
|
||||
assertEquals(22, jane.getAge());
|
||||
|
||||
verify(jane, times(1)).getAge();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void spy1() {
|
||||
Person jane = Mockito.spy(new Person("Jane",22,"jane@example.com"));
|
||||
when(jane.getName()).thenReturn("JANE");
|
||||
|
||||
assertEquals("JANE", jane.getName());
|
||||
assertEquals(22, jane.getAge());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class PersonListTest {
|
||||
|
||||
private static List<Person> persons;
|
||||
|
||||
@BeforeAll
|
||||
static void beforeEach() {
|
||||
persons = Mockito.mock(List.class);
|
||||
persons.add(new Person("Charlie",11, "charlie@example.com"));
|
||||
persons.add(new Person("Steve",34, "steve@example.com"));
|
||||
persons.add(new Person("Jane",22, "jane@example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
System.out.println(persons.get(0));
|
||||
|
||||
verify(persons).get(0);
|
||||
}
|
||||
}
|
||||
47
mockito/src/test/java/kr/pe/elex/examples/PersonTest.java
Normal file
47
mockito/src/test/java/kr/pe/elex/examples/PersonTest.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class PersonTest {
|
||||
|
||||
private Person charlie, steve;
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
charlie = Mockito.mock(Person.class);
|
||||
when(charlie.getName()).thenReturn("Charlie");
|
||||
when(charlie.getAge()).thenReturn(11);
|
||||
|
||||
doThrow(new IllegalArgumentException())
|
||||
.when(charlie).setName(eq("Brown"));
|
||||
|
||||
steve = Mockito.spy(new Person("Steve",34,"steve@example.com"));
|
||||
when(steve.getName()).thenReturn("STEVE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test1() {
|
||||
assertEquals("Charlie", charlie.getName());
|
||||
assertEquals(11, charlie.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test2() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> charlie.setName("Brown"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test3() {
|
||||
assertEquals("STEVE", steve.getName());
|
||||
assertEquals(34, steve.getAge());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user