Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jmockit 与 junit5 整合来测试和mock私有与静态的方法与变量的问题 #22

Open
ddddage opened this issue Jul 26, 2019 · 9 comments

Comments

@ddddage
Copy link

ddddage commented Jul 26, 2019

网上好多例子都是关于junit4的但是在有junit5支持的版本以前jmockit已经把最省事的类Deencapsulation弃用了,我现在在学1.46版本,请问这个版本怎么mock私有的方法与变量?测试时要注意什么,希望大佬能帮助一下

@ddddage ddddage changed the title jmockit 与 junit5 整合来测试和mock私有与静态的方法与变量 jmockit 与 junit5 整合来测试和mock私有与静态的方法与变量的问题 Jul 26, 2019
@ddddage
Copy link
Author

ddddage commented Jul 26, 2019

那如果只用method那么没有返回值的方法怎么办?

@hzdavid
Copy link
Owner

hzdavid commented Jul 26, 2019

Please show your code.

@ddddage
Copy link
Author

ddddage commented Jul 26, 2019

public class Demo1 {

private static String File1 ;

private static String File2;

private void setValue(String val1, String val2){
xxxxxxxxxxxxx
File1=xxxxxxxx;
File2=xxxxxxxx;
}
}
我现在想在测试时覆盖setValue的逻辑并设置另外的File1和File2的值这个用Deencapsulation类我能做,但是改junit5就不会了,还有在用1.46的jmockit时,我也出现了JMockit didn't get initialized; please check the -javaagent JVM initialization parameter was used这个错误,确定用的jdk,请大佬解答一下

@hzdavid
Copy link
Owner

hzdavid commented Jul 26, 2019

JMockit至1.42后,就需要手工添加-javaagent参数指定JMockit的jar包,

见作者的release原文 :
Version 1.42 (Aug 26, 2018):
JMockit now requires the use of the "-javaagent" JVM initialization parameter. See Running tests with JMockit.
Dropped support for mock parameters in TestNG methods.
Issues closed: #515, #542.“
” (https://jmockit.github.io/changes.html#current)

这一点对开发很不友好。 如果1.36能满足你的需要,就优先使用1.36吧。

@hzdavid
Copy link
Owner

hzdavid commented Jul 26, 2019

@ddddage 另外, 按你的例子,用Deencapsulation 可以达到你的目的呀,与junit的版本有啥关系呢,

@ddddage
Copy link
Author

ddddage commented Jul 27, 2019

如果引用junit5,之前的版本会显示的英文意思为我要测试的目标类不支持filter过滤,因此执行了原来的全部逻辑,如果用junit4就不会报错 我不知道为什么,还有大佬,1.36的Deencapsulation好像不支持invoke了,最近的1.46连setfiled都没有了,如果我想在Junit5.42的版本中用jmokit,那个版本最好用呢@hzdavid 再次感谢大佬解答

@ddddage
Copy link
Author

ddddage commented Jul 27, 2019

-javaagent:${settings.localRepository}/org/jmockit/jmockit/1.19/jmockit-1.19.jar
大佬,我用这个配置和junit5-junit.jupiter 的5.32在maven里配置强行跑jmokit1.19,会报Invalid place to record expectations,用低版本的jmokit和junit5搭配这条路是不是行不通,只能用高版本的jmokit和junit5搭配?

@ddddage
Copy link
Author

ddddage commented Jul 28, 2019

大佬我的问题已经由@tested和Mockup方法解决,我还有个问题,mockup里能设置私有变量吗,如果不能该用哪个方法设?我看你的例子里mockup里只有方法设置,没有属性设置啊

@hzdavid
Copy link
Owner

hzdavid commented Aug 1, 2019

MockUp ,@mock 是针对方法的Mock。 对私有变量无效。

如果要Mock属性, 直接用Deencapsulation这个,通过反射来修改即可。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants