Your question is quite badly worded and your code example is far from how mocks are usually used. However, given the benefit of the doubt, I'm assuming you're wanting to know why your test doesn't get past the if
block.
It is essentially because you've called session.getAttribute("testAttribute")
twice, but only expected it once.
So, you have this following expectation:
expect(session.getAttribute("testAttribute")).andReturn("testValue");
But, then you have this block:
if(session.getAttribute("testAttribute")!=null){
System.out.println("fired session");
System.out.println((String)session.getAttribute("testAttribute"));
}
So you need to expect the call twice, since you call it twice. There are a number of ways to do this. Any of the following would work:
- Using the
times(int)
method. - Using the
anyTimes()
method. - Calling the expectation multiple times.
Here are examples of each of those options.
1. expect(session.getAttribute("testAttribute")).andReturn("testValue").times(2);
2. expect(session.getAttribute("testAttribute")).andReturn("testValue").anyTimes();
3. expect(session.getAttribute("testAttribute")).andReturn("testValue");
expect(session.getAttribute("testAttribute")).andReturn("testValue");