Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When testing controller mappings using AnnotationMethodHandlerAdapter NPE is thrown [SPR-9629] #14263

Closed
spring-projects-issues opened this issue Jul 24, 2012 · 4 comments
Assignees
Labels
in: web type: bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jul 24, 2012

Oleksiy Rezchykov opened SPR-9629 and commented

When using AnnotationMethodHandlerAdapter with Mock request and response to test controller mappings and controller under test has class- and method-level @RequestMapping annotation - receiving an NPE. I have to manually put some attribute to the request. Please see the test from URL for details. The bug root case is null unsafe useTypeLevelMapping method at AMHA. I propose to change the return to something which is close to useSuffixPattern method return. Like "
Object value = request.getAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING);
return (value != null) ? (Boolean) value : Boolean.TRUE;
"


Affects: 3.1.2

Reference URL: https://github.com/springbyexample/spring-by-example/blob/master/spring-by-example-2/src/test/java/ua/com/springbyexample/controller/EmployeeControllerTest.java

Sub-tasks:

  • #14267 Backport "When testing controller mappings using AnnotationMethodHandlerAdapter NPE is thrown"

Referenced from: commits a7e2297, 914557b

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 25, 2012

Rossen Stoyanchev commented

This is now fixed but I wonder if you have seen the spring-test-mvc project yet for the kinds of tests you have?

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 27, 2012

Oleksiy Rezchykov commented

Thanks for the link! Added to my pet-project. But on my mind for this particular type of tests AMHA is just enough because spring-test-mvc even in standalone mode creates a real context which makes test quite heavy. Also in standalone mode you should somehow inject your services which is easier with specific test context.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 27, 2012

Rossen Stoyanchev commented

spring-test-mvc even in standalone mode creates a real context which makes test quite heavy

It's quite fast actually. The created context is static, think of it as a map. It's there for the Dispatcher, which discovers components like HandlerMapping's, and others.

Also in standalone mode you should somehow inject your services which is easier with specific test context.

I don't follow this point. With this option you create your own Controller instances so you can inject them with mock services just like in a "pure" unit test.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 27, 2012

Oleksiy Rezchykov commented

  1. I agree that standalone is very fast, but still not faster than "plain" test
  2. Frankly speaking I am too lazy to create mocks for all used services, I just created a Mockito factory as a part of the test context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web type: bug
Projects
None yet
Development

No branches or pull requests

2 participants