From 14c3d841c5376150ac90b096c5a49982871c2d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Wed, 28 Apr 2021 23:52:52 +0800 Subject: [PATCH] add demo for test scope --- .../demo/association/CookerService.java | 33 +++++++++++++++++++ .../demo/association/SellerService.java | 18 ++++++++++ .../demo/association/CookerServiceMock.java | 27 +++++++++++++++ .../demo/association/SellerServiceTest.java | 21 ++++++++++++ .../com/alibaba/demo/basic/DemoMockTest.java | 7 +++- 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 demo/java-demo/src/main/java/com/alibaba/demo/association/CookerService.java create mode 100644 demo/java-demo/src/main/java/com/alibaba/demo/association/SellerService.java create mode 100644 demo/java-demo/src/test/java/com/alibaba/demo/association/CookerServiceMock.java create mode 100644 demo/java-demo/src/test/java/com/alibaba/demo/association/SellerServiceTest.java diff --git a/demo/java-demo/src/main/java/com/alibaba/demo/association/CookerService.java b/demo/java-demo/src/main/java/com/alibaba/demo/association/CookerService.java new file mode 100644 index 0000000..6829824 --- /dev/null +++ b/demo/java-demo/src/main/java/com/alibaba/demo/association/CookerService.java @@ -0,0 +1,33 @@ +package com.alibaba.demo.association; + +/** + * 目标类,此类中的一些调用将会被Mock掉 + * Target class, some invocations inside this class will be mocked + */ +public class CookerService { + + private static String hireSandwichCooker() { + return "Sandwich-Cooker"; + } + + private static String hireHamburgerCooker() { + return "Hamburger-Cooker"; + } + + private String cookSandwich() { + return "Cooked-Sandwich"; + } + + private String cookHamburger() { + return "Cooked-Hamburger"; + } + + public String prepareSandwich() { + return hireSandwichCooker() + " & " + cookSandwich(); + } + + public String prepareHamburger() { + return hireHamburgerCooker() + " & " + cookHamburger(); + } + +} diff --git a/demo/java-demo/src/main/java/com/alibaba/demo/association/SellerService.java b/demo/java-demo/src/main/java/com/alibaba/demo/association/SellerService.java new file mode 100644 index 0000000..b03cf8f --- /dev/null +++ b/demo/java-demo/src/main/java/com/alibaba/demo/association/SellerService.java @@ -0,0 +1,18 @@ +package com.alibaba.demo.association; + +/** + * 被测类,会访问`CookerService`里的方法 + * Class to be tested, which will access methods in TargetService class + */ +public class SellerService { + + private CookerService cookerService = new CookerService(); + + public String sellSandwich() { + return cookerService.prepareSandwich(); + } + + public String sellHamburger() { + return cookerService.prepareHamburger(); + } +} diff --git a/demo/java-demo/src/test/java/com/alibaba/demo/association/CookerServiceMock.java b/demo/java-demo/src/test/java/com/alibaba/demo/association/CookerServiceMock.java new file mode 100644 index 0000000..772d32a --- /dev/null +++ b/demo/java-demo/src/test/java/com/alibaba/demo/association/CookerServiceMock.java @@ -0,0 +1,27 @@ +package com.alibaba.demo.association; + +import com.alibaba.testable.core.annotation.MockMethod; +import com.alibaba.testable.core.model.MockScope; + +class CookerServiceMock { + + @MockMethod(targetClass = CookerService.class) + public static String hireSandwichCooker() { + return "Fake-Sandwich-Cooker"; + } + + @MockMethod(targetClass = CookerService.class, scope = MockScope.ASSOCIATED) + public static String hireHamburgerCooker() { + return "Fake-Hamburger-Cooker"; + } + + @MockMethod(targetClass = CookerService.class) + private String cookSandwich() { + return "Faked-Sandwich"; + } + + @MockMethod(targetClass = CookerService.class, scope = MockScope.ASSOCIATED) + private String cookHamburger() { + return "Faked-Hamburger"; + } +} diff --git a/demo/java-demo/src/test/java/com/alibaba/demo/association/SellerServiceTest.java b/demo/java-demo/src/test/java/com/alibaba/demo/association/SellerServiceTest.java new file mode 100644 index 0000000..ae3c481 --- /dev/null +++ b/demo/java-demo/src/test/java/com/alibaba/demo/association/SellerServiceTest.java @@ -0,0 +1,21 @@ +package com.alibaba.demo.association; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class SellerServiceTest { + + private SellerService sellerService = new SellerService(); + + @Test + void should_sell_sandwich() { + assertEquals("Fake-Sandwich-Cooker & Faked-Sandwich", sellerService.sellSandwich()); + } + + @Test + void should_sell_hamburger() { + assertEquals("Hamburger-Cooker & Cooked-Hamburger", sellerService.sellHamburger()); + } + +} diff --git a/demo/java-demo/src/test/java/com/alibaba/demo/basic/DemoMockTest.java b/demo/java-demo/src/test/java/com/alibaba/demo/basic/DemoMockTest.java index e587501..b22228d 100644 --- a/demo/java-demo/src/test/java/com/alibaba/demo/basic/DemoMockTest.java +++ b/demo/java-demo/src/test/java/com/alibaba/demo/basic/DemoMockTest.java @@ -2,7 +2,9 @@ package com.alibaba.demo.basic; import com.alibaba.demo.basic.model.mock.BlackBox; import com.alibaba.testable.core.annotation.MockConstructor; +import com.alibaba.testable.core.annotation.MockDiagnose; import com.alibaba.testable.core.annotation.MockMethod; +import com.alibaba.testable.core.model.LogLevel; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -21,12 +23,15 @@ class DemoMockTest { private DemoMock demoMock = new DemoMock(); - public static class Mock { + @MockDiagnose(LogLevel.VERBOSE) + public static class Mock extends BaseMock { @MockConstructor private BlackBox createBlackBox(String text) { return new BlackBox("mock_" + text); } + } + public static class BaseMock { @MockMethod(targetClass = DemoMock.class) private String innerFunc(String text) { return "mock_" + text;