Best Mockito code snippet using org.mockito.ArgumentMatcher
Source:MatchersMixin.java
...3import java.util.List; 4import java.util.Map; 5import java.util.Set; 6import java.util.regex.Pattern; 7import org.mockito.ArgumentMatcher; 8import org.mockito.ArgumentMatchers; 9/** 10 * Wrapper of static elements in org.mockito.ArgumentMatchers11 * Generated by Interface-It: https://github.com/aro-tech/interface-it12 * {@link org.mockito.ArgumentMatchers}13 */14public interface MatchersMixin {15 // CONSTANTS: 16 // DELEGATE METHODS: 17 /**18 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.any()19 * {@link org.mockito.ArgumentMatchers#any()}20 */21 default <T> T any() {22 return ArgumentMatchers.any();23 }24 /**25 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.any(java.lang.Class<T>)26 * {@link org.mockito.ArgumentMatchers#any(java.lang.Class)}27 */28 default <T> T any(Class<T> type) {29 return ArgumentMatchers.any(type);30 }31 /**32 * Delegate call to public static boolean org.mockito.ArgumentMatchers.anyBoolean()33 * {@link org.mockito.ArgumentMatchers#anyBoolean()}34 */35 default boolean anyBoolean() {36 return ArgumentMatchers.anyBoolean();37 }38 /**39 * Delegate call to public static byte org.mockito.ArgumentMatchers.anyByte()40 * {@link org.mockito.ArgumentMatchers#anyByte()}41 */42 default byte anyByte() {43 return ArgumentMatchers.anyByte();44 }45 /**46 * Delegate call to public static char org.mockito.ArgumentMatchers.anyChar()47 * {@link org.mockito.ArgumentMatchers#anyChar()}48 */49 default char anyChar() {50 return ArgumentMatchers.anyChar();51 }52 /**53 * Delegate call to public static <T> java.util.Collection<T> org.mockito.ArgumentMatchers.anyCollection()54 * {@link org.mockito.ArgumentMatchers#anyCollection()}55 */56 default <T> Collection<T> anyCollection() {57 return ArgumentMatchers.anyCollection();58 }59 /**60 * Delegate call to public static double org.mockito.ArgumentMatchers.anyDouble()61 * {@link org.mockito.ArgumentMatchers#anyDouble()}62 */63 default double anyDouble() {64 return ArgumentMatchers.anyDouble();65 }66 /**67 * Delegate call to public static float org.mockito.ArgumentMatchers.anyFloat()68 * {@link org.mockito.ArgumentMatchers#anyFloat()}69 */70 default float anyFloat() {71 return ArgumentMatchers.anyFloat();72 }73 /**74 * Delegate call to public static int org.mockito.ArgumentMatchers.anyInt()75 * {@link org.mockito.ArgumentMatchers#anyInt()}76 */77 default int anyInt() {78 return ArgumentMatchers.anyInt();79 }80 /**81 * Delegate call to public static <T> java.lang.Iterable<T> org.mockito.ArgumentMatchers.anyIterable()82 * {@link org.mockito.ArgumentMatchers#anyIterable()}83 */84 default <T> Iterable<T> anyIterable() {85 return ArgumentMatchers.anyIterable();86 }87 /**88 * Delegate call to public static <T> java.util.List<T> org.mockito.ArgumentMatchers.anyList()89 * {@link org.mockito.ArgumentMatchers#anyList()}90 */91 default <T> List<T> anyList() {92 return ArgumentMatchers.anyList();93 }94 /**95 * Delegate call to public static long org.mockito.ArgumentMatchers.anyLong()96 * {@link org.mockito.ArgumentMatchers#anyLong()}97 */98 default long anyLong() {99 return ArgumentMatchers.anyLong();100 }101 /**102 * Delegate call to public static <K,V> java.util.Map<K, V> org.mockito.ArgumentMatchers.anyMap()103 * {@link org.mockito.ArgumentMatchers#anyMap()}104 */105 default <K,V> Map<K, V> anyMap() {106 return ArgumentMatchers.anyMap();107 }108 /**109 * Delegate call to public static <T> java.util.Set<T> org.mockito.ArgumentMatchers.anySet()110 * {@link org.mockito.ArgumentMatchers#anySet()}111 */112 default <T> Set<T> anySet() {113 return ArgumentMatchers.anySet();114 }115 /**116 * Delegate call to public static short org.mockito.ArgumentMatchers.anyShort()117 * {@link org.mockito.ArgumentMatchers#anyShort()}118 */119 default short anyShort() {120 return ArgumentMatchers.anyShort();121 }122 /**123 * Delegate call to public static java.lang.String org.mockito.ArgumentMatchers.anyString()124 * {@link org.mockito.ArgumentMatchers#anyString()}125 */126 default String anyString() {127 return ArgumentMatchers.anyString();128 }129 /**130 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.argThat(org.mockito.ArgumentMatcher<T>)131 * {@link org.mockito.ArgumentMatchers#argThat(org.mockito.ArgumentMatcher)}132 */133 default <T> T argThat(ArgumentMatcher<T> matcher) {134 return ArgumentMatchers.argThat(matcher);135 }136 /**137 * Delegate call to public static boolean org.mockito.ArgumentMatchers.booleanThat(org.mockito.ArgumentMatcher<java.lang.Boolean>)138 * {@link org.mockito.ArgumentMatchers#booleanThat(org.mockito.ArgumentMatcher)}139 */140 default boolean booleanThat(ArgumentMatcher<Boolean> matcher) {141 return ArgumentMatchers.booleanThat(matcher);142 }143 /**144 * Delegate call to public static byte org.mockito.ArgumentMatchers.byteThat(org.mockito.ArgumentMatcher<java.lang.Byte>)145 * {@link org.mockito.ArgumentMatchers#byteThat(org.mockito.ArgumentMatcher)}146 */147 default byte byteThat(ArgumentMatcher<Byte> matcher) {148 return ArgumentMatchers.byteThat(matcher);149 }150 /**151 * Delegate call to public static char org.mockito.ArgumentMatchers.charThat(org.mockito.ArgumentMatcher<java.lang.Character>)152 * {@link org.mockito.ArgumentMatchers#charThat(org.mockito.ArgumentMatcher)}153 */154 default char charThat(ArgumentMatcher<Character> matcher) {155 return ArgumentMatchers.charThat(matcher);156 }157 /**158 * Delegate call to public static java.lang.String org.mockito.ArgumentMatchers.contains(java.lang.String)159 * {@link org.mockito.ArgumentMatchers#contains(java.lang.String)}160 */161 default String contains(String substring) {162 return ArgumentMatchers.contains(substring);163 }164 /**165 * Delegate call to public static double org.mockito.ArgumentMatchers.doubleThat(org.mockito.ArgumentMatcher<java.lang.Double>)166 * {@link org.mockito.ArgumentMatchers#doubleThat(org.mockito.ArgumentMatcher)}167 */168 default double doubleThat(ArgumentMatcher<Double> matcher) {169 return ArgumentMatchers.doubleThat(matcher);170 }171 /**172 * Delegate call to public static java.lang.String org.mockito.ArgumentMatchers.endsWith(java.lang.String)173 * {@link org.mockito.ArgumentMatchers#endsWith(java.lang.String)}174 */175 default String endsWith(String suffix) {176 return ArgumentMatchers.endsWith(suffix);177 }178 /**179 * Delegate call to public static boolean org.mockito.ArgumentMatchers.eq(boolean)180 * {@link org.mockito.ArgumentMatchers#eq(boolean)}181 */182 default boolean eq(boolean value) {183 return ArgumentMatchers.eq(value);184 }185 /**186 * Delegate call to public static byte org.mockito.ArgumentMatchers.eq(byte)187 * {@link org.mockito.ArgumentMatchers#eq(byte)}188 */189 default byte eq(byte value) {190 return ArgumentMatchers.eq(value);191 }192 /**193 * Delegate call to public static char org.mockito.ArgumentMatchers.eq(char)194 * {@link org.mockito.ArgumentMatchers#eq(char)}195 */196 default char eq(char value) {197 return ArgumentMatchers.eq(value);198 }199 /**200 * Delegate call to public static double org.mockito.ArgumentMatchers.eq(double)201 * {@link org.mockito.ArgumentMatchers#eq(double)}202 */203 default double eq(double value) {204 return ArgumentMatchers.eq(value);205 }206 /**207 * Delegate call to public static float org.mockito.ArgumentMatchers.eq(float)208 * {@link org.mockito.ArgumentMatchers#eq(float)}209 */210 default float eq(float value) {211 return ArgumentMatchers.eq(value);212 }213 /**214 * Delegate call to public static int org.mockito.ArgumentMatchers.eq(int)215 * {@link org.mockito.ArgumentMatchers#eq(int)}216 */217 default int eq(int value) {218 return ArgumentMatchers.eq(value);219 }220 /**221 * Delegate call to public static long org.mockito.ArgumentMatchers.eq(long)222 * {@link org.mockito.ArgumentMatchers#eq(long)}223 */224 default long eq(long value) {225 return ArgumentMatchers.eq(value);226 }227 /**228 * Delegate call to public static short org.mockito.ArgumentMatchers.eq(short)229 * {@link org.mockito.ArgumentMatchers#eq(short)}230 */231 default short eq(short value) {232 return ArgumentMatchers.eq(value);233 }234 /**235 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.eq(T)236 * {@link org.mockito.ArgumentMatchers#eq(java.lang.Object)}237 */238 default <T> T eq(T value) {239 return ArgumentMatchers.eq(value);240 }241 /**242 * Delegate call to public static float org.mockito.ArgumentMatchers.floatThat(org.mockito.ArgumentMatcher<java.lang.Float>)243 * {@link org.mockito.ArgumentMatchers#floatThat(org.mockito.ArgumentMatcher)}244 */245 default float floatThat(ArgumentMatcher<Float> matcher) {246 return ArgumentMatchers.floatThat(matcher);247 }248 /**249 * Delegate call to public static int org.mockito.ArgumentMatchers.intThat(org.mockito.ArgumentMatcher<java.lang.Integer>)250 * {@link org.mockito.ArgumentMatchers#intThat(org.mockito.ArgumentMatcher)}251 */252 default int intThat(ArgumentMatcher<Integer> matcher) {253 return ArgumentMatchers.intThat(matcher);254 }255 /**256 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.isA(java.lang.Class<T>)257 * {@link org.mockito.ArgumentMatchers#isA(java.lang.Class)}258 */259 default <T> T isA(Class<T> type) {260 return ArgumentMatchers.isA(type);261 }262 /**263 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.isNotNull()264 * {@link org.mockito.ArgumentMatchers#isNotNull()}265 */266 default <T> T isNotNull() {267 return ArgumentMatchers.isNotNull();268 }269 /**270 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.isNull()271 * {@link org.mockito.ArgumentMatchers#isNull()}272 */273 default <T> T isNull() {274 return ArgumentMatchers.isNull();275 }276 /**277 * Delegate call to public static long org.mockito.ArgumentMatchers.longThat(org.mockito.ArgumentMatcher<java.lang.Long>)278 * {@link org.mockito.ArgumentMatchers#longThat(org.mockito.ArgumentMatcher)}279 */280 default long longThat(ArgumentMatcher<Long> matcher) {281 return ArgumentMatchers.longThat(matcher);282 }283 /**284 * Delegate call to public static java.lang.String org.mockito.ArgumentMatchers.matches(java.util.regex.Pattern)285 * {@link org.mockito.ArgumentMatchers#matches(java.util.regex.Pattern)}286 */287 default String matches(Pattern pattern) {288 return ArgumentMatchers.matches(pattern);289 }290 /**291 * Delegate call to public static java.lang.String org.mockito.ArgumentMatchers.matches(java.lang.String)292 * {@link org.mockito.ArgumentMatchers#matches(java.lang.String)}293 */294 default String matches(String regex) {295 return ArgumentMatchers.matches(regex);296 }297 /**298 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.notNull()299 * {@link org.mockito.ArgumentMatchers#notNull()}300 */301 default <T> T notNull() {302 return ArgumentMatchers.notNull();303 }304 /**305 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.nullable(java.lang.Class<T>)306 * {@link org.mockito.ArgumentMatchers#nullable(java.lang.Class)}307 */308 default <T> T nullable(Class<T> clazz) {309 return ArgumentMatchers.nullable(clazz);310 }311 /**312 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.refEq(T,java.lang.String...)313 * {@link org.mockito.ArgumentMatchers#refEq(java.lang.Object,java.lang.String[])}314 */315 default <T> T refEq(T value, String... excludeFields) {316 return ArgumentMatchers.refEq(value, excludeFields);317 }318 /**319 * Delegate call to public static <T> T org.mockito.ArgumentMatchers.same(T)320 * {@link org.mockito.ArgumentMatchers#same(java.lang.Object)}321 */322 default <T> T same(T value) {323 return ArgumentMatchers.same(value);324 }325 /**326 * Delegate call to public static short org.mockito.ArgumentMatchers.shortThat(org.mockito.ArgumentMatcher<java.lang.Short>)327 * {@link org.mockito.ArgumentMatchers#shortThat(org.mockito.ArgumentMatcher)}328 */329 default short shortThat(ArgumentMatcher<Short> matcher) {330 return ArgumentMatchers.shortThat(matcher);331 }332 /**333 * Delegate call to public static java.lang.String org.mockito.ArgumentMatchers.startsWith(java.lang.String)334 * {@link org.mockito.ArgumentMatchers#startsWith(java.lang.String)}335 */336 default String startsWith(String prefix) {337 return ArgumentMatchers.startsWith(prefix);338 }339}...
Source:CustomerServiceImplMockTest.java
...14import java.util.Arrays;15import java.util.List;16import java.util.Objects;17import static org.junit.jupiter.api.Assertions.*;18import static org.mockito.ArgumentMatchers.argThat;19import static org.mockito.ArgumentMatchers.eq;20import static org.mockito.Mockito.times;21@ExtendWith(MockitoExtension.class)22public class CustomerServiceImplMockTest {23 @InjectMocks24 private CustomerDaoJdbc customerDaoJdbc;25 @Mock26 private NamedParameterJdbcTemplate namedParameterJdbcTemplate;27 @Captor28 private ArgumentCaptor<RowMapper<Customer>> captor;29 @Test30 public void findAllTest() {31 String sql = "select";32 ReflectionTestUtils.setField(customerDaoJdbc, "selectSql", sql);33 Customer customer1 = new Customer();34 Customer customer2 = new Customer();35 List<Customer> customerList = Arrays.asList(customer1, customer2);36 Mockito.when(namedParameterJdbcTemplate.query(eq(sql), ArgumentMatchers.<RowMapper<Customer>>any())).thenReturn(customerList);37 List<Customer> customers = customerDaoJdbc.findAll();38 assertNotNull(customers);39 assertFalse(customers.isEmpty());40 assertEquals(2, customers.size());41 assertSame(customers.get(0), customer1);42 assertSame(customers.get(1), customer2);43 Mockito.verify(namedParameterJdbcTemplate).query(eq(sql), captor.capture());44 RowMapper<Customer> mapper = captor.getValue();45 assertNotNull(mapper);46 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);47 }48 @Test49 public void findByLoginTest() {50 Customer testCustomer = new Customer();51 ReflectionTestUtils.setField(customerDaoJdbc, "findByLoginSql", "findByLogin");52 Mockito.when(namedParameterJdbcTemplate.queryForObject(eq("findByLogin"),53 argThat(new ArgumentMatcher<SqlParameterSource>() {54 @Override55 public boolean matches(SqlParameterSource sqlParameterSource) {56 return sqlParameterSource.getParameterNames()[0].equals("CUSTOMER_LOGIN");57 }58 })59 , ArgumentMatchers.<RowMapper<Customer>>any())).thenReturn(testCustomer);60// Mockito.when(namedParameterJdbcTemplate.queryForObject(eq("findByLogin"),61// argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> sqlParameterSource.getParameterNames()[0].equals("CUSTOMER_LOGIN"))62// , ArgumentMatchers.<RowMapper<Customer>>any())).thenReturn(testCustomer);63// Mockito.when(namedParameterJdbcTemplate.queryForObject(eq("findByLogin"),64// argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("CUSTOMER_LOGIN"))65// , ArgumentMatchers.<RowMapper<Customer>>any())).thenReturn(testCustomer);66 Customer customer = customerDaoJdbc.findByLogin("").get();67 assertNotNull(customer);68 assertSame(testCustomer, customer);69 Mockito.verify(namedParameterJdbcTemplate).queryForObject(eq("findByLogin"), ArgumentMatchers.<SqlParameterSource>any(), ArgumentMatchers.<RowMapper<Customer>>any());70 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);71 }72 @Test73 public void findByIdTest() {74 Customer testCustomer = new Customer();75 testCustomer.setCustomerId(1);76 ReflectionTestUtils.setField(customerDaoJdbc, "findByIdSql", "findById");77 Mockito.when(namedParameterJdbcTemplate.queryForObject(78 eq("findById"),79 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("CUSTOMER_ID")),80 ArgumentMatchers.<RowMapper<Customer>>any())).thenReturn(testCustomer);81 Customer customer = customerDaoJdbc.findById(1).get();82 assertNotNull(customer);83 assertSame(testCustomer, customer);84 Mockito.verify(namedParameterJdbcTemplate).queryForObject(eq("findById"), ArgumentMatchers.<SqlParameterSource>any(), ArgumentMatchers.<RowMapper<Customer>>any());85 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);86 }87 @Test88 public void searchCustomersByLoginTest() {89 String sql = "searchCustomersByLogin";90 ReflectionTestUtils.setField(customerDaoJdbc, "searchCustomersByLoginSql", sql);91 Customer customer1 = new Customer();92 Customer customer2 = new Customer();93 List<Customer> customerList = Arrays.asList(customer1, customer2);94 ArgumentMatcher<SqlParameterSource> argumentMatcher = sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("FILTER");95 Mockito.when(namedParameterJdbcTemplate.query(eq(sql), argThat(argumentMatcher), ArgumentMatchers.<RowMapper<Customer>>any())).thenReturn(customerList);96 List<Customer> customers = customerDaoJdbc.searchCustomersByLogin("");97 assertNotNull(customers);98 assertFalse(customers.isEmpty());99 assertEquals(2, customers.size());100 assertSame(customers.get(0), customer1);101 assertSame(customers.get(1), customer2);102 Mockito.verify(namedParameterJdbcTemplate).query(eq(sql), argThat(argumentMatcher), ArgumentMatchers.<RowMapper<Customer>>any());103 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);104 }105 @Test106 public void createTest() {107 Customer customer = new Customer("testLogin", "testPassword", Role.ROLE_USER, true);108 ReflectionTestUtils.setField(customerDaoJdbc, "createSql", "create");109 ReflectionTestUtils.setField(customerDaoJdbc, "checkLoginSql", "checkLogin");110 ArgumentMatcher<MapSqlParameterSource> argumentMatcher = sqlParameterSource ->111 Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("LOGIN") &&112 Objects.requireNonNull(sqlParameterSource.getParameterNames())[1].equals("PASSWORD") &&113 Objects.requireNonNull(sqlParameterSource.getParameterNames())[2].equals("ROLE") &&114 Objects.requireNonNull(sqlParameterSource.getParameterNames())[3].equals("IS_ACTUAL");115 Mockito.when(namedParameterJdbcTemplate.queryForObject(eq("checkLogin"),116 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("LOGIN")),117 eq(Integer.class))).118 thenReturn(0);119 assertTrue(customerDaoJdbc.isLoginUnique(customer));120 Mockito.when(namedParameterJdbcTemplate.update(eq("create"), argThat(argumentMatcher))).thenReturn(1);121 assertEquals(1, customerDaoJdbc.create(customer));122 Mockito.verify(namedParameterJdbcTemplate, times(2)).queryForObject(eq("checkLogin"),123 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("LOGIN")),124 eq(Integer.class));125 Mockito.verify(namedParameterJdbcTemplate).update(eq("create"), argThat(argumentMatcher));126 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);127 }128 @Test129 public void updateTest() {130 Customer customer = new Customer("testLogin", "testPassword", Role.ROLE_USER, true);131 ReflectionTestUtils.setField(customerDaoJdbc, "updateSql", "update");132 ArgumentMatcher<MapSqlParameterSource> argumentMatcher = sqlParameterSource ->133 Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("CUSTOMER_ID") &&134 Objects.requireNonNull(sqlParameterSource.getParameterNames())[1].equals("LOGIN") &&135 Objects.requireNonNull(sqlParameterSource.getParameterNames())[2].equals("PASSWORD") &&136 Objects.requireNonNull(sqlParameterSource.getParameterNames())[3].equals("ROLE") &&137 Objects.requireNonNull(sqlParameterSource.getParameterNames())[4].equals("IS_ACTUAL");138 Mockito.when(namedParameterJdbcTemplate.update(eq("update"), argThat(argumentMatcher))).thenReturn(1);139 assertEquals(1, customerDaoJdbc.update(customer));140 Mockito.verify(namedParameterJdbcTemplate).update(eq("update"), argThat(argumentMatcher));141 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);142 }143 @Test144 public void deleteTest() {145 Customer testCustomer = new Customer();146 testCustomer.setCustomerId(1);147 testCustomer.setRole(Role.ROLE_USER);148 ReflectionTestUtils.setField(customerDaoJdbc, "findByIdSql", "findById");149 Mockito.when(namedParameterJdbcTemplate.queryForObject(150 eq("findById"),151 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("CUSTOMER_ID")),152 ArgumentMatchers.<RowMapper<Customer>>any())).thenReturn(testCustomer);153 Customer customer = customerDaoJdbc.findById(1).get();154 assertNotNull(customer);155 assertSame(testCustomer, customer);156 Mockito.verify(namedParameterJdbcTemplate).queryForObject(eq("findById"), ArgumentMatchers.<SqlParameterSource>any(), ArgumentMatchers.<RowMapper<Customer>>any());157 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);158 ReflectionTestUtils.setField(customerDaoJdbc, "deleteAdminSql", "deleteAdmin");159 ReflectionTestUtils.setField(customerDaoJdbc, "deleteCustomerSql", "deleteCustomer");160 Mockito.when(namedParameterJdbcTemplate.update(161 eq("deleteCustomer"),162 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("ID")))).163 thenReturn(1);164 Integer result = customerDaoJdbc.delete(1);165 assertNotNull(result);166 assertEquals(1, result);167 Mockito.verify(namedParameterJdbcTemplate).update(eq("deleteCustomer"),168 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("ID")));169 customer = customerDaoJdbc.findById(1).get();170 assertNotNull(customer);171 assertSame(testCustomer, customer);172 testCustomer.setRole(Role.ROLE_ADMIN);173 Mockito.when(namedParameterJdbcTemplate.update(174 eq("deleteAdmin"),175 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("ID")))).176 thenReturn(1);177 result = customerDaoJdbc.delete(1);178 assertNotNull(result);179 assertEquals(1, result);180 Mockito.verify(namedParameterJdbcTemplate).update(eq("deleteAdmin"),181 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("ID")));182 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);183 }184}...
Source:ProductServiceImplMockTest.java
...14import java.util.Collections;15import java.util.List;16import java.util.Objects;17import static org.junit.jupiter.api.Assertions.*;18import static org.mockito.ArgumentMatchers.argThat;19import static org.mockito.ArgumentMatchers.eq;20import static org.mockito.Mockito.times;21@ExtendWith(MockitoExtension.class)22public class ProductServiceImplMockTest {23 @InjectMocks24 private ProductDaoJdbc productDaoJdbc;25 @Mock26 private NamedParameterJdbcTemplate namedParameterJdbcTemplate;27 @Test28 public void findAllTest() {29 String sql = "select";30 ReflectionTestUtils.setField(productDaoJdbc, "selectAllSql", sql);31 Product product1 = new Product();32 Product product2 = new Product();33 List<Product> productList = Arrays.asList(product1, product2);34 Mockito.when(namedParameterJdbcTemplate.query(eq(sql), ArgumentMatchers.<RowMapper<Product>>any())).thenReturn(productList);35 List<Product> products = productDaoJdbc.findAll();36 assertNotNull(products);37 assertFalse(products.isEmpty());38 assertEquals(2, products.size());39 assertSame(products.get(0), product1);40 assertSame(products.get(1), product2);41 assertSame(products, productList);42 Mockito.verify(namedParameterJdbcTemplate).query(eq(sql), ArgumentMatchers.<RowMapper<Product>>any());43 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);44 }45 @Test46 public void findByIdTest() {47 Product testProduct = new Product();48 testProduct.setProductId(1);49 List<Product> products = Collections.singletonList(testProduct);50 ReflectionTestUtils.setField(productDaoJdbc, "selectByIdSql", "selectById");51 Mockito.when(namedParameterJdbcTemplate.query(52 eq("selectById"),53 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("ID")),54 ArgumentMatchers.<RowMapper<Product>>any())).thenReturn(products);55 Product product = productDaoJdbc.findById(1);56 assertNotNull(product);57 assertSame(testProduct, product);58 Mockito.verify(namedParameterJdbcTemplate).query(eq("selectById"), ArgumentMatchers.<SqlParameterSource>any(), ArgumentMatchers.<RowMapper<Product>>any());59 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);60 }61 @Test62 public void findProductsByDescriptionTest() {63 String sql = "selectByDescription";64 ReflectionTestUtils.setField(productDaoJdbc, "selectByDescriptionSql", sql);65 Product product1 = new Product();66 Product product2 = new Product();67 List<Product> productList = Arrays.asList(product1, product2);68 ArgumentMatcher<SqlParameterSource> argumentMatcher = sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("FILTER");69 Mockito.when(namedParameterJdbcTemplate.query(eq(sql), argThat(argumentMatcher), ArgumentMatchers.<RowMapper<Product>>any())).thenReturn(productList);70 List<Product> products = productDaoJdbc.findProductsByDescription("");71 assertNotNull(products);72 assertFalse(products.isEmpty());73 assertEquals(2, products.size());74 assertSame(products.get(0), product1);75 assertSame(products.get(1), product2);76 assertSame(products, productList);77 Mockito.verify(namedParameterJdbcTemplate).query(eq(sql), argThat(argumentMatcher), ArgumentMatchers.<RowMapper<Product>>any());78 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);79 }80 @Test81 public void createTest() {82 Product product = new Product();83 ReflectionTestUtils.setField(productDaoJdbc, "createSql", "create");84 ReflectionTestUtils.setField(productDaoJdbc, "checkShortDescriptionSql", "checkShortDescription");85 ArgumentMatcher<MapSqlParameterSource> argumentMatcher = sqlParameterSource ->86 Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("PICTURE") &&87 Objects.requireNonNull(sqlParameterSource.getParameterNames())[1].equals("SHORT_DESCRIPTION") &&88 Objects.requireNonNull(sqlParameterSource.getParameterNames())[2].equals("DETAIL_DESCRIPTION") &&89 Objects.requireNonNull(sqlParameterSource.getParameterNames())[3].equals("PRICE") &&90 Objects.requireNonNull(sqlParameterSource.getParameterNames())[4].equals("CHANGED_BY");91 Mockito.when(namedParameterJdbcTemplate.queryForObject(eq("checkShortDescription"),92 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("SHORT_DESCRIPTION")),93 eq(Integer.class))).94 thenReturn(0);95 assertTrue(productDaoJdbc.isShortDescriptionUnique(product));96 Mockito.when(namedParameterJdbcTemplate.update(eq("create"), argThat(argumentMatcher))).thenReturn(1);97 assertEquals(1, productDaoJdbc.create(product));98 Mockito.verify(namedParameterJdbcTemplate, times(2)).queryForObject(eq("checkShortDescription"),99 argThat((ArgumentMatcher<SqlParameterSource>) sqlParameterSource -> Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("SHORT_DESCRIPTION")),100 eq(Integer.class));101 Mockito.verify(namedParameterJdbcTemplate).update(eq("create"), argThat(argumentMatcher));102 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);103 }104 @Test105 public void updateTest() {106 Product product = new Product();107 ReflectionTestUtils.setField(productDaoJdbc, "updateSql", "update");108 ArgumentMatcher<MapSqlParameterSource> argumentMatcher = sqlParameterSource ->109 Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("PRODUCT_ID") &&110 Objects.requireNonNull(sqlParameterSource.getParameterNames())[1].equals("PICTURE") &&111 Objects.requireNonNull(sqlParameterSource.getParameterNames())[2].equals("SHORT_DESCRIPTION") &&112 Objects.requireNonNull(sqlParameterSource.getParameterNames())[3].equals("DETAIL_DESCRIPTION") &&113 Objects.requireNonNull(sqlParameterSource.getParameterNames())[4].equals("PRICE") &&114 Objects.requireNonNull(sqlParameterSource.getParameterNames())[5].equals("CHANGED_BY");115 Mockito.when(namedParameterJdbcTemplate.update(eq("update"), argThat(argumentMatcher))).thenReturn(1);116 assertEquals(1, productDaoJdbc.update(product));117 Mockito.verify(namedParameterJdbcTemplate).update(eq("update"), argThat(argumentMatcher));118 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);119 }120 @Test121 public void deleteTest() {122 ReflectionTestUtils.setField(productDaoJdbc, "deleteSql", "delete");123 ArgumentMatcher<MapSqlParameterSource> argumentMatcher = sqlParameterSource ->124 Objects.requireNonNull(sqlParameterSource.getParameterNames())[0].equals("ID");125 Mockito.when(namedParameterJdbcTemplate.update(eq("delete"), argThat(argumentMatcher))).thenReturn(1);126 assertEquals(1, productDaoJdbc.delete(1));127 Mockito.verify(namedParameterJdbcTemplate).update(eq("delete"), argThat(argumentMatcher));128 Mockito.verifyNoMoreInteractions(namedParameterJdbcTemplate);129 }130}...
ArgumentMatcher
Using AI Code Generation
1import static org.mockito.Matchers.argThat;2import static org.mockito.Mockito.mock;3import static org.mockito.Mockito.verify;4import static org.mockito.Mockito.when;5import org.junit.Test;6import org.mockito.ArgumentMatcher;7import org.mockito.internal.matchers.Equals;8public class TestClass {9 public void test() {10 Comparable c= mock(Comparable.class);11 when(c.compareTo(argThat(new Equals(5)))).thenReturn(1);12 System.out.println(c.compareTo(5));13 verify(c).compareTo(argThat(new Equals(5)));14 }15}16ArgumentCaptor.capture()17ArgumentCaptor.getValue()18ArgumentCaptor.getAllValues()
ArgumentMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2import org.mockito.Mockito;3import org.mockito.invocation.InvocationOnMock;4import org.mockito.stubbing.Answer;5import java.util.List;6public class ArgumentMatcherExample {7public static void main(String[] args) {8List mock = Mockito.mock(List.class);9Mockito.when(mock.get(Mockito.anyInt())).thenReturn("element");10Mockito.when(mock.contains(argThat(isValid()))).thenReturn(true);11System.out.println(mock.get(999));12Mockito.verify(mock).get(Mockito.anyInt());13Mockito.when(mock.contains(argThat(s -> s.length() > 5))).thenReturn(true);14}15private static ArgumentMatcher<String> isValid() {16return new ArgumentMatcher<String>() {17public boolean matches(String argument) {18return argument.length() > 5;19}20};21}22}
ArgumentMatcher
Using AI Code Generation
1import org.junit.Test;2import org.mockito.ArgumentMatcher;3import org.mockito.ArgumentMatchers;4import org.mockito.Mockito;5import java.util.ArrayList;6import java.util.List;7import static org.junit.Assert.assertTrue;8import static org.mockito.Mockito.*;9public class MockitoTest {10 public void testMockito(){11 List<String> mockList = mock(List.class);12 when(mockList.get(anyInt())).thenReturn("Hello");13 when(mockList.get(2)).thenReturn("World");14 String value = mockList.get(1);15 String value2 = mockList.get(2);16 System.out.println(value);17 System.out.println(value2);18 assertTrue(value.equals("Hello"));19 assertTrue(value2.equals("World"));20 }21}
ArgumentMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2import org.mockito.Mockito;3import static org.mockito.Mockito.*;4public class 1 {5 public static void main(String[] args) {6 ArgumentMatcher<String> matcher = new ArgumentMatcher<String>() {7 public boolean matches(String argument) {8 return argument.startsWith("hello");9 }10 };11 List<String> mockedList = mock(List.class);12 when(mockedList.get(anyInt())).thenReturn("hello");13 when(mockedList.contains(argThat(matcher))).thenReturn(true);14 System.out.println(mockedList.get(1));15 System.out.println(mockedList.contains("hello world"));16 System.out.println(mockedList.contains("bye"));17 }18}19import org.mockito.ArgumentCaptor;20import org.mockito.Mockito;21import static org.mockito.Mockito.*;22public class 2 {23 public static void main(String[] args) {24 List<String> mockedList = mock(List.class);25 when(mockedList.get(anyInt())).thenReturn("hello");26 mockedList.get(1);27 mockedList.get(2);28 mockedList.get(3);29 ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);30 verify(mockedList, times(3)).get(captor.capture());31 List<Integer> allValues = captor.getAllValues();32 System.out.println(allValues);33 }34}35import org.mockito.ArgumentCaptor;36import org.mockito.Mockito;37import static org.mockito.Mockito.*;38public class 3 {39 public static void main(String[] args) {40 List<String> mockedList = mock(List.class);41 when(mockedList.get(anyInt())).thenReturn("hello");42 mockedList.get(1);43 mockedList.get(2);44 mockedList.get(3);45 ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);46 verify(mockedList, times(3)).get(captor.capture());47 int firstValue = captor.getValue();48 System.out.println(firstValue);49 }50}51import org.mockito.ArgumentCaptor;52import org.mockito.Mockito;53import static org.mockito.Mockito.*;54public class 4 {
ArgumentMatcher
Using AI Code Generation
1package com.automation;2import java.util.List;3import org.junit.Test;4import static org.junit.Assert.assertEquals;5import static org.mockito.Mockito.mock;6import static org.mockito.Mockito.when;7import static org.mockito.Matchers.argThat;8import org.mockito.ArgumentMatcher;9public class ArgumentMatcherTest {10 public void testArgumentMatcher() {11 List mockedList = mock(List.class);12 when(mockedList.get(argThat(new IsValid()))).thenReturn("valid");13 assertEquals("valid", mockedList.get(1));14 assertEquals("valid", mockedList.get(5));15 assertEquals("valid", mockedList.get(9));16 }17}18class IsValid extends ArgumentMatcher {19 public boolean matches(Object o) {20 Integer i = (Integer) o;21 return (i > 0 && i < 10);22 }23}
ArgumentMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2import static org.mockito.Mockito.*;3import org.junit.Test;4import static org.junit.Assert.*;5import static org.hamcrest.CoreMatchers.*;6public class Test1 {7 public void test1() {8 Comparable c = mock(Comparable.class);9 when(c.compareTo(anyInt())).thenReturn(-1);10 System.out.println(c.compareTo(9));11 }12}13import org.mockito.ArgumentMatcher;14import static org.mockito.Mockito.*;15import org.junit.Test;16import static org.junit.Assert.*;17import static org.hamcrest.CoreMatchers.*;18public class Test1 {19 public void test1() {20 Comparable c = mock(Comparable.class);21 when(c.compareTo(anyInt())).thenReturn(-1);22 System.out.println(c.compareTo(9));23 }24}25import org.mockito.ArgumentMatcher;26import static org.mockito.Mockito.*;27import org.junit.Test;28import static org.junit.Assert.*;29import static org.hamcrest.CoreMatchers.*;30public class Test1 {31 public void test1() {32 Comparable c = mock(Comparable.class);33 System.out.println(c.compareTo(9));34 }35}36import org.mockito.ArgumentMatcher;37import static org.mockito.Mockito.*;38import org.junit.Test;39import static org.junit.Assert.*;40import static org.hamcrest.CoreMatchers.*;41public class Test1 {42 public void test1() {43 Comparable c = mock(Comparable.class);
ArgumentMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2public class MyArgumentMatcher implements ArgumentMatcher<String> {3 public boolean matches(String argument) {4 return argument.startsWith("foo");5 }6}7import org.mockito.ArgumentMatcher;8public class MyArgumentMatcher implements ArgumentMatcher<String> {9 public boolean matches(String argument) {10 return argument.startsWith("foo");11 }12}13import org.mockito.ArgumentMatcher;14public class MyArgumentMatcher implements ArgumentMatcher<String> {15 public boolean matches(String argument) {16 return argument.startsWith("foo");17 }18}19import org.mockito.ArgumentMatcher;20public class MyArgumentMatcher implements ArgumentMatcher<String> {21 public boolean matches(String argument) {22 return argument.startsWith("foo");23 }24}25import org.mockito.ArgumentMatcher;26public class MyArgumentMatcher implements ArgumentMatcher<String> {27 public boolean matches(String argument) {28 return argument.startsWith("foo");29 }30}31import org.mockito.ArgumentMatcher;32public class MyArgumentMatcher implements ArgumentMatcher<String> {33 public boolean matches(String argument) {34 return argument.startsWith("foo");35 }36}37import org.mockito.ArgumentMatcher;38public class MyArgumentMatcher implements ArgumentMatcher<String> {39 public boolean matches(String argument) {40 return argument.startsWith("foo");41 }42}43import org.mockito.ArgumentMatcher;44public class MyArgumentMatcher implements ArgumentMatcher<String> {45 public boolean matches(String argument) {46 return argument.startsWith("foo");47 }48}49import org.mockito.ArgumentMatcher;50public class MyArgumentMatcher implements ArgumentMatcher<String> {51 public boolean matches(String argument) {52 return argument.startsWith("foo");53 }54}55import org.mockito.ArgumentMatcher;56public class MyArgumentMatcher implements ArgumentMatcher<String> {57 public boolean matches(String argument) {58 return argument.startsWith("foo");59 }60}
ArgumentMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2import org.mockito.Mockito;3import org.mockito.stubbing.Answer;4public class ArgumentMatcherExample {5 public static void main(String[] args) {6 ArgumentMatcherExample argumentMatcherExample = new ArgumentMatcherExample();7 argumentMatcherExample.testArgumentMatcher();8 }9 public void testArgumentMatcher() {10 List<String> mockList = Mockito.mock(List.class);11 Mockito.when(mockList.get(Mockito.anyInt())).thenReturn("element");12 Mockito.when(mockList.contains(argThat(isValid()))).thenReturn(true);13 System.out.println(mockList.get(999));14 Mockito.verify(mockList).get(Mockito.anyInt());15 Mockito.when(mockList.contains(argThat(s -> s.length() > 5))).thenReturn(true);16 Mockito.when(mockList.contains(argThat(new IsValid()))).thenReturn(true);17 Mockito.when(mockList.contains(argThat(new IsValid()))).thenReturn(true);18 }19 private ArgumentMatcher<String> isValid() {20 return new ArgumentMatcher<String>() {21 public boolean matches(String argument) {22 return argument.length() > 5;23 }24 };25 }26 private class IsValid extends ArgumentMatcher<String> {27 public boolean matches(String argument) {28 return argument.length() > 5;29 }30 }31}32Following stubbings are unnecessary (click to navigate to relevant line of code):33 1. -> at org.mockito.internal.stubbing.answers.Returns.getAnswerFor(Returns.java:24)34 2. -> at org.mockito.internal.stubbing.answers.Returns.getAnswerFor(Returns.java:24)
ArgumentMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2import static org.mockito.Mockito.*;3import org.junit.Test;4import org.junit.runner.RunWith;5import org.mockito.runners.MockitoJUnitRunner;6class MyCustomArgumentMatcher extends ArgumentMatcher<String> {7 public boolean matches(Object argument) {8 return ((String) argument).startsWith("foo");9 }10}11@RunWith(MockitoJUnitRunner.class)12public class Test1 {13 public void test() {14 MyCustomArgumentMatcher myCustomArgumentMatcher = new MyCustomArgumentMatcher();15 when(myCustomArgumentMatcher.matches(anyString())).thenReturn(true);16 verify(myCustomArgumentMatcher).matches(anyString());17 }18}191. Invalid: when() method requires an argument which has to be 'matcher' (matcher methods are: anyInt(), anyObject(), anyString(), eq(), isA(), notNull(), same(), startsWith(), endsWith(), contains(), matches(), not() etc.). For example:20when(mock.get(anyInt())).thenReturn(100);21If you are trying to verify exact number of invocations then use verify(mock, times(10)).foo();22If you are trying to verify using exact argument values then use verify(mock).foo(10);23verify(mock, times(5)).foo();24verify(mock, atLeast(2)).bar();25verifyNoMoreInteractions(mock);26 at org.mockito.internal.runners.RunnerFactory$1.create(RunnerFactory.java:25)27 at org.mockito.internal.runners.RunnerFactory.create(RunnerFactory.java:16)28 at org.mockito.runners.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:22)29 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)30 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)31 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)32 at java.lang.reflect.Constructor.newInstance(Constructor.java:513)33 at org.junit.runners.model.DefaultRunnerBuilder.safeCreate(DefaultRunnerBuilder.java
ArgumentMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2public class HelloMatcher implements ArgumentMatcher<String> {3 public boolean matches(String argument) {4 return argument.contains("Hello World");5 }6}7import org.junit.Test;8import static org.junit.Assert.*;9import static org.mockito.Mockito.*;10public class HelloMatcherTest {11 public void test() {12 HelloMatcher helloMatcher = new HelloMatcher();13 assertTrue(helloMatcher.matches("Hello World"));14 assertFalse(helloMatcher.matches("Hello World!"));15 }16}17import org.junit.Test;18import static org.junit.Assert.*;19import static org.mockito.Mockito.*;20import org.mockito.ArgumentMatcher;21public class HelloMatcherTest {22 public void test() {23 ArgumentMatcher<String> helloMatcher = new ArgumentMatcher<String>() {24 public boolean matches(String argument) {25 return argument.contains("Hello World");26 }27 };28 assertTrue(helloMatcher.matches("Hello World"));29 assertFalse(helloMatcher.matches("Hello World!"));30 }31}32import org.junit.Test;33import static org.junit.Assert.*;34import static org.mockito.Mockito.*;35import org.mockito.ArgumentMatcher;36public class HelloMatcherTest {37 public void test() {38 ArgumentMatcher<String> helloMatcher = (String argument) -> argument.contains("Hello World");39 assertTrue(helloMatcher.matches("Hello World"));40 assertFalse(helloMatcher.matches("Hello World!"));41 }42}43import org.junit.Test;44import static org.junit.Assert.*;45import static org
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!