jmockit

Why should I not be mocking File or Path

狂风中的少年 提交于 2019-12-24 06:38:56
问题 I've been struggling with understanding the minefield that is classes supported by the jmockit Mocking API. I found what I thought was a bug with File, but the issue was simply closed saying "Don't mock a File or Path" which no explanation. Can someone on here help me understand why certain classes should not be mocked and how I'm supposed to work around that. I'm trying to contribute to the library with bug reports, but each one I file just leaves me more confused. Pardon my ignorance, but

Log4j Logger.getLogger(Class) throws NPE when running with jMockit and Cobertura

a 夏天 提交于 2019-12-23 20:04:34
问题 I have found a strange interaction between cobertura-maven-plugin 2.6 and jmockit 1.8. A particular pattern in our production code has a class with a lot of static methods that effectively wraps a different class that acts like a singleton. Writing unit tests for these classes went fine until I tried to run coverage reports with cobertura, when this error cropped up: java.lang.ExceptionInInitializerError at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at sun.reflect

Combined test coverage report with jMockit

家住魔仙堡 提交于 2019-12-23 15:41:02
问题 I am using jmockit with Ant. For each test file run, an index.html file gets created/over-written in the coverage report folder. For multiple test files, this index.html gets over-written. I am looking for a combined report for all files. what should be done? I have read about using .ser files but I do not know how to create and then use them? 回答1: Have a look here. The trick is to set -Djmockit-coverage-output=merge (or serial - read up on the differences in the link above). Cheers, 来源:

Jmockit: Can't mock method toString of net.android.Uri class

坚强是说给别人听的谎言 提交于 2019-12-23 05:38:10
问题 IDE: Android Studio 2.2.2 | Jmockit: 2.1 | Java: 1.8.0_102-b14 I tried to mock method toString() of class net.android.Uri. All method on this class could mock OK, but toString() method. @Test public void method(@Mocked final Uri uri) throws Exception { new NonStrictExpectations() { { uri.getHost(); result = "sendViewLog"; uri.getAuthority(); result = "getAuthority"; uri.getEncodedFragment(); result = "getEncodedFragment"; uri.getPort(); result = 8080; uri.toString(); // <------- result =

JMockit javaagent isn't initializing JMockit

回眸只為那壹抹淺笑 提交于 2019-12-22 10:48:29
问题 I've set up JMockit for use with some JUnit tests also using Robolectric, but I am getting errors. I'm primarily using maven to run the tests. When I run the test with mvn test and the javaagent configured as specified here I get the usual exception: java.lang.IllegalStateException: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation) I have validated that JMockit is on the classpath before JUnit

Mock a private method of the class under test in JMockit

穿精又带淫゛_ 提交于 2019-12-22 09:15:14
问题 In my class under test (CUT) - an ejb - I have a private method "getConnection". I want to test another method of the CUT but this method will fail before hand. I tried it like shown below, but "invoke" is wrong. I don't want to invoke the method, I want to stub it. But how? ('connection' is a stub) new NonStrictExpectations() { { invoke(archivingBean, "getConnection");result = connection; } }; archivingBean.moveCreditBasic2Archive(new Date()); 回答1: Your test is correct, except that it's

Wants to understand how @Tested works with JMockit

て烟熏妆下的殇ゞ 提交于 2019-12-22 07:59:49
问题 I am using JMockit since long.I would like to understand how @Tested works. Today i was trying to use it within my Test class. What i understand is Whatever class we wants to test we can mark it as @Tested. One thing which was confusing me about the behaviur of this is when i try to set something in @Before.Below is my query. My Class for which i want to write Test case public Class A{ public A(){} } Test class public class ATest { @Tested private A a; @Before public void setUp(){ a

How to mock JdbcTemplate.queryForObject() method

坚强是说给别人听的谎言 提交于 2019-12-21 17:36:47
问题 My method looks like this: public class Decompile extends JdbcDaoSupport public void getRunner(){ String val = this.getJdbcTemplate().queryForObject(sql,String.class, new Object[]{1001}); } } Please suggest how I would mock this. 回答1: an EasyMock-3.0 example String sql = "select * from t1"; Object[] params = new Object[] { 1001 }; JdbcTemplate t = EasyMock.createMock(JdbcTemplate.class); EasyMock.expect( t.queryForObject(sql, String.class, params)).andReturn("res"); EasyMock.replay(t); 回答2:

Mocking private method of class under test using JMockit

青春壹個敷衍的年華 提交于 2019-12-21 03:47:17
问题 I want to mock private method of a class under test but method return false first two times when the method is called after that it should return false. Here is the code what I tried. This is the class which is being tested public class ClassToTest { public void methodToTest() { Integer integerInstance = new Integer(0); boolean returnValue= methodToMock(integerInstance); if(returnValue) { System.out.println("methodToMock returned true"); } else { System.out.println("methodToMock returned true

How to mock the default constructor of the Date class with JMockit?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 08:57:30
问题 I want to mock the default constructor of java.util.date so it does not construct a Date object representing the time when it was created, but always the same Date object (in my example below 31 Dec 2010). I tried doing this with JMockit and JUnit , but when executing my test below, the output is always Thu Jan 01 01:00:00 CET 1970 . So what is wrong with my mock of Date() ? import java.util.Date; import org.junit.*; import mockit.*; public class AppTest { @Before public void setUp() { Mockit