mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-24 03:10:14 +08:00
add demo for test scope
This commit is contained in:
parent
5f6cf3888c
commit
14c3d841c5
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user