Mockito Method call failing.

I have configured my pom.xml file for Mockito by adding this section:

		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-core</artifactId>
			<version>1.10.5</version>
		</dependency>

Open in new window


I can see the mockito-core-1.10.5 jar in my Libraries folder.

In my test controller test file I mock the security in the following manner:

	@Before
	public void setUp() throws Exception {
		request = new MockHttpServletRequest();
		response = new MockHttpServletResponse();
		userVO = Mockito.mock(UserVO.class);
		securityContextHolder = Mockito.mock(SecurityContextHolder.class);
		authentication = Mockito.mock(Authentication.class);
		securityContext = Mockito.mock(SecurityContext.class);

		Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
		Mockito.when(authentication.getPrincipal()).thenReturn(userVO);

	}

Open in new window



In my controller I make used of the mocked up methods in the following manner:

		System.out.println("\n SecurityContextHolder.getContext(): " + SecurityContextHolder.getContext());
		System.out.println("\n  SecurityContextHolder.getContext().getAuthentication(): " + SecurityContextHolder.getContext().getAuthentication());
		System.out.println("\n SecurityContextHolder.getContext().getAuthentication().principal(): " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());

Open in new window


The code always throws a NullPointerException at the line:

System.out.println("\n  SecurityContextHolder.getContext().getAuthentication(): " + SecurityContextHolder.getContext().getAuthentication());


The call to the mocked up method 'getAuthentication' always throws a NullPointerException.

Anyone knows why this happens and can help me with a solution.  My thought is that it is not calling the mocked method successfully.  there might be some disconnect there.
onaled777Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
The code always throws a NullPointerException at the line:

Are you positive that it is that line? It isn't line the below that that's throwing the NPE is it? That would surprise me as the documentation says that .getContext() "never returns a null" so I'm not sure how that line that you indicated could throw a NPE.

My thought is that it is not calling the mocked method successfully.  there might be some disconnect there.

Yes there is a disconnect. There is nothing linking the mock objects that you are creating and those three lines of code where you are getting the context/auth/principal.

First, you can ditch the mock SecurityContextHolder object because that class only has static methods, ie. there is never a need (either in prod code or testing) to create an object of that class, you just use the static methods. So you can delete line 6 of your above snippet.

Now to connect it all together, you should just need to add an extra line to your setUp(), like below...

	@Before
	public void setUp() throws Exception {
		request = new MockHttpServletRequest();
		response = new MockHttpServletResponse();
		userVO = Mockito.mock(UserVO.class);
		authentication = Mockito.mock(Authentication.class);
		securityContext = Mockito.mock(SecurityContext.class);

		Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
		Mockito.when(authentication.getPrincipal()).thenReturn(userVO);

		SecurityContextHolder.setContext(securityContext);     // This line is what will 'connect' your mock objects to your controller code.
	}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
onaled777Author Commented:
THANK YOU!
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.