Reset Mockito Mock Object
Environment and Prerequisite
- Java
- Mockito
org.mockito.Mockito.reset
reset() Method
...
@Mock
private UserRepository userRepository;
...
reset(userRepository);
...
Reset Mockito Mock Object
...
@Mock
private UserRepository userRepository;
...
reset(userRepository);
...
ReflectionTestUtils를 사용해 private 변수나 메소드(함수)를 테스트하는 방법을 알아보자
@Component
@Getter
public class ReflectionTestUtilsComponent {
private int privateValue;
private int privateMethod(int a, int b) {
privateValue += a;
privateValue += b;
return privateValue;
}
}
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());
}
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());
}
Test private variable or method(function) using ReflectionTestUtils
@Component
@Getter
public class ReflectionTestUtilsComponent {
private int privateValue;
private int privateMethod(int a, int b) {
privateValue += a;
privateValue += b;
return privateValue;
}
}
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(target object, "variable name", target value);
Assertions.assertEquals(100, reflectionTestUtilsComponent.getPrivateValue());
}
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(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());
}
gradle build 또는 test 후에 테스트 결과를 표시해보자
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
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 donetestDescriptor
: Describes a testtestResult
: Describes a test resulttestDescriptor.parent == null
: Run when TestDescriptor’s parent is null which means end of the testsTaewoo-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