2021-08-02

This commit is contained in:
2021-08-02 19:08:03 +09:00
parent 25d0987611
commit 365f7e489b
16 changed files with 179 additions and 17 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@
/buildSrc/build/ /buildSrc/build/
/.idea/ /.idea/
/build/ /build/
/**/build/**

View File

@@ -1,10 +0,0 @@
plugins {
id("elex-application")
}
application{
mainClass.set("com.elex_project.sample.Application")
}
dependencies {
}

View File

@@ -1 +0,0 @@
package com.elex_project.sample;

View File

@@ -29,12 +29,11 @@ configurations {
} }
tasks.jar { tasks.jar {
manifest { // todo manifest {
attributes(mapOf( attributes(mapOf(
"Implementation-Title" to project.name, "Implementation-Title" to project.name,
"Implementation-Version" to project.version, "Implementation-Version" to project.version,
"Implementation-Vendor" to "ELEX co.,pte.", "Implementation-Vendor" to "ELEX co.,pte."
"Automatic-Module-Name" to "com.elex_project.${project.name}"
)) ))
} }
} }
@@ -61,7 +60,7 @@ tasks.javadoc {
} }
dependencies { dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar")))) //implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("org.slf4j:slf4j-api:1.7.30") implementation("org.slf4j:slf4j-api:1.7.30")
implementation("org.jetbrains:annotations:21.0.1") implementation("org.jetbrains:annotations:21.0.1")

View File

@@ -0,0 +1,7 @@
plugins {
id ("elex-base")
}
dependencies {
}

View File

@@ -0,0 +1 @@
package kr.pe.elex.examples;

View File

@@ -1 +0,0 @@
package com.elex_project.sample;

5
mockito/README.md Normal file
View File

@@ -0,0 +1,5 @@
# Mockito
## mock()
## when()

8
mockito/build.gradle.kts Normal file
View 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")
}

View 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;
}

View File

@@ -0,0 +1 @@
package kr.pe.elex.examples;

View 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());
}
}

View File

@@ -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);
}
}

View 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());
}
}

View File

@@ -1,2 +1,2 @@
rootProject.name = "java-examples" rootProject.name = "java-examples"
include("lib", "app") include("java-web-token", "mockito")