Jmockit activate and deactivate MockUp object inside the same methode
I am writing a test case for a scenario at which the customer can upload contents into the DB. in this test case I assumed the contents are missing, so RuntimeException(PAYLOAD_NOT_FOUND)
will be thrown and in the catch
block I will construct dummy content and continue another execution path.
To simulate missing contents I am mocking the getContent getter
new MockUp<CustomerContentDto>() {
@Mock
public String getContent() {
throw new RuntimeException(PAYLOAD_NOT_FOUND);
}
};
So far so good, the problem that I need to call public String getContent()
after constructing the dummy contents, but every time I call this method the mock will throw new RuntimeException(PAYLOAD_NOT_FOUND);
Is there is any way to deactivate that mock after certain execution point?
@Test
public void uploadContent_PayloadMissing_uploadError() throws DaoException, URISyntaxException, IOException,MessageTaskException {
final String sameAccounts = "some info...";
new MockUp<CustomerContentDto>() {
@Mock
public String getContent() {
throw new RuntimeException(PAYLOAD_NOT_FOUND);
}
};
// i need to mock getContent() befor this call
objectUnderTest.handleMessage(111,sxpConnection);
new Verifications() {{
CustomerContentDto customerContentDto;
iCustomerContentDao.setApproved(anyLong, anyString, null);
minTimes = 1;
maxTimes = 1;
iCustomerContentDao.uploadContent(customerContentDto = withCapture());
maxTimes = 1;
// i need to deactivate the mock getContent() befor this call
Assert.assertEquals(customerContentDto.getContent(), "111nn"Content is missing a Payload tag"");
}};
}
java unit-testing testng jmockit
add a comment |
I am writing a test case for a scenario at which the customer can upload contents into the DB. in this test case I assumed the contents are missing, so RuntimeException(PAYLOAD_NOT_FOUND)
will be thrown and in the catch
block I will construct dummy content and continue another execution path.
To simulate missing contents I am mocking the getContent getter
new MockUp<CustomerContentDto>() {
@Mock
public String getContent() {
throw new RuntimeException(PAYLOAD_NOT_FOUND);
}
};
So far so good, the problem that I need to call public String getContent()
after constructing the dummy contents, but every time I call this method the mock will throw new RuntimeException(PAYLOAD_NOT_FOUND);
Is there is any way to deactivate that mock after certain execution point?
@Test
public void uploadContent_PayloadMissing_uploadError() throws DaoException, URISyntaxException, IOException,MessageTaskException {
final String sameAccounts = "some info...";
new MockUp<CustomerContentDto>() {
@Mock
public String getContent() {
throw new RuntimeException(PAYLOAD_NOT_FOUND);
}
};
// i need to mock getContent() befor this call
objectUnderTest.handleMessage(111,sxpConnection);
new Verifications() {{
CustomerContentDto customerContentDto;
iCustomerContentDao.setApproved(anyLong, anyString, null);
minTimes = 1;
maxTimes = 1;
iCustomerContentDao.uploadContent(customerContentDto = withCapture());
maxTimes = 1;
// i need to deactivate the mock getContent() befor this call
Assert.assertEquals(customerContentDto.getContent(), "111nn"Content is missing a Payload tag"");
}};
}
java unit-testing testng jmockit
I can only offer to say: in Mockito, you can create a fluent chain call, likewhen(foo.bar).thenThrow(new Whatever()).thenReturn()
.
– GhostCat
Nov 15 '18 at 11:56
I'm not even sure if can do that. Once you mock the method, you have it mocked forever (at least, as far as I know). Either way, you're doing a weird test, if you mock a method to give out an exception, why are you using it again for a verification? Maybe partial mocking could help you but I doubt it as it mocks methods jmockit.github.io/tutorial/Mocking.html#partial
– Alfergon
Nov 16 '18 at 9:22
You can, of course, record a sequence of results for an expectation with the JMockit mocking API. This test has lots of problems, however... For starters, why use aMockUp
in the first place? Or why mock a DTO?
– Rogério
Nov 19 '18 at 17:18
add a comment |
I am writing a test case for a scenario at which the customer can upload contents into the DB. in this test case I assumed the contents are missing, so RuntimeException(PAYLOAD_NOT_FOUND)
will be thrown and in the catch
block I will construct dummy content and continue another execution path.
To simulate missing contents I am mocking the getContent getter
new MockUp<CustomerContentDto>() {
@Mock
public String getContent() {
throw new RuntimeException(PAYLOAD_NOT_FOUND);
}
};
So far so good, the problem that I need to call public String getContent()
after constructing the dummy contents, but every time I call this method the mock will throw new RuntimeException(PAYLOAD_NOT_FOUND);
Is there is any way to deactivate that mock after certain execution point?
@Test
public void uploadContent_PayloadMissing_uploadError() throws DaoException, URISyntaxException, IOException,MessageTaskException {
final String sameAccounts = "some info...";
new MockUp<CustomerContentDto>() {
@Mock
public String getContent() {
throw new RuntimeException(PAYLOAD_NOT_FOUND);
}
};
// i need to mock getContent() befor this call
objectUnderTest.handleMessage(111,sxpConnection);
new Verifications() {{
CustomerContentDto customerContentDto;
iCustomerContentDao.setApproved(anyLong, anyString, null);
minTimes = 1;
maxTimes = 1;
iCustomerContentDao.uploadContent(customerContentDto = withCapture());
maxTimes = 1;
// i need to deactivate the mock getContent() befor this call
Assert.assertEquals(customerContentDto.getContent(), "111nn"Content is missing a Payload tag"");
}};
}
java unit-testing testng jmockit
I am writing a test case for a scenario at which the customer can upload contents into the DB. in this test case I assumed the contents are missing, so RuntimeException(PAYLOAD_NOT_FOUND)
will be thrown and in the catch
block I will construct dummy content and continue another execution path.
To simulate missing contents I am mocking the getContent getter
new MockUp<CustomerContentDto>() {
@Mock
public String getContent() {
throw new RuntimeException(PAYLOAD_NOT_FOUND);
}
};
So far so good, the problem that I need to call public String getContent()
after constructing the dummy contents, but every time I call this method the mock will throw new RuntimeException(PAYLOAD_NOT_FOUND);
Is there is any way to deactivate that mock after certain execution point?
@Test
public void uploadContent_PayloadMissing_uploadError() throws DaoException, URISyntaxException, IOException,MessageTaskException {
final String sameAccounts = "some info...";
new MockUp<CustomerContentDto>() {
@Mock
public String getContent() {
throw new RuntimeException(PAYLOAD_NOT_FOUND);
}
};
// i need to mock getContent() befor this call
objectUnderTest.handleMessage(111,sxpConnection);
new Verifications() {{
CustomerContentDto customerContentDto;
iCustomerContentDao.setApproved(anyLong, anyString, null);
minTimes = 1;
maxTimes = 1;
iCustomerContentDao.uploadContent(customerContentDto = withCapture());
maxTimes = 1;
// i need to deactivate the mock getContent() befor this call
Assert.assertEquals(customerContentDto.getContent(), "111nn"Content is missing a Payload tag"");
}};
}
java unit-testing testng jmockit
java unit-testing testng jmockit
edited Nov 16 '18 at 11:17
Alfergon
3,17943145
3,17943145
asked Nov 15 '18 at 11:51
Melad EzzatMelad Ezzat
8141727
8141727
I can only offer to say: in Mockito, you can create a fluent chain call, likewhen(foo.bar).thenThrow(new Whatever()).thenReturn()
.
– GhostCat
Nov 15 '18 at 11:56
I'm not even sure if can do that. Once you mock the method, you have it mocked forever (at least, as far as I know). Either way, you're doing a weird test, if you mock a method to give out an exception, why are you using it again for a verification? Maybe partial mocking could help you but I doubt it as it mocks methods jmockit.github.io/tutorial/Mocking.html#partial
– Alfergon
Nov 16 '18 at 9:22
You can, of course, record a sequence of results for an expectation with the JMockit mocking API. This test has lots of problems, however... For starters, why use aMockUp
in the first place? Or why mock a DTO?
– Rogério
Nov 19 '18 at 17:18
add a comment |
I can only offer to say: in Mockito, you can create a fluent chain call, likewhen(foo.bar).thenThrow(new Whatever()).thenReturn()
.
– GhostCat
Nov 15 '18 at 11:56
I'm not even sure if can do that. Once you mock the method, you have it mocked forever (at least, as far as I know). Either way, you're doing a weird test, if you mock a method to give out an exception, why are you using it again for a verification? Maybe partial mocking could help you but I doubt it as it mocks methods jmockit.github.io/tutorial/Mocking.html#partial
– Alfergon
Nov 16 '18 at 9:22
You can, of course, record a sequence of results for an expectation with the JMockit mocking API. This test has lots of problems, however... For starters, why use aMockUp
in the first place? Or why mock a DTO?
– Rogério
Nov 19 '18 at 17:18
I can only offer to say: in Mockito, you can create a fluent chain call, like
when(foo.bar).thenThrow(new Whatever()).thenReturn()
.– GhostCat
Nov 15 '18 at 11:56
I can only offer to say: in Mockito, you can create a fluent chain call, like
when(foo.bar).thenThrow(new Whatever()).thenReturn()
.– GhostCat
Nov 15 '18 at 11:56
I'm not even sure if can do that. Once you mock the method, you have it mocked forever (at least, as far as I know). Either way, you're doing a weird test, if you mock a method to give out an exception, why are you using it again for a verification? Maybe partial mocking could help you but I doubt it as it mocks methods jmockit.github.io/tutorial/Mocking.html#partial
– Alfergon
Nov 16 '18 at 9:22
I'm not even sure if can do that. Once you mock the method, you have it mocked forever (at least, as far as I know). Either way, you're doing a weird test, if you mock a method to give out an exception, why are you using it again for a verification? Maybe partial mocking could help you but I doubt it as it mocks methods jmockit.github.io/tutorial/Mocking.html#partial
– Alfergon
Nov 16 '18 at 9:22
You can, of course, record a sequence of results for an expectation with the JMockit mocking API. This test has lots of problems, however... For starters, why use a
MockUp
in the first place? Or why mock a DTO?– Rogério
Nov 19 '18 at 17:18
You can, of course, record a sequence of results for an expectation with the JMockit mocking API. This test has lots of problems, however... For starters, why use a
MockUp
in the first place? Or why mock a DTO?– Rogério
Nov 19 '18 at 17:18
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318879%2fjmockit-activate-and-deactivate-mockup-object-inside-the-same-methode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318879%2fjmockit-activate-and-deactivate-mockup-object-inside-the-same-methode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I can only offer to say: in Mockito, you can create a fluent chain call, like
when(foo.bar).thenThrow(new Whatever()).thenReturn()
.– GhostCat
Nov 15 '18 at 11:56
I'm not even sure if can do that. Once you mock the method, you have it mocked forever (at least, as far as I know). Either way, you're doing a weird test, if you mock a method to give out an exception, why are you using it again for a verification? Maybe partial mocking could help you but I doubt it as it mocks methods jmockit.github.io/tutorial/Mocking.html#partial
– Alfergon
Nov 16 '18 at 9:22
You can, of course, record a sequence of results for an expectation with the JMockit mocking API. This test has lots of problems, however... For starters, why use a
MockUp
in the first place? Or why mock a DTO?– Rogério
Nov 19 '18 at 17:18