jmockit

Can JMockit mock constructors with any argument?

淺唱寂寞╮ 提交于 2021-01-29 13:46:03
问题 I am replacing PowerMock with JMockit in old unit testing case. Below is the PowerMock sample code which mock the File.class constructors with any argument . Can JMockit mock constructors with any argument like it ? The situation is this:myFile is a mock. And I want to simulate returning myFile when calling any constructor in the File class..So What is the code like. // PowerMock whenNew(File.class).withAnyArguments().thenReturn(myfile); // JMockit new Expectations() {{ new File(anyString);

initialize jmockit without -javaagent

隐身守侯 提交于 2021-01-28 05:05:06
问题 I use jmockit and junit to write unit test for a module and run it in a STB. I use jmockit-1.7 because the STB only have java 5. I got this error when run unit test: java.lang.IllegalStateException: Jmockit has not been initialized. Check that your Java 5 VM has been started with -javaagent:jmockit.jar command line option but my STB use siege java VM, so it doesn't have -javaagent command line option I have google, and found a solution from Running tests with JMockit @BeforeClass public

initialize jmockit without -javaagent

ぐ巨炮叔叔 提交于 2021-01-28 05:04:30
问题 I use jmockit and junit to write unit test for a module and run it in a STB. I use jmockit-1.7 because the STB only have java 5. I got this error when run unit test: java.lang.IllegalStateException: Jmockit has not been initialized. Check that your Java 5 VM has been started with -javaagent:jmockit.jar command line option but my STB use siege java VM, so it doesn't have -javaagent command line option I have google, and found a solution from Running tests with JMockit @BeforeClass public

Mocking static method that is called multiple times

一曲冷凌霜 提交于 2021-01-27 14:52:58
问题 I have a static method, that is used in multiple places, mostly in static initialization block. It takes a Class object as parameter, and returns the class's instance. I want to mock this static method only when particular Class object is used as parameter. But when the method is called from other places, with different Class objects, it returns null. How can we have the static method execute actual implementation in case of parameters other than the mocked one? class ABC{ void someMethod(){

Mocking static method that is called multiple times

落花浮王杯 提交于 2021-01-27 14:35:38
问题 I have a static method, that is used in multiple places, mostly in static initialization block. It takes a Class object as parameter, and returns the class's instance. I want to mock this static method only when particular Class object is used as parameter. But when the method is called from other places, with different Class objects, it returns null. How can we have the static method execute actual implementation in case of parameters other than the mocked one? class ABC{ void someMethod(){

How to mock @PrePersist method?

故事扮演 提交于 2021-01-27 11:50:23
问题 How do I mock a @PrePersist method, e.g. preInit(), of an entity that I instantiate? I'm using TestNG. EasyMock is prefered. @Test(enabled = true) public void testCreateOrder() { // Instantiating the new mini order will automatically invoke the pre-persist method, which needs to be mocked/overwritten! MiniOrder order = new MiniOrder(); order.setDate(new Date()); order.setCustomerId(32423423); } The MiniOrder.java is an entity that has a pre-persist method. Again, the one I like to mock

JMockit工具总结

夙愿已清 提交于 2020-08-07 21:07:08
JMockit is a Java toolkit for automated developer testing.It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking externalAPIs; JUnit (4 and 5) and TestNG test runners are supported.It also contains an advanced code coverage tool. Jmockit是一个Java工具,用于开发者做自动测试。Jmockit包含了被创建对象的创建API,主要是为了模拟外部依赖和外部API,支持Junit4、5和TestNG,并且Jmockit还包含了优秀的代码覆盖工具。 JMockit简介 JMockit是一个用于在测试阶段模拟Java对象的Java框架,支持Junit4和TestNG。JMockit使用Java的instrumentation API在运行时修改类class文件的字节码来动态的改变类的行为。JMockit比较强的地方在于它的可表达性和开箱即用的能力,它支持模拟静态方法和私有方法(不推荐mock私有方法)。 Jmockit具有超强的表达能力

2020,你需掌握go 单元测试进阶篇

大憨熊 提交于 2020-08-06 14:00:17
本文说明go语言自带的测试框架未提供或者未方便地提供的测试方案,主要是用于解决写单元测试中比较头痛的依赖问题。也就是伪造模式,经典的伪造模式有桩对象(stub),模拟对象(mock)和伪对象(fake)。比较幸运的是,社区有丰富的第三方测试框架支持支持。下面就对笔者亲身试用并实践到项目中的几个框架做介绍: 1.gomock gomock模拟对象的方式是让用户声明一个接口,然后使用gomock提供的mockgen工具生成mock对象代码。要模拟(mock)被测试代码的依赖对象时候,即可使用mock出来的对象来模拟和记录依赖对象的各种行为:比如最常用的返回值,调用次数等等。文字叙述有点抽象,直接上代码: dick.go中DickFunc依赖外部对象OutterObj,本示例就是说明如何使用gomock框架控制所依赖的对象。 func DickFunc( outterObj MockInterface,para int )(result int ){ fmt.Println( " This init DickFunc " ) fmt.Println( " call outter.func: " ) return outterObj.OutterFunc(para) } mockgen工具命令是: mockgen -source {source_file}.go -destination