use of org.mockito.internal.stubbing.answers.Returns in project mockito by mockito.
the class InvocationContainerImplStubbingTest method should_get_results_for_methods_stub_only.
@Test
public void should_get_results_for_methods_stub_only() throws Throwable {
invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
invocationContainerImplStubOnly.addAnswer(new Returns("simpleMethod"));
Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
invocationContainerImplStubOnly.addAnswer(new ThrowsException(new MyException()));
assertEquals("simpleMethod", invocationContainerImplStubOnly.answerTo(simpleMethod));
try {
invocationContainerImplStubOnly.answerTo(differentMethod);
fail();
} catch (MyException e) {
}
}
use of org.mockito.internal.stubbing.answers.Returns in project mockito by mockito.
the class InvocationContainerImplStubbingTest method should_finish_stubbing_on_adding_return_value.
@Test
public void should_finish_stubbing_on_adding_return_value() throws Exception {
state.stubbingStarted();
invocationContainerImpl.addAnswer(new Returns("test"));
state.validateState();
}
use of org.mockito.internal.stubbing.answers.Returns in project mockito by mockito.
the class InvocationContainerImplStubbingTest method should_get_results_for_methods.
@Test
public void should_get_results_for_methods() throws Throwable {
invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
invocationContainerImpl.addAnswer(new Returns("simpleMethod"));
Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
invocationContainerImpl.addAnswer(new ThrowsException(new MyException()));
assertEquals("simpleMethod", invocationContainerImpl.answerTo(simpleMethod));
try {
invocationContainerImpl.answerTo(differentMethod);
fail();
} catch (MyException e) {
}
}
use of org.mockito.internal.stubbing.answers.Returns in project mockito by mockito.
the class InvocationContainerImplTest method doShouldBeThreadSafe.
//works 50% of the time
private void doShouldBeThreadSafe(final InvocationContainerImpl c) throws Throwable {
//given
Thread[] t = new Thread[200];
final CountDownLatch starter = new CountDownLatch(200);
for (int i = 0; i < t.length; i++) {
t[i] = new Thread() {
public void run() {
try {
//NOPMD
starter.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
c.setInvocationForPotentialStubbing(new InvocationMatcher(invocation));
c.addAnswer(new Returns("foo"));
c.findAnswerFor(invocation);
}
};
t[i].setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
exceptions.add(e);
}
});
t[i].start();
starter.countDown();
}
//when
for (Thread aT : t) {
aT.join();
}
//then
if (exceptions.size() != 0) {
throw exceptions.getFirst();
}
}
use of org.mockito.internal.stubbing.answers.Returns in project spring-security by spring-projects.
the class DelegatingSecurityContextCallableTests method setUp.
@Before
@SuppressWarnings("serial")
public void setUp() throws Exception {
originalSecurityContext = SecurityContextHolder.createEmptyContext();
when(delegate.call()).thenAnswer(new Returns(callableResult) {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
assertThat(SecurityContextHolder.getContext()).isEqualTo(securityContext);
return super.answer(invocation);
}
});
executor = Executors.newFixedThreadPool(1);
}
Aggregations