mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-22 19:23:29 +08:00
split core package into core and processor
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.testable</groupId>
|
||||
<artifactId>testable-core</artifactId>
|
||||
<artifactId>testable-processor</artifactId>
|
||||
<version>${testable.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -12,4 +12,8 @@ public class BlackBox {
|
||||
return data;
|
||||
}
|
||||
|
||||
public static BlackBox secretBox() {
|
||||
return new BlackBox("secret");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,6 +47,13 @@ public class DemoService {
|
||||
return "anything".trim() + "__" + "anything".substring(1, 2) + "__" + "abc".startsWith("ab");
|
||||
}
|
||||
|
||||
/**
|
||||
* Target 6 - method with static method invoke
|
||||
*/
|
||||
public BlackBox getBox() {
|
||||
return BlackBox.secretBox();
|
||||
}
|
||||
|
||||
public String callerOne() {
|
||||
return callFromDifferentMethod();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.alibaba.testable.demo;
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||
import com.alibaba.testable.core.annotation.EnablePrivateAccess;
|
||||
import com.alibaba.testable.processor.annotation.EnablePrivateAccess;
|
||||
import com.alibaba.testable.core.annotation.TestableMock;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -38,6 +38,11 @@ class DemoServiceTest {
|
||||
return false;
|
||||
}
|
||||
|
||||
@TestableMock
|
||||
private BlackBox secretBox() {
|
||||
return new BlackBox("not_secret_box");
|
||||
}
|
||||
|
||||
@TestableMock
|
||||
private String callFromDifferentMethod(DemoService self) {
|
||||
if (TEST_CASE.equals("should_able_to_get_test_case_name")) {
|
||||
@@ -52,13 +57,13 @@ class DemoServiceTest {
|
||||
private DemoService demoService = new DemoService();
|
||||
|
||||
@Test
|
||||
void should_able_to_test_private_method() throws Exception {
|
||||
void should_able_to_mock_private_method() throws Exception {
|
||||
assertEquals("hello - 1", demoService.privateFunc("hello", 1));
|
||||
assertEquals("hello - 1", PrivateAccessor.invoke(demoService, "privateFunc", "hello", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_able_to_test_private_field() throws Exception {
|
||||
void should_able_to_mock_private_field() throws Exception {
|
||||
demoService.count = 2;
|
||||
assertEquals("4", demoService.privateFieldAccessFunc());
|
||||
PrivateAccessor.set(demoService, "count", 3);
|
||||
@@ -67,19 +72,19 @@ class DemoServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_able_to_test_new_object() throws Exception {
|
||||
void should_able_to_mock_new_object() throws Exception {
|
||||
assertEquals("mock_something", demoService.newFunc());
|
||||
verify("createBlackBox").times(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_able_to_test_member_method() throws Exception {
|
||||
void should_able_to_mock_member_method() throws Exception {
|
||||
assertEquals("{ \"res\": \"mock_hello\"}", demoService.outerFunc("hello"));
|
||||
verify("innerFunc").times(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_able_to_test_common_method() throws Exception {
|
||||
void should_able_to_mock_common_method() throws Exception {
|
||||
assertEquals("trim_string__sub_string__false", demoService.commonFunc());
|
||||
verify("trim").times(1);
|
||||
verify("sub").times(1);
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.testable</groupId>
|
||||
<artifactId>testable-core</artifactId>
|
||||
<artifactId>testable-processor</artifactId>
|
||||
<version>${testable.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -19,4 +19,16 @@ class BlackBox(private val data: String) {
|
||||
return data.startsWith(prefix)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun secretBox(): BlackBox {
|
||||
return BlackBox("secret")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object ColorBox {
|
||||
fun createBox(color: String): BlackBox {
|
||||
return BlackBox("${color}_Box")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.alibaba.testable.demo
|
||||
|
||||
import com.alibaba.testable.core.accessor.PrivateAccessor
|
||||
import com.alibaba.testable.core.annotation.EnablePrivateAccess
|
||||
import com.alibaba.testable.processor.annotation.EnablePrivateAccess
|
||||
import com.alibaba.testable.core.annotation.TestableMock
|
||||
import com.alibaba.testable.core.tool.TestableTool.*
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
@@ -42,31 +42,31 @@ internal class DemoServiceTest {
|
||||
private val demoService = DemoService()
|
||||
|
||||
@Test
|
||||
fun should_able_to_test_private_method() {
|
||||
fun should_able_to_mock_private_method() {
|
||||
assertEquals("hello - 1", PrivateAccessor.invoke(demoService, "privateFunc", "hello", 1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_able_to_test_private_field() {
|
||||
fun should_able_to_mock_private_field() {
|
||||
PrivateAccessor.set(demoService, "count", 3)
|
||||
assertEquals("5", demoService.privateFieldAccessFunc())
|
||||
assertEquals(5, PrivateAccessor.get(demoService, "count"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_able_to_test_new_object() {
|
||||
fun should_able_to_mock_new_object() {
|
||||
assertEquals("mock_something", demoService.newFunc())
|
||||
verify("createBlackBox").times(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_able_to_test_member_method() {
|
||||
fun should_able_to_mock_member_method() {
|
||||
assertEquals("{ \"res\": \"mock_hello\"}", demoService.outerFunc("hello"))
|
||||
verify("innerFunc").times(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_able_to_test_common_method() {
|
||||
fun should_able_to_mock_common_method() {
|
||||
assertEquals("trim_string__sub_string__false", demoService.commonFunc())
|
||||
verify("trim").times(1)
|
||||
verify("sub").times(1)
|
||||
|
||||
Reference in New Issue
Block a user