// Pass in a stub that was created by a mocking framework. AccessManager accessManager = new AccessManager(stubAuthenticationService); // The user shouldn't have access when the authentication service returns false. when(stubAuthenticationService.isAuthenticated(USER_ID)).thenReturn(false); assertFalse(accessManager.userHasAccess(USER_ID)); // The user should have access when the authentication service returns true. when(stubAuthenticationService.isAuthenticated(USER_ID)).thenReturn(true); assertTrue(accessManager.userHasAccess(USER_ID));
// Pass in a mock that was created by a mocking framework. AccessManager accessManager = new AccessManager(mockAuthenticationService); accessManager.userHasAccess(USER_ID); // The test should fail if accessManager.userHasAccess(USER_ID) didn't call // mockAuthenticationService.isAuthenticated(USER_ID) or if it called it more than once. verify(mockAuthenticationService).isAuthenticated(USER_ID);
// Creating the fake is fast and easy. AuthenticationService fakeAuthenticationService = new FakeAuthenticationService(); AccessManager accessManager = new AccessManager(fakeAuthenticationService); // The user shouldn't have access since the authentication service doesn't // know about the user. assertFalse(accessManager.userHasAccess(USER_ID)); // The user should have access after it's added to the authentication service. fakeAuthenticationService.addAuthenticatedUser(USER_ID); assertTrue(accessManager.userHasAccess(USER_ID));
Hi, you have some : in place of some ; in mock and fake code samples. :)Good explanations and examples, thanks.
Have you tried AQuA instead of PESQ?
Great, really simple explanation! Thanks!
Nice, great to know mock test doubles can be created using a mocking framework
The comments you read and contribute here belong only to the person who posted them. We reserve the right to remove off-topic comments.
Hi, you have some : in place of some ; in mock and fake code samples. :)
ReplyDeleteGood explanations and examples, thanks.
Have you tried AQuA instead of PESQ?
ReplyDeleteGreat, really simple explanation! Thanks!
ReplyDeleteNice, great to know mock test doubles can be created using a mocking framework
ReplyDelete