This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Joe Ess
  • salvin francis
  • fred rosenberger

Powermock - null is returned

Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I need your help with the following issue related with Junit testing:

I have the following class under test:

and the following unit test

when I uncomment the  "assertEquals("res",ExceptionUtil.stackToString(throwMock));" code I'm getting a null object returned.

java.lang.AssertionError: expected:<res> but was:<null>
at org.junit.Assert.failNotEquals(
at org.junit.Assert.assertEquals(
at org.junit.Assert.assertEquals(
at com.vw.client.test.ExceptionUtilTest.test(

I've tried to debug the method under test with Junit but without any success.

What I'm doing wrong?


Posts: 14767
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused... what are you trying to test here?  Because line 31 mocks all the static methods of the ExceptionUtil class and yet the rest of the test code appears to be wanting to test the static stackToString() method.

You don't mock code that you're actually trying to test. You only mock the dependencies that you want to control the behavior of. The Throwable object that the stackToString() method is collaborating with can be mocked but not the method under test itself.

This is the problem with mocks, especially when you're just starting out with unit testing. Beginners often get confused and end up testing mocks themselves instead of the production code.

The PowerMock README wrote:Please note that PowerMock is mainly intended for people with expert knowledge in unit testing. Putting it in the hands of junior developers may cause more harm than good.

If you're just trying to learn how to unit test, I suggest you start out by NOT using mock objects.
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!