Mockito response code mismatch assert issue
I am trying to run a junit test file with below code, am getting exception as follows, i know if i can upgrade to latest version (springboot) of mockito it will work, but trying to resolve the code to make it work as it is.
I am getting response code as 404 if i use standaloneSetup in @Before setUp method, if i use webAppContextSetup getting 500 error. but instead am expecting 201.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"file:src/main/resources/applicationContext.xml", "file:src/main/resources/sampleDispatcherServlet-servlet.xml"})
public class SampleControllerTest {
@Autowired
private SampleController controller;
@Mock
private SampleService service;
@Autowired
private WebApplicationContext wac;
/** The mvc. */
private MockMvc mvc;
/** The mapper. */
private ObjectMapper mapper = new ObjectMapper();
@Before
public void setup() {
// MockitoAnnotations.initMocks(this);
// this.mvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockitoAnnotations.initMocks(this);
this.mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void testCreate() throws Exception {
when(service.create(ArgumentMatchers.any())).thenReturn(buildResponse());
MvcResult result = this.mvc
.perform(post("/rest/sample")
.content(mapper.writeValueAsString(requestObj()))
.accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated()).andDo(print()).andReturn();
assertThat(result.getResponse()).isNotNull();
}
private SampleResponse<SampleVO> buildResponse() {
SampleResponse<SampleVO> sampleResponse = new SampleResponse<SampleVO>();
sampleResponse.setCNum("21212");
sampleResponse.setId("312312");
return sampleResponse;
}
private RequestObj<SampleVO> requestObj() {
RequestObj<SampleVO> request = RequestObj.<SampleVO>builder()
.cNum("320").code("VI")
.payMethod(SampleVO.builder().digit("21212").expirationDate("22")
.expirationMonth("12").year("2025").build())
.statusCode("ACTIVE").build();
return request;
}
}
Pom xml:
<!-- Test -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.meanbean</groupId>
<artifactId>meanbean</artifactId>
<version>2.0.3</version>
<scope>test</scope>
junit mockito springmockito
add a comment |
I am trying to run a junit test file with below code, am getting exception as follows, i know if i can upgrade to latest version (springboot) of mockito it will work, but trying to resolve the code to make it work as it is.
I am getting response code as 404 if i use standaloneSetup in @Before setUp method, if i use webAppContextSetup getting 500 error. but instead am expecting 201.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"file:src/main/resources/applicationContext.xml", "file:src/main/resources/sampleDispatcherServlet-servlet.xml"})
public class SampleControllerTest {
@Autowired
private SampleController controller;
@Mock
private SampleService service;
@Autowired
private WebApplicationContext wac;
/** The mvc. */
private MockMvc mvc;
/** The mapper. */
private ObjectMapper mapper = new ObjectMapper();
@Before
public void setup() {
// MockitoAnnotations.initMocks(this);
// this.mvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockitoAnnotations.initMocks(this);
this.mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void testCreate() throws Exception {
when(service.create(ArgumentMatchers.any())).thenReturn(buildResponse());
MvcResult result = this.mvc
.perform(post("/rest/sample")
.content(mapper.writeValueAsString(requestObj()))
.accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated()).andDo(print()).andReturn();
assertThat(result.getResponse()).isNotNull();
}
private SampleResponse<SampleVO> buildResponse() {
SampleResponse<SampleVO> sampleResponse = new SampleResponse<SampleVO>();
sampleResponse.setCNum("21212");
sampleResponse.setId("312312");
return sampleResponse;
}
private RequestObj<SampleVO> requestObj() {
RequestObj<SampleVO> request = RequestObj.<SampleVO>builder()
.cNum("320").code("VI")
.payMethod(SampleVO.builder().digit("21212").expirationDate("22")
.expirationMonth("12").year("2025").build())
.statusCode("ACTIVE").build();
return request;
}
}
Pom xml:
<!-- Test -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.meanbean</groupId>
<artifactId>meanbean</artifactId>
<version>2.0.3</version>
<scope>test</scope>
junit mockito springmockito
add a comment |
I am trying to run a junit test file with below code, am getting exception as follows, i know if i can upgrade to latest version (springboot) of mockito it will work, but trying to resolve the code to make it work as it is.
I am getting response code as 404 if i use standaloneSetup in @Before setUp method, if i use webAppContextSetup getting 500 error. but instead am expecting 201.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"file:src/main/resources/applicationContext.xml", "file:src/main/resources/sampleDispatcherServlet-servlet.xml"})
public class SampleControllerTest {
@Autowired
private SampleController controller;
@Mock
private SampleService service;
@Autowired
private WebApplicationContext wac;
/** The mvc. */
private MockMvc mvc;
/** The mapper. */
private ObjectMapper mapper = new ObjectMapper();
@Before
public void setup() {
// MockitoAnnotations.initMocks(this);
// this.mvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockitoAnnotations.initMocks(this);
this.mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void testCreate() throws Exception {
when(service.create(ArgumentMatchers.any())).thenReturn(buildResponse());
MvcResult result = this.mvc
.perform(post("/rest/sample")
.content(mapper.writeValueAsString(requestObj()))
.accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated()).andDo(print()).andReturn();
assertThat(result.getResponse()).isNotNull();
}
private SampleResponse<SampleVO> buildResponse() {
SampleResponse<SampleVO> sampleResponse = new SampleResponse<SampleVO>();
sampleResponse.setCNum("21212");
sampleResponse.setId("312312");
return sampleResponse;
}
private RequestObj<SampleVO> requestObj() {
RequestObj<SampleVO> request = RequestObj.<SampleVO>builder()
.cNum("320").code("VI")
.payMethod(SampleVO.builder().digit("21212").expirationDate("22")
.expirationMonth("12").year("2025").build())
.statusCode("ACTIVE").build();
return request;
}
}
Pom xml:
<!-- Test -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.meanbean</groupId>
<artifactId>meanbean</artifactId>
<version>2.0.3</version>
<scope>test</scope>
junit mockito springmockito
I am trying to run a junit test file with below code, am getting exception as follows, i know if i can upgrade to latest version (springboot) of mockito it will work, but trying to resolve the code to make it work as it is.
I am getting response code as 404 if i use standaloneSetup in @Before setUp method, if i use webAppContextSetup getting 500 error. but instead am expecting 201.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"file:src/main/resources/applicationContext.xml", "file:src/main/resources/sampleDispatcherServlet-servlet.xml"})
public class SampleControllerTest {
@Autowired
private SampleController controller;
@Mock
private SampleService service;
@Autowired
private WebApplicationContext wac;
/** The mvc. */
private MockMvc mvc;
/** The mapper. */
private ObjectMapper mapper = new ObjectMapper();
@Before
public void setup() {
// MockitoAnnotations.initMocks(this);
// this.mvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockitoAnnotations.initMocks(this);
this.mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void testCreate() throws Exception {
when(service.create(ArgumentMatchers.any())).thenReturn(buildResponse());
MvcResult result = this.mvc
.perform(post("/rest/sample")
.content(mapper.writeValueAsString(requestObj()))
.accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated()).andDo(print()).andReturn();
assertThat(result.getResponse()).isNotNull();
}
private SampleResponse<SampleVO> buildResponse() {
SampleResponse<SampleVO> sampleResponse = new SampleResponse<SampleVO>();
sampleResponse.setCNum("21212");
sampleResponse.setId("312312");
return sampleResponse;
}
private RequestObj<SampleVO> requestObj() {
RequestObj<SampleVO> request = RequestObj.<SampleVO>builder()
.cNum("320").code("VI")
.payMethod(SampleVO.builder().digit("21212").expirationDate("22")
.expirationMonth("12").year("2025").build())
.statusCode("ACTIVE").build();
return request;
}
}
Pom xml:
<!-- Test -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.meanbean</groupId>
<artifactId>meanbean</artifactId>
<version>2.0.3</version>
<scope>test</scope>
junit mockito springmockito
junit mockito springmockito
asked Nov 12 '18 at 12:04
user2000189
114111
114111
add a comment |
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%2f53261813%2fmockito-response-code-mismatch-assert-issue%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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53261813%2fmockito-response-code-mismatch-assert-issue%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