diff --git a/agent.properties b/agent.properties index 6a51543..4993dcc 100644 --- a/agent.properties +++ b/agent.properties @@ -1,7 +1,7 @@ # mock文件路径 foxMockFilePath=/Users/yinjihuan/Documents/foxmockdata # foxmock的agent jar包路径,不填默认为当前文件夹 -foxMockAgentJarPath=fox-mock-agent/target/fox-mock-agent-3.0.jar +foxMockAgentJarPath=fox-mock-agent/target/fox-mock-agent-4.0.jar # mock方法白名单,如果文件夹中有多个方法会被全部mock,如果指定了此配置将只会mock这里指定的方法 #mockMethodWhiteList=com.cxytiandi.foxmock.example.UserService#getAge|com.cxytiandi.foxmock.example.UserService#getName2 # httpn mock数据地址,可以对接配置中心,必须是get请求 diff --git a/fox-mock-agent/pom.xml b/fox-mock-agent/pom.xml index d35616d..84aeae8 100644 --- a/fox-mock-agent/pom.xml +++ b/fox-mock-agent/pom.xml @@ -7,7 +7,7 @@ com.cxytiandi 1.0-SNAPSHOT - 3.0 + 4.0 4.0.0 fox-mock-agent diff --git a/fox-mock-agent/src/main/java/com/cxytiandi/foxmock/agent/transformer/MockClassFileTransformer.java b/fox-mock-agent/src/main/java/com/cxytiandi/foxmock/agent/transformer/MockClassFileTransformer.java index a2a91e9..71977ff 100644 --- a/fox-mock-agent/src/main/java/com/cxytiandi/foxmock/agent/transformer/MockClassFileTransformer.java +++ b/fox-mock-agent/src/main/java/com/cxytiandi/foxmock/agent/transformer/MockClassFileTransformer.java @@ -60,10 +60,16 @@ public byte[] transform(ClassLoader loader, String className, Class classBein if (Objects.nonNull(data)) { match = true; LOG.info(String.format("mock methods %s, mock data is %s", key, data)); - String mockCode = "if(true){" + - "return ($r)com.cxytiandi.foxmock.agent.utils.JsonUtils.parse(%s,%s,%s);" + - "}"; - method.insertBefore(String.format(mockCode, new Gson().toJson(data), "\""+className+"\"", "\""+methodName+"\"")); + if (data.startsWith("throw new")) { + String mockCode = "if(true){%s}"; + method.insertBefore(String.format(mockCode, data)); + } else { + String mockCode = "if(true){" + + "return ($r)com.cxytiandi.foxmock.agent.utils.JsonUtils.parse(%s,%s,%s);" + + "}"; + method.insertBefore(String.format(mockCode, new Gson().toJson(data), "\""+className+"\"", "\""+methodName+"\"")); + } + } } diff --git a/fox-mock-boot/src/main/java/com/cxytiandi/foxmock/boot/Bootstrap.java b/fox-mock-boot/src/main/java/com/cxytiandi/foxmock/boot/Bootstrap.java index 0d5a942..1ee4fcd 100644 --- a/fox-mock-boot/src/main/java/com/cxytiandi/foxmock/boot/Bootstrap.java +++ b/fox-mock-boot/src/main/java/com/cxytiandi/foxmock/boot/Bootstrap.java @@ -31,7 +31,7 @@ public static void main(String[] args) { String mockDataHttpUrl = config.getProperty("mockDataHttpUrl", ""); if (Objects.isNull(foxMockAgentJarPath)) { - foxMockAgentJarPath = PathUtils.getAgentPath() + File.separator + "fox-mock-agent-3.0.jar"; + foxMockAgentJarPath = PathUtils.getAgentPath() + File.separator + "fox-mock-agent-4.0.jar"; } VirtualMachine attach = VirtualMachine.attach(String.valueOf(getPid())); diff --git a/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/FoxMockApp.java b/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/FoxMockApp.java index 8784578..1a255f3 100644 --- a/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/FoxMockApp.java +++ b/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/FoxMockApp.java @@ -36,7 +36,11 @@ public static void main(String[] args) throws Exception { System.out.println(k + "\t" + v.getAddress()); }); } - + try { + userService.mockException(); + } catch (Exception e) { + e.printStackTrace(); + } System.out.println("----------------------------------------"); Thread.sleep(5000); } diff --git a/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/MockException.java b/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/MockException.java new file mode 100644 index 0000000..4736d96 --- /dev/null +++ b/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/MockException.java @@ -0,0 +1,16 @@ +package com.cxytiandi.foxmock.example; + +/** + * @作者 尹吉欢 + * @个人微信 jihuan900 + * @微信公众号 猿天地 + * @GitHub https://github.com/yinjihuan + * @作者介绍 http://cxytiandi.com/about + * @时间 2022-05-09 21:59 + */ +public class MockException extends RuntimeException { + + public MockException(String msg) { + super(msg); + } +} diff --git a/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/UserService.java b/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/UserService.java index d176490..2d6be96 100644 --- a/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/UserService.java +++ b/fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/UserService.java @@ -44,4 +44,7 @@ public Result getUserDetail() { return result; } + public void mockException() { + + } } diff --git a/fox-mock-example/src/main/resources/mockdata/com.cxytiandi.foxmock.example.UserService#mockException b/fox-mock-example/src/main/resources/mockdata/com.cxytiandi.foxmock.example.UserService#mockException new file mode 100644 index 0000000..f78fd5f --- /dev/null +++ b/fox-mock-example/src/main/resources/mockdata/com.cxytiandi.foxmock.example.UserService#mockException @@ -0,0 +1 @@ +throw new com.cxytiandi.foxmock.example.MockException("mock exception"); \ No newline at end of file