Questions tagged [mockmvc]

MockMVC is the key part of the Spring MVC Test framework is. It simulates the internals of Spring MVC.

MockMVC is the key part of the Spring MVC Test framework is. It simulates the internals of Spring MVC.

742 questions
0
votes
2 answers

Mock SpringContextHolder using MockMVC

I am writing unit test cases for Controller layer. I have a call where i am getting user from Spring SecurityContextHolder. When i run my test case i get Null pointer exception because I don't know how to mock Spring security context. Below is my…
Sohaib Yasir
  • 41
  • 1
  • 5
0
votes
2 answers

406 error while mocking File Download REST API using MockMVC

I am implementing a REST API using Spring framework, which returns return new ResponseEntity<>(new InputStreamResource(myInputStream), responseHeaders, HttpStatus.OK); REST API is declared as: @RequestMapping(value = "/download",…
rmagarwal
  • 1
  • 4
0
votes
2 answers

mock a non spring managed object while integration testing using mockMVC

I am writing integration test cases using MockMvc to test my REST API. Within my implementation of the RESTAPI I am internally using RestTemplate(not directly from the controller but from within a util class which the controller calls) to call a…
Ashwini
  • 1
  • 2
0
votes
0 answers

Getting value of a JSON property using JSONpath

I have the following JSON and I need to check if the value of total is 10 using JSONPath: { "total": 10, "parkingSlots": [ { "type": "MOTORCYCLE", "empty": true, "spaceId": "1m60" }, { "type": "MOTORCYCLE", …
0
votes
2 answers

Mocking spring + hibernate using mockmvc and mockito

I am using spring + hibernate in my project and below is my java code @Repository @Transactional public class DomainDaoImpl implements DomainDao { static Logger logger = LoggerFactory.getLogger(DomainDaoImpl.class); @Autowired …
Nagendra
  • 191
  • 1
  • 3
  • 20
0
votes
0 answers

Spring Boot, MockMvc: how do I add an attribute to the model?

I'm using MockMvc to test a method on my controller. But that controller method, as designed, expects a certain attribute to have been added to the model already, which would be the case in normal use. But when testing just this method, I can't…
TimF
  • 151
  • 1
  • 2
  • 8
0
votes
1 answer

Unit testing spring mvc with spring-test and junit

I am new to unit testing with spring-test. I have a spring-mvc-rest application. I am able to mock the environment using MockMvc. My question is do I need to build the MockMvc object in every class of testing? Would that not be repetitive…
Akshay
  • 3,558
  • 4
  • 43
  • 77
0
votes
1 answer

Exception caught while running junit

I am getting below error while checking the output of my junit, it seems there is something wrong with JSON path provided with spring. java.lang.NoSuchMethodError:…
Bhavesh
  • 882
  • 2
  • 9
  • 18
0
votes
1 answer

IllegalStateException at start up Spring JUnit test

I've made a simple Maven Spring REST project. (this one: https://spring.io/guides/gs/rest-service/) Afterwards I made a simple JUnit test using MockMVC to test the REST functionality. In this case: does my code respond to /greeting? When I run my…
Ricardo
  • 335
  • 1
  • 4
  • 13
0
votes
0 answers

MockMvc: Forwarded URL expected: but was:

I have the below Velocity config for both test and main in my spring mvc app: @Bean public VelocityConfigurer velocityConfig(){ VelocityConfigurer velocityConfig = new VelocityConfigurer(); …
user1561108
  • 2,666
  • 9
  • 44
  • 69
0
votes
1 answer

hibernate detached object persist

I am writing Test cases for a webapp which requires me to mock controllers and verify results from URL hits. However, I am getting the error "detached entity passed to persist". The webapp however is live and working without errors meaning the…
Sarthak Nigam
  • 117
  • 1
  • 10
0
votes
0 answers

Testing controllers in Spring?

I want to write a functional test by calling some controllers. @Autowired private WebApplicationContext ctx; private MockMvc mockMvc; .... MvcResult mvcResult =…
Rey333
  • 350
  • 2
  • 12
0
votes
2 answers

Why is this MockMvc test failing?

This is the Controller I have: @Controller @EnableWebMvc @RequestMapping(value = "/spittles") public class SpittleController { SpittleRepository spittleRepository; @RequestMapping(method = RequestMethod.GET) public String…
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
0
votes
0 answers

Junit Spring Portlet with MockMvc post request

Need to test the redirect in action response using mockmvc in Junit Controller.java @ActionMapping(param="submit") public void handleSubmit(ActionRequest req, ActionResponse res) { ... …
AJJ
  • 3,570
  • 7
  • 43
  • 76
0
votes
2 answers

Mocked repository does not trigger as expected

I have a Controller Unit test using Mockito and MockMvc. After a POST request, the POSTed object is resolved correctly, but my repository mock is not triggered. Here is the mock code: Date mydate = new Date(); Notification not = new…