Reset Mockito Mock Object


Environment and Prerequisite

  • Java
  • Mockito


org.mockito.Mockito.reset

reset() Method

    ...
    @Mock
    private UserRepository userRepository;
    ...
    reset(userRepository);
    ...


Reference

ReflectionTestUtils를 사용해 private 변수나 메소드(함수)를 테스트하는 방법을 알아보자


환경

  • Java
  • Spring


ReflectionTestUtils

대상 Class

@Component
@Getter
public class ReflectionTestUtilsComponent {

    private int privateValue;

    private int privateMethod(int a, int b) {
        privateValue += a;
        privateValue += b;
        return privateValue;
    }
}


private 변수 테스트

형태

static void	setField(Class<?> targetClass, String name, Object value)

static void	setField(Class<?> targetClass, String name, Object value, Class<?> type)

static void	setField(Object targetObject, Class<?> targetClass, String name, Object value, Class<?> type)

static void	setField(Object targetObject, String name, Object value)

static void	setField(Object targetObject, String name, Object value, Class<?> type)

사용 예시

@Test
public void testPrivateValue() {
    ReflectionTestUtils.setField(reflectionTestUtilsComponent, "privateValue", 100);
    // ReflectionTestUtils.setField(대상 객체, "변수명", 원하는 값);

    Assertions.assertEquals(100, reflectionTestUtilsComponent.getPrivateValue());
}


private 메소드(함수) 테스트

형태

static <T> T	invokeMethod(Class<?> targetClass, String name, Object... args)

static <T> T	invokeMethod(Object targetObject, Class<?> targetClass, String name, Object... args)

static <T> T	invokeMethod(Object target, String name, Object... args)

사용 예시

@Test
public void testPrivateMethod() {
    ReflectionTestUtils.setField(reflectionTestUtilsComponent, "privateValue", 100);
    // ReflectionTestUtils.setField(대상 객체, "변수명", 원하는 값);

    Assertions.assertEquals(103, (Integer) ReflectionTestUtils.invokeMethod(reflectionTestUtilsComponent, "privateMethod", 1, 2));
    // ReflectionTestUtils.invokeMethod(대상 객체, "메소드(함수)명", 인자);

    Assertions.assertEquals(103, reflectionTestUtilsComponent.getPrivateValue());
}


Github 예시


참고자료

Test private variable or method(function) using ReflectionTestUtils


Environment and Prerequisite

  • Java
  • Spring


ReflectionTestUtils

Target Class

@Component
@Getter
public class ReflectionTestUtilsComponent {

    private int privateValue;

    private int privateMethod(int a, int b) {
        privateValue += a;
        privateValue += b;
        return privateValue;
    }
}


private Variable Test

Format

static void	setField(Class<?> targetClass, String name, Object value)

static void	setField(Class<?> targetClass, String name, Object value, Class<?> type)

static void	setField(Object targetObject, Class<?> targetClass, String name, Object value, Class<?> type)

static void	setField(Object targetObject, String name, Object value)

static void	setField(Object targetObject, String name, Object value, Class<?> type)

Usage Example

@Test
public void testPrivateValue() {
    ReflectionTestUtils.setField(reflectionTestUtilsComponent, "privateValue", 100);
    // ReflectionTestUtils.setField(target object, "variable name", target value);

    Assertions.assertEquals(100, reflectionTestUtilsComponent.getPrivateValue());
}


private Method(Function) Test

Format

static <T> T	invokeMethod(Class<?> targetClass, String name, Object... args)

static <T> T	invokeMethod(Object targetObject, Class<?> targetClass, String name, Object... args)

static <T> T	invokeMethod(Object target, String name, Object... args)

Usage Example

@Test
public void testPrivateMethod() {
    ReflectionTestUtils.setField(reflectionTestUtilsComponent, "privateValue", 100);
    // ReflectionTestUtils.setField(target object, "variable name", target value);

    Assertions.assertEquals(103, (Integer) ReflectionTestUtils.invokeMethod(reflectionTestUtilsComponent, "privateMethod", 1, 2));
    // ReflectionTestUtils.invokeMethod(target object, "method(function) name", arguments);

    Assertions.assertEquals(103, reflectionTestUtilsComponent.getPrivateValue());
}


Github Example


Reference

gradle build 또는 test 후에 테스트 결과를 표시해보자


환경

  • Gradle
  • 테스트 환경은 구축되어 있다고 가정


방법

build.gradle의 test 부분에 아래처럼 testLogging 부분을 추가

test {
    useJUnitPlatform()
    testLogging {
        afterSuite { testDescriptor, testResult ->
            if (testDescriptor.parent == null) {
                println "Results: ${testResult.resultType} (${testResult.testCount} tests, ${testResult.successfulTestCount} successes, ${testResult.failedTestCount} failures, ${testResult.skippedTestCount} skipped)"
            }
        }
    }
}
  • afterSuite: Test Suite가 실행되고나서 실행되는 부분
  • testDescriptor: 테스트에 대한 정보를 가지고 있는 인터페이스
  • testResult: 테스트 결과에 대한 정보를 가지고 있는 인터페이스
  • testDescriptor.parent == null: TestDescriptor의 부모가 null일때 실행하며 이는 테스트가 다 끝났음을 의미

결과 예시

Taewoo-Macbook-Air-M1-2020:SpringBatchTestExample taewoo$ ./gradlew build
...
> Task :test
...
Results: SUCCESS (6 tests, 6 successes, 0 failures, 0 skipped)
...
BUILD SUCCESSFUL in 10s
6 actionable tasks: 3 executed, 3 up-to-date


참고자료

Show test results after gradle build or test


Environment and Prerequisite

  • Gradle
  • Assume that test environment is set


Method

Add below testLogging part to test in build.gradle

test {
    useJUnitPlatform()
    testLogging {
        afterSuite { testDescriptor, testResult ->
            if (testDescriptor.parent == null) {
                println "Results: ${testResult.resultType} (${testResult.testCount} tests, ${testResult.successfulTestCount} successes, ${testResult.failedTestCount} failures, ${testResult.skippedTestCount} skipped)"
            }
        }
    }
}
  • afterSuite: Run after test suite is done
  • testDescriptor: Describes a test
  • testResult: Describes a test result
  • testDescriptor.parent == null: Run when TestDescriptor’s parent is null which means end of the tests

Result Example

Taewoo-Macbook-Air-M1-2020:SpringBatchTestExample taewoo$ ./gradlew build
...
> Task :test
...
Results: SUCCESS (6 tests, 6 successes, 0 failures, 0 skipped)
...
BUILD SUCCESSFUL in 10s
6 actionable tasks: 3 executed, 3 up-to-date


Reference