@Test public void testGetUsersWithAuthority_page2() { String expectedQuery = "select \"public\".\"users\".\"username\", \"public\".\"users\".\"locked\", \"public\".\"authorities\".\"authority\", count(\"public\".\"users\".\"username\") over () as \"count\" from \"public\".\"users\" join \"public\".\"authorities\" on \"public\".\"users\".\"username\" = \"public\".\"authorities\".\"username\" where \"public\".\"authorities\".\"authority\" = 'ROLE_ADMIN' order by \"public\".\"users\".\"username\" asc limit 5 offset 5"; Pageable pageable = Mockito.mock(Pageable.class); Mockito.when(pageable.getPageNumber()).thenReturn(1); Mockito.when(pageable.getPageSize()).thenReturn(5); userDao.getUsersWithAuthority("ROLE_ADMIN", pageable); Mockito.verify(jdbcTemplate) .query(Matchers.eq(expectedQuery), Matchers.any(PagingRowCallbackHandler.class)); }
@Test public void testGetUsersWithAuthority() throws SQLException { String expectedQuery = "select \"public\".\"users\".\"username\", \"public\".\"users\".\"locked\", \"public\".\"authorities\".\"authority\", count(\"public\".\"users\".\"username\") over () as \"count\" from \"public\".\"users\" join \"public\".\"authorities\" on \"public\".\"users\".\"username\" = \"public\".\"authorities\".\"username\" where \"public\".\"authorities\".\"authority\" = 'ROLE_ADMIN' order by \"public\".\"users\".\"username\" asc limit 5 offset 0"; Pageable pageable = Mockito.mock(Pageable.class); ResultSet resultSet = Mockito.mock(ResultSet.class); Mockito.when(pageable.getPageNumber()).thenReturn(0); Mockito.when(pageable.getPageSize()).thenReturn(5); Mockito.when(resultSet.getInt(PagingConstants.COUNT)).thenReturn(1); Mockito.when(resultSet.getString("username")).thenReturn("admin"); RESTPage<UserDTO> page = userDao.getUsersWithAuthority("ROLE_ADMIN", pageable); ArgumentCaptor<PagingRowCallbackHandler> pagingRowCallbackHandlerCaptor = ArgumentCaptor.forClass(PagingRowCallbackHandler.class); Mockito.verify(jdbcTemplate) .query(Matchers.eq(expectedQuery), pagingRowCallbackHandlerCaptor.capture()); PagingRowCallbackHandler pagingRowCallbackHandler = pagingRowCallbackHandlerCaptor.getValue(); pagingRowCallbackHandler.processRow(resultSet); Mockito.verify(resultSet).getInt(PagingConstants.COUNT); Mockito.verify(resultSet).getString("username"); Assert.assertEquals(1, page.getContentSize()); Assert.assertEquals("admin", page.getContent().get(0).getUsername()); }
@Test public void testMatchersCreation() { assertTrue(Matchers.createMatcher("*moskito*") instanceof SubstringMatcher); assertTrue(Matchers.createMatcher("moskito*") instanceof EndsWithMatcher); assertTrue(Matchers.createMatcher("*moskito") instanceof StartsWithMatcher); assertTrue(Matchers.createMatcher("moskito") instanceof EqualsMatcher); // check that asterisk doesn't have any impact. assertTrue(Matchers.createMatcher("*mos*kito*") instanceof SubstringMatcher); assertTrue(Matchers.createMatcher("mos*kito*") instanceof EndsWithMatcher); assertTrue(Matchers.createMatcher("*mos*kito") instanceof StartsWithMatcher); assertTrue(Matchers.createMatcher("mosk*ito") instanceof EqualsMatcher); }
@Test public void shouldMatch() { writeFile( "A.java", "public class A {", " public String testToString() {", " Object obj = new Object();", " return obj.toString();", " }", "}"); assertCompiles(methodInvocationMatches(true, Matchers.methodReturnsNonNull())); }
@Test public void shouldNotMatchWhenMethodNameDiffers() { writeFile( "A.java", "public class A {", " public String ToString() {", " return \"match should be case sensitive\";", " }", " public String testMethodWithDifferentCase() {", " return ToString();", " }", "}"); assertCompiles(methodInvocationMatches(false, Matchers.methodReturnsNonNull())); }
@Test public void shouldMatchBareOverride() { writeFile( "A.java", "public class A {", " public String toString() {", " return \"a string\";", " }", " public String testToString() {", " return toString();", " }", "}"); assertCompiles(methodInvocationMatches(true, Matchers.methodReturnsNonNull())); }
@Test public void recruitersCanDisplayJobsTheyPosted() { setupActors(); JobRepository jobRepository = new JobRepository(); JobPosting developerPosting = recruiter.post(developerJob).to(jobRepository); JobPosting architectPosting = recruiter.post(architectJob).to(jobRepository); JobPosting programmerPosting = recruiter.post(programmerJob).to(jobRepository); JobPostings jobPostings = recruiter.getPostedJobs().from(jobRepository); JobPostingsDisplayer postingsDisplayer = Mockito.mock(JobPostingsDisplayer.class); jobPostings.displayTo(postingsDisplayer); Mockito.verify(postingsDisplayer) .displayJobPostings( Matchers.argThat( new SetOfThreeJobPostings(developerPosting, architectPosting, programmerPosting))); }
@Test public void shouldMatchDescendants() { writeFile( "A.java", "public class A {", " public String testThisToString() {", " return toString();", " }", " public String testInstanceToString() {", " Boolean b = new Boolean(true);", " return b.toString();", " }", " public String testStringToString() {", " String str = \"a string\";", " return str.toString();", " }", "}"); assertCompiles(methodInvocationMatches(true, Matchers.methodReturnsNonNull())); }
public static URL getResourceUrl(String resource) { URL url = ClassLoader.getSystemResource(getResourceName(resource)); assertThat("Failed to find test resource " + resource, url, Matchers.notNullValue()); return url; }