Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Testing
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
TDD for a Shopping Website LiveProject
this week in the
Testing
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Testing
simulating exceptions
Peter Kovac
Ranch Hand
Posts: 42
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I need to
test
this method
catch
block but couldn't find out how to simulate
UnsupportedEncodingException
. I'm wondering if it's possible to test this block
without
changing the code at all.
private ResultObject calcValue (String key) { try { ... String str = URLEncoder.encode (key, "UTF-8") ); ... return ...; } catch (UnsupportedEncodingException e) { ... fallback calculations ... return ... ; } }
Thanks,
enric jaen
Greenhorn
Posts: 27
posted 9 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
@RunWith(PowerMockRunner.class) public class YourTest { @Test @PrepareForTest(URLEncoder.class) public void checkCalcValueCatchesException { PowerMock.mockStatic(URLEncoder.class); String key="..."; EasyMock.expect( URLEncoder.encode (key, "UTF-8") ).andThrow(new UnsupportedEncodingException ()); PowerMock.replay(URLEncoder.class); //here call the public method which calls calcValue() } }
Peter Kovac
Ranch Hand
Posts: 42
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks
moose poop looks like football shaped elk poop. About the size of this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
I am getting StreamCorruptedException
String encoding and decoding
How to hide key in client?
Try catch block.
Need help in password Encryption and Decryption
More...