java


Java Spring RequestContext test throws NullPointerException even with MockHttpServletRequest


I'm writing test for RequestContextHolder. I got the tests passed but then after I refactored I got NullPointerException but can't figure out why.
Here's the code.
String processorName = "<UNKNOWN>".intern();
Optional<HttpServletRequest> request = Optional.of(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest());
String procName = (String) request.get().getAttribute(CONTROLLER_NAME);
if (!Strings.isEmpty(procName)) {
processorName = procName;
}
return processorName;
And here's my tests
#Test
public void testProcessorName() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setAttribute(CONTROLLER_NAME, "default");
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
String processorName = halUtils.processorName();
assertEquals("default", processorName);
}
#Test
public void testProcessorNameWithoutAttribute() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
String processorName = halUtils.processorName();
assertEquals("<UNKNOWN>", processorName);
}
#Test
public void testProcessorNameWithoutRequest() throws Exception {
String processorName = halUtils.processorName();
assertEquals("<UNKNOWN>", processorName);
}
Then I refactored to be a one liner but I got NullPointerException instead.
public String processorName() {
return Optional.of(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getAttribute(CONTROLLER_NAME)).orElse("<UNKNOWN>".intern()).toString();
}
What you are doing is basically:
return Optional.of(null).orElse("<UNKNOWN>".intern()).toString();
Doing so will give you a NullPointerException, because the return of getAttribute(CONTROLLER_NAME) is null, and Optional.of(..) does not accept null parameters, while in your original code, you were testing its value with Strings.isEmpty(..).

Related Links

How to receive simultaneously converted and raw request body on spring mvc
SparkML feature vectors and Spark 2.0.2 Encoders in Java
Method returns null and tries to call method
How to save pdf file generated in spring mvc in to database directly after being generated..?
How do use the getters made in a different class?
Trying to instanciate interface javax.persistence.Table with unknown elements
How to style google maps with Android SDK (map structure)
Volatile for int? [duplicate]
Caesar Cipher with at least 5 methods
Android ListView with button
mysql-connector strange behaviour in Tomcat
Java Runtime exec() not working
Unable to inject AutoWired Enviroment when using Spring Boot 1.4 testing annotations
order dependency (topological sort) with input of string
How to properly print nested HTML lists using iText? [duplicate]
Cancel postDelayed within onTextChanged

Categories

HOME
cloud
deezer
plot
relative-path
objectgears
octobercms
rsyslog
at-command
maven-3
cakephp-2.5
windows-server
android-youtube-api
awesome-wm
correlation
task
percona
opentracing
pythonanywhere
visual-composer
nodatime
amazonsellercentral
visjs
ehcache
kvc
realex-payments-api
cultureinfo
lcd
firebase-crash-reporting
vlsi
web-api-testing
neo4j-spatial
opentype
objectanimator
quote
librato
bluestacks
binary-data
preg-match-all
scorm2004
kendo-ui-grid
geopositioning
atl
scaffold
dynamics-crm-2013
lumberjack
greenrobot-eventbus
preconditions
gpx
skeleton-css-boilerplate
jsch
network-flow
hibernate-tools
revapi
jquery-nestable
rails-routing
jquery-validate
typescript1.8
integrity
mu
aurelia-validation
carrot
boost-preprocessor
orthogonal
quartz-composer
jquery-filter
nxt
trash
py2app
mongocsharpdriver
autorest
simplewebrtc
ubuntu-10.04
packagist
apachebench
tcpserver
eclipse-clp
ruby-2.2
atk4
tarjans-algorithm
dukescript
generic-programming
p2
pretty-print
block-device
modalpopup
network-interface
rdtsc
google-reader
openexr
typo3-neos
viadeo
dotnetnuke-5
dataadapter
qt-faststart
yui-compressor
sqlperformance
spring-validator
enterprisedb
sublist
manchester-syntax
armcc
pendrive
adk
window-management
xmlspy
rfc1123
servicehost
table-footer
netbeans-7.1
radcombobox
backcolor
yagni
zune
dbisam

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile