Skip to content

Commit

Permalink
feat:支持mock指定的异常
Browse files Browse the repository at this point in the history
  • Loading branch information
yinjihuan committed May 9, 2022
1 parent 4edf635 commit 0c6301f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion agent.properties
Original file line number Diff line number Diff line change
@@ -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请求
Expand Down
2 changes: 1 addition & 1 deletion fox-mock-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.cxytiandi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<version>3.0</version>
<version>4.0</version>
<modelVersion>4.0.0</modelVersion>

<artifactId>fox-mock-agent</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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+"\""));
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ public Result<UserDetail> getUserDetail() {
return result;
}

public void mockException() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new com.cxytiandi.foxmock.example.MockException("mock exception");

0 comments on commit 0c6301f

Please sign in to comment.