jmockit

Mock private method in the same class that is being tested

余生颓废 提交于 2019-12-31 10:42:02
问题 I have a Java class named, MyClass , that I want to test with JUnit. The public method, methodA , that I want to test calls a private method, methodB , in the same class to determine which conditional path to follow. My goal is to write JUnit tests for the different paths in methodA . Also, methodB calls a service, so I do not want it to actually be executed when I run the JUnit tests. What is the best way to mock methodB and control its return so that I can test different paths for 'methodA'

jMockit: How to expect constructor calls to Mocked objects?

大憨熊 提交于 2019-12-30 21:04:39
问题 I am unit-testing a method performing some serialization operations. I intend to mock the serialization logic. The code is as below: ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); I have created the following mock objects: @Mocked FileInputStream mockFIS; @Mocked BufferedInputStream mockBIS; @Mocked ObjectInputStream mockOIS; I have setup a NonStrictExpectations() block where I want to expect the above constructor calls. Any ideas on how I

jMockit: How to expect constructor calls to Mocked objects?

旧时模样 提交于 2019-12-30 21:03:21
问题 I am unit-testing a method performing some serialization operations. I intend to mock the serialization logic. The code is as below: ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); I have created the following mock objects: @Mocked FileInputStream mockFIS; @Mocked BufferedInputStream mockBIS; @Mocked ObjectInputStream mockOIS; I have setup a NonStrictExpectations() block where I want to expect the above constructor calls. Any ideas on how I

Comparison between Mockito vs JMockit - why is Mockito voted better than JMockit? [closed]

十年热恋 提交于 2019-12-29 10:03:45
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I'm investigating which mocking framework to use for my project and have narrowed it down to JMockit and Mockito. I notice that

How to mock public void method using jmockit?

℡╲_俬逩灬. 提交于 2019-12-25 02:29:31
问题 I am trying to write some junit test for my Spring MVC controller and so far I am able to do it successfully. I am using Spring Mock along with jmockit as well. Below is my method in the DataController controller - @RequestMapping(value = "processWork", method = RequestMethod.GET) public @ResponseBody DataResponse processWork(@RequestParam("conf") final String value, @RequestParam("dc") final String dc) { // .. some code here SomeOtherClass modifier = new SomeOtherClass(zookClient); List<Map

How to mock JdbcTemplate.update using Jmockit?

╄→尐↘猪︶ㄣ 提交于 2019-12-24 20:43:29
问题 I'm new to Jmockit and I'm trying to mock jdbcTemplate.udpate() using the following verification, new Expectations() {{ someRef.flushUpdates(); }}; new Verifications() {{ String query; jdbcTemplate.update(query = withCapture(), withInstanceOf(Date.class)); times = 1; }}; The flushUpdate has an update query, public void flushUpdates(){ Date now = new Date(); String query = "Update table_name set last_updated = ? "; jdbcTemplate.update(query,now); } The test is to verify if update query is

jMockit - How make constructor invocation to return a mock

江枫思渺然 提交于 2019-12-24 16:44:12
问题 We are currently using Mockito + PowerMock as our main mocking framework and had a few issues with this once started moving some of our code to java 8. Because of this we decided to evaluate jMockit as an alternative. I have quite a good understanding of mocking concepts but I admit that my experience with jMockit is very limited. However I am having problems with testing something that in my view should be very basic: the class under test creates an instance of some other class inside its

In jmockit, how can I mock a void method to throw an Exception on the first call and not to on subsequent calls?

a 夏天 提交于 2019-12-24 15:32:34
问题 I can make a void method throw an exception like this: class TestClass { public void send(int a) {}; } @Mocked private TestClass mock; @Test public void test() throws Exception { new Expectations() { { mock.send(var1); this.result = new Exception("some exception"); } }; } however, if I want the void method to throw an exception on the first call, but not on subsequent calls, these approaches do not seem to work: @Test public void test() throws Exception { new Expectations() { { mock.send(var1

How to mock a top-level-function in kotlin with jmockit

旧街凉风 提交于 2019-12-24 09:47:45
问题 Assuming the I have a function to be test below, declare at the file named "Utils.kt" //Utils.kt fun doSomething() = 1 Then we create a test class to test it //UtilsTest.kt @RunWith(JMockit::class) class UtilsTest { @Test fun testDoSomething() { object : Expectation() { init { doSomething() result = 2 } } assertEquals(2, doSomething()) } } I want to mock doSomething , make it return 2 , but it won't work, actual result is 1 Is there any workaround for this purpose? 回答1: A workaround mock it

How to mock a top-level-function in kotlin with jmockit

痞子三分冷 提交于 2019-12-24 09:42:01
问题 Assuming the I have a function to be test below, declare at the file named "Utils.kt" //Utils.kt fun doSomething() = 1 Then we create a test class to test it //UtilsTest.kt @RunWith(JMockit::class) class UtilsTest { @Test fun testDoSomething() { object : Expectation() { init { doSomething() result = 2 } } assertEquals(2, doSomething()) } } I want to mock doSomething , make it return 2 , but it won't work, actual result is 1 Is there any workaround for this purpose? 回答1: A workaround mock it