jmockit

Using jmockit expectations with matchers and primitive types

I'm using jmockit for unit testing (with TestNG), and I'm having trouble using the Expectations class to mock out a method that takes a primitive type (boolean) as a parameter, using a matcher. Here's some sample code that illustrates the problem. /******************************************************/ import static org.hamcrest.Match...

How to mock the InitialContext class with jmockit?

I'm using jmockit with my tests and with one class I wish to test, uses InitialContext directly. So I have the following: public class MyClass { public void myMethod() { InitialContext ic = new InitialContext(); javax.mail.Session mailSession = ic.lookup("my.mail.session"); // rest of method follows..... } In my test ...

How do I unit test a Java method which uses ProcessBuilder and Process?

I have a Java method which starts up a Process with ProcessBuilder, and pipes its output into a byte array, and then returns its byte array when the process is finished. Pseudo-code: ProcessBuilder b = new ProcessBuilder("my.exe") Process p = b.start(); ... // get output from process, close process What would be the best way to go ab...

NoClassDefFoundError when trying to use JMockit Coverage

I am trying to use JMockit's code coverage abilities. Using the JVM parameter -javaagent:jmockit.jar=coverage=.*MyClass.java:html:: I am able to run my tests (jmockit.jar and coverage.jar are on the classpath), unfortunately my log file says: Loaded external tool: mockit.coverage.CodeCoverage=.*MyClass.java:html:: Loaded external too...

Is there a way to use a custom JUnit Test Runner when running tests in Eclipse?

I want to use JMockit's incremental test runner instead of the standard JUnit test runners from within Eclipse. Is there a way to do this? Edit Received the following answer to my email to the JMockit dev: You would need to start the JVM with a command line such as "-javaagent:jmockit.jar=incjunit4", where "incjunit4" specifie...

Mocking non-public static methods in abstract classes with JMockit?

I have the following class: public abstract class AbstractParent { static String method() { return "OriginalOutput"; } } I want to mock this method. I decide to use JMockit. So I create a mock class: public class MockParent { static String method() { return "MOCK"; } } And my test code looks like thi...

Getting ClassFormatError with EMMA?

I'm trying to generate code coverage reports with EMMA using tests of which some use JMockit as a mocking framework. For the most part, it works, but a few of my tests crash with a ClassFormatError, like so: java.lang.ClassFormatError at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method) at sun.instrument.Instrum...

Is there a way in JMockit to call the original method from a mocked method?

In my mock class, I'm mocking method foo(). For some test cases, I want the mock implementation of foo() to return a special value. For other test cases, I want to use the real implementation of foo(). I have a boolean defined in my mock class so that I can determine in the mock method whether I want to return the special value, or us...

mocking superclass protected variable using jmockit

Hi, I couldnt able to mock the protected varibale defined in the superclass.i could able to mock the protected method in superclass but couldnt to mock the protected variable in to the subclass ,wherein am writing the testcase for subclass,Please if anybody out there has any soluton for it .please reply. Thanks Shashi ...

How do I mock a method with generic objects in JMockit?

This question is self explanatory if you know how to use JMockit: How do I mock a method that has generics on it? I want to mock this method: public T save(T entity) but it always throws an exception like this: mockit.RealMethodNotFoundForMockException: Corresponding real methods not found for the following mocks: Object save(Object) ...

Mocking Toolkit using JMockit Expectations

I am trying to mock java.awt.Toolkit.beep() using JMockit Expectations. I have the following code in my test case: new Expectations() { Toolkit mock; { mock.beep(); } }.endRecording(); When I run the test case (JUnit 4), I get the following exception at the "new Expectations" line: java.lang.ClassFormatError: Cod...

Debugger won't work with JMockit

Hopefully an easy question here for someone..... I'm using RAD 7.5.2, and am writing Junit tests. I was writing them just fine with JUnit 3, and then I wanted to mock up some function calls. So I loaded up jmockit 0.9.7 and Junit 4.6. I also include -javaagent:jmockit.jar as a VM argument. When I "Run As Junit Test", it goes along w...

Jmockit abstract class instance

I want to mock a method in abstract class say 'A' and also need to pass and instance of type A to the methods I am unit testing. Is there a way to create an instance using Jmockit like Mockit.newemptyProxy How do I solve this scenario ...

ClassFormatError using JMockit with EMMA

I am trying to use EMMA to measure coverage of some JUnit tests that use JMockit. But when I try to run the JMockit tests after instrumenting with EMMA, about a quarter of the tests fail with the following error: com.logstorage.engine.sensor.SensorManagerTest.setUpBeforeClass(SensorManagerTest.java:98) java.lang.ClassFormatError ...

J Mock it for Non Static Classes

I have a class under test which says some thing like ClassA a = new ClassA(); In this case, i want it to instantiate ClassB which is a mock of ClassA. I am using Mockit.redefine and restore .. During execution of a.doSomething() I get an error saying java.lang.NoSuchMethodError: ClassB: method ()V not found.. The method signature is ma...

Is there a neater way of testing calls to mocked methods for each item in a list

This is an example of a pattern I've encountered a lot recently. I have a method to be tested that takes a List and may invoke some other method(s) for each item in the list. To test this I define an Iterator with the expected call parameters and a loop in the JMock expectations to check the call is made against each item of the iterator...

JMockit Hibernate Emulation

I was wondering if anyone tried using JMockit Hibernate Emulation? Jmockit documentation says that when Hibernate Emulation tests are run, they won't use the O/R mapping information. So, this means it doesn't test O/R mappings, HQL query strings, Native queries, etc. Then what really are the benefits of Hibernate Emulation? One can jus...

How to JMockIt System.getenv(String)?

What I have right now I have a 3rd party singleton instance that my class under test relies on and that singleton is using System.getenv(String) in its constructor. Is it possible to mock this call? I tried this JMockIt Example new Expectations() { System mockedSystem; { System.getenv( "FISSK_CONF...

How to mock native methods using jMockit

I'm trying to use jMockit to stub out a call to netscape.javascript.JSObject which is unfortunately an abstract class with a few native methods in it, both of which don't play well with jMockit. Is there any way for me to stub this out, either with jMockit, or using plain java (I'd prefer not having to introduce a new library if possible...

Is it possible to insert module dependency to the beginnig of the classpath in some maven profile?

I'm trying to move out jmockit-coverage-0.994.jar dependency from the project to some profile not active by default, but cannot insert it to the beginning of result classpath from the profile dependencies. ...