How to use matching method of org.hamcrest.Condition class

Best junit code snippet using org.hamcrest.Condition.matching

Source:Cortado_Tests.java Github

copy

Full Screen

1package cortado;2import android.support.test.espresso.matcher.ViewMatchers;3import android.view.View;4import android.widget.Button;5import android.widget.EditText;6import android.widget.ImageButton;7import android.widget.ImageView;8import android.widget.TextView;9import org.hamcrest.Matcher;10import org.hamcrest.Matchers;11import org.junit.Test;12import org.junit.runner.RunWith;13import internal.Order;14import internal.OrderedJUnit4TestRunner;15import internal.Utils;16import static com.google.common.truth.Truth.assertThat;17@RunWith(OrderedJUnit4TestRunner.class)18public class Cortado_Tests {19 // view20 @Order(1)21 @Test22 public void view_doesNotImplementMatcher_implementsMatching() {23 final Start.Matcher matcher = Cortado.view();24 assertThat(matcher).isNotInstanceOf(org.hamcrest.Matcher.class);25 assertThat(matcher).isInstanceOf(Matching.class);26 }27 @Order(2)28 @Test29 public void view_withNot_doesNotImplementMatcher_implementsMatching() {30 final Negated.Start.Matcher matcher = Cortado.view().not();31 assertThat(matcher).isNotInstanceOf(org.hamcrest.Matcher.class);32 assertThat(matcher).isInstanceOf(Matching.class);33 }34 @Order(3)35 @Test36 public void view_withSingleCondition_implementsMatcher_doesNotImplementMatching() {37 final Cortado.OrAnd.Matcher matcher = Cortado.view().withText("test");38 assertThat(matcher).isInstanceOf(org.hamcrest.Matcher.class);39 assertThat(matcher).isNotInstanceOf(Matching.class);40 }41 @Order(4)42 @Test43 public void view_withCondition_thenAnd_doesNotImplementMatcher_implementsMatching() {44 final Unfinished.And.Matcher matcher = Cortado.view().withText("test").and();45 assertThat(matcher).isNotInstanceOf(org.hamcrest.Matcher.class);46 assertThat(matcher).isInstanceOf(Matching.class);47 }48 @Order(5)49 @Test50 public void view_withCondition_thenAnd_thenNot_doesNotImplementMatcher_implementsMatching() {51 final Negated.Unfinished.And.Matcher matcher = Cortado.view().withText("test").and().not();52 assertThat(matcher).isNotInstanceOf(org.hamcrest.Matcher.class);53 assertThat(matcher).isInstanceOf(Matching.class);54 }55 @Order(6)56 @Test57 public void view_withCondition_thenAnd_thenCondition_implementsMatcher_doesNotImplementMatching() {58 final Cortado.And.Matcher matcher = Cortado.view().withText("test").and().withHint("test");59 assertThat(matcher).isInstanceOf(org.hamcrest.Matcher.class);60 assertThat(matcher).isNotInstanceOf(Matching.class);61 }62 @Order(7)63 @Test64 public void view_withCondition_thenAnd_thenNot_thenCondition_implementsMatcher_doesNotImplementMatching() {65 final Cortado.And.Matcher matcher = Cortado.view().withText("test").and().not().withHint("test");66 assertThat(matcher).isInstanceOf(org.hamcrest.Matcher.class);67 assertThat(matcher).isNotInstanceOf(Matching.class);68 }69 @Order(8)70 @Test71 public void view_withCondition_thenOr_doesNotImplementMatcher_implementsMatching() {72 final Unfinished.Or.Matcher matcher = Cortado.view().withText("test").or();73 assertThat(matcher).isNotInstanceOf(org.hamcrest.Matcher.class);74 assertThat(matcher).isInstanceOf(Matching.class);75 }76 @Order(9)77 @Test78 public void view_withCondition_thenOr_thenNot_doesNotImplementMatcher_implementsMatching() {79 final Negated.Unfinished.Or.Matcher matcher = Cortado.view().withText("test").or().not();80 assertThat(matcher).isNotInstanceOf(org.hamcrest.Matcher.class);81 assertThat(matcher).isInstanceOf(Matching.class);82 }83 @Order(10)84 @Test85 public void view_withCondition_thenOr_thenCondition_implementsMatcher_doesNotImplementMatching() {86 final Cortado.Or.Matcher matcher = Cortado.view().withText("test").or().withHint("test");87 assertThat(matcher).isInstanceOf(org.hamcrest.Matcher.class);88 assertThat(matcher).isNotInstanceOf(Matching.class);89 }90 @Order(11)91 @Test92 public void view_withCondition_thenOr_thenNot_thenCondition_implementsMatcher_doesNotImplementMatching() {93 final Cortado.Or.Matcher matcher = Cortado.view().withText("test").or().not().withHint("test");94 assertThat(matcher).isInstanceOf(org.hamcrest.Matcher.class);95 assertThat(matcher).isNotInstanceOf(Matching.class);96 }97 // onView98 @Order(12)99 @Test100 public void onView_doesNotImplementInteractable_implementsMatching() {101 final Start.ViewInteraction interaction = Cortado.onView();102 assertThat(interaction).isNotInstanceOf(Interactable.class);103 assertThat(interaction).isInstanceOf(Matching.class);104 }105 @Order(13)106 @Test107 public void onView_thenNot_doesNotImplementInteractable_implementsMatching() {108 final Negated.Start.ViewInteraction interaction = Cortado.onView().not();109 assertThat(interaction).isNotInstanceOf(Interactable.class);110 assertThat(interaction).isInstanceOf(Matching.class);111 }112 @Order(14)113 @Test114 public void onView_withSingleCondition_implementsInteractable_doesNotImplementMatching() {115 final Cortado.OrAnd.ViewInteraction interaction = Cortado.onView().withText("test");116 assertThat(interaction).isInstanceOf(Interactable.class);117 assertThat(interaction).isNotInstanceOf(Matching.class);118 }119 @Order(15)120 @Test121 public void onView_withCondition_thenAnd_doesNotImplementInteractable_implementsMatching() {122 final Unfinished.And.ViewInteraction interaction = Cortado.onView().withText("test").and();123 assertThat(interaction).isNotInstanceOf(Interactable.class);124 assertThat(interaction).isInstanceOf(Matching.class);125 }126 @Order(16)127 @Test128 public void onView_withCondition_thenAnd_thenNot_doesNotImplementInteractable_implementsMatching() {129 final Negated.Unfinished.And.ViewInteraction interaction = Cortado.onView().withText("test").and().not();130 assertThat(interaction).isNotInstanceOf(Interactable.class);131 assertThat(interaction).isInstanceOf(Matching.class);132 }133 @Order(17)134 @Test135 public void onView_withCondition_thenAnd_thenCondition_implementsInteractable_doesNotImplementMatching() {136 final Cortado.And.ViewInteraction interaction = Cortado.onView().withText("test").and().withHint("test");137 assertThat(interaction).isInstanceOf(Interactable.class);138 assertThat(interaction).isNotInstanceOf(Matching.class);139 }140 @Order(18)141 @Test142 public void onView_withCondition_thenAnd_thenNot_thenCondition_implementsInteractable_doesNotImplementMatching() {143 final Cortado.And.ViewInteraction interaction = Cortado.onView().withText("test").and().not().withHint("test");144 assertThat(interaction).isInstanceOf(Interactable.class);145 assertThat(interaction).isNotInstanceOf(Matching.class);146 }147 @Order(19)148 @Test149 public void onView_withCondition_thenOr_doesNotImplementInteractable_implementsMatching() {150 final Unfinished.Or.ViewInteraction interaction = Cortado.onView().withText("test").or();151 assertThat(interaction).isNotInstanceOf(Interactable.class);152 assertThat(interaction).isInstanceOf(Matching.class);153 }154 @Order(20)155 @Test156 public void onView_withCondition_thenOr_thenNot_doesNotImplementInteractable_implementsMatching() {157 final Negated.Unfinished.Or.ViewInteraction interaction = Cortado.onView().withText("test").or().not();158 assertThat(interaction).isNotInstanceOf(Interactable.class);159 assertThat(interaction).isInstanceOf(Matching.class);160 }161 @Order(21)162 @Test163 public void onView_withCondition_thenOr_thenCondition_implementsInteractable_doesNotImplementMatching() {164 final Cortado.Or.ViewInteraction interaction = Cortado.onView().withText("test").or().withHint("test");165 assertThat(interaction).isInstanceOf(Interactable.class);166 assertThat(interaction).isNotInstanceOf(Matching.class);167 }168 @Order(21)169 @Test170 public void onView_withCondition_thenOr_thenNot_thenCondition_implementsInteractable_doesNotImplementMatching() {171 final Cortado.Or.ViewInteraction interaction = Cortado.onView().withText("test").or().not().withHint("test");172 assertThat(interaction).isInstanceOf(Interactable.class);173 assertThat(interaction).isNotInstanceOf(Matching.class);174 }175 // setting linkers176 @Test177 public void view_hasProperLinker_initially() {178 //given179 Start.Matcher view = Cortado.view();180 //when181 Cortado.OrAnd.Matcher beforeAnd = view.withText("test");182 //then183 assertThat(beforeAnd.getCortado().linker).isEqualTo(Linker.REGULAR);184 }185 @Test186 public void view_and_setsProperLinker() {187 //given188 Cortado.OrAnd.Matcher beforeAnd = Cortado.view().withText("test");189 //when190 Unfinished.And.Matcher afterAnd = beforeAnd.and();191 //then192 assertThat(afterAnd.getCortado().linker).isEqualTo(Linker.AND);193 }194 @Test195 public void view_or_setsProperLinker() {196 //given197 Cortado.OrAnd.Matcher beforeOr = Cortado.view().withText("test");198 //when199 Unfinished.Or.Matcher afterOr = beforeOr.or();200 //then201 assertThat(afterOr.getCortado().linker).isEqualTo(Linker.OR);202 }203 @Test204 public void onView_hasProperLinker_initially() {205 //given206 Start.ViewInteraction viewInteraction = Cortado.onView();207 //when208 Cortado.OrAnd.ViewInteraction beforeAnd = viewInteraction.withText("test");209 //then210 assertThat(beforeAnd.getCortado().linker).isEqualTo(Linker.REGULAR);211 }212 @Test213 public void onView_and_setsProperLinker() {214 //given215 Cortado.OrAnd.ViewInteraction beforeAnd = Cortado.onView().withText("test");216 //when217 Unfinished.And.ViewInteraction afterAnd = beforeAnd.and();218 //then219 assertThat(afterAnd.getCortado().linker).isEqualTo(Linker.AND);220 }221 @Test222 public void onView_or_setsProperLinker() {223 //given224 Cortado.OrAnd.ViewInteraction beforeOr = Cortado.onView().withText("test");225 //when226 Unfinished.Or.ViewInteraction afterOr = beforeOr.or();227 //then228 assertThat(afterOr.getCortado().linker).isEqualTo(Linker.OR);229 }230 // negated constructors231 @Test232 public void view_hasProperNegatedFlag_initially() {233 //given234 //when235 Start.Matcher view = Cortado.view();236 //then237 assertThat(view.getCortado().negateNextMatcher).isFalse();238 }239 @Test240 public void view_not_setsNegatedFlag() {241 //given242 Start.Matcher view = Cortado.view();243 //when244 Negated.Start.Matcher not = view.not();245 //then246 assertThat(not.getCortado().negateNextMatcher).isTrue();247 }248 @Test249 public void view_withCondition_hasProperNegatedFlag() {250 //given251 //when252 Cortado.OrAnd.Matcher matcher = Cortado.view().withText("test");253 //then254 assertThat(matcher.getCortado().negateNextMatcher).isFalse();255 }256 @Test257 public void view_withCondition_or_not_setsNegatedFlag() {258 //given259 Cortado.OrAnd.Matcher matcher = Cortado.view().withText("test");260 //when261 Negated.Unfinished.Or.Matcher not = matcher.or().not();262 //then263 assertThat(not.getCortado().negateNextMatcher).isTrue();264 }265 @Test266 public void view_withCondition_and_not_setsNegatedFlag() {267 //given268 Cortado.OrAnd.Matcher matcher = Cortado.view().withText("test");269 //when270 Negated.Unfinished.And.Matcher not = matcher.and().not();271 //then272 assertThat(not.getCortado().negateNextMatcher).isTrue();273 }274 @Test275 public void onView_hasProperNegatedFlag_initially() {276 //given277 //when278 Start.ViewInteraction viewInteraction = Cortado.onView();279 //then280 assertThat(viewInteraction.getCortado().negateNextMatcher).isFalse();281 }282 @Test283 public void onView_not_setsNegatedFlag() {284 //given285 Start.ViewInteraction viewInteraction = Cortado.onView();286 //when287 Negated.Start.ViewInteraction not = viewInteraction.not();288 //then289 assertThat(not.getCortado().negateNextMatcher).isTrue();290 }291 @Test292 public void onView_withCondition_hasProperNegatedFlag() {293 //given294 //when295 Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onView().withText("test");296 //then297 assertThat(viewInteraction.getCortado().negateNextMatcher).isFalse();298 }299 @Test300 public void onView_withCondition_or_not_setsNegatedFlag() {301 //given302 Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onView().withText("test");303 //when304 Negated.Unfinished.Or.ViewInteraction not = viewInteraction.or().not();305 //then306 assertThat(not.getCortado().negateNextMatcher).isTrue();307 }308 @Test309 public void onView_withCondition_and_not_setsNegatedFlag() {310 //given311 Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onView().withText("test");312 //when313 Negated.Unfinished.And.ViewInteraction not = viewInteraction.and().not();314 //then315 assertThat(not.getCortado().negateNextMatcher).isTrue();316 }317 // addMatcher working with negateNextWatcher318 @Test319 public void addMatcher_doesNotNegateMatcher_when_negateNextMatcher_isFalse() {320 final Start.Matcher matcher = Cortado.view();321 final Cortado cortado = matcher.getCortado();322 assertThat(cortado.matchers).hasSize(0);323 assertThat(cortado.negateNextMatcher).isFalse();324 // no matchers added, negateNextMatcher is false325 org.hamcrest.Matcher<View> viewMatcher = ViewMatchers.withText("test");326 org.hamcrest.Matcher<View> negatedViewMatcher = Matchers.not(viewMatcher);327 cortado.negateNextMatcher = false;328 cortado.addMatcher(viewMatcher);329 assertThat(cortado.matchers).hasSize(1);330 // one matcher added331 assertThat(cortado.negateNextMatcher).isFalse();332 // negateNextMatcher is still false333 final Matcher<? super View> addedMatcher = cortado.matchers.get(0);334 Utils.assertThat(addedMatcher).isEqualTo(viewMatcher);335 Utils.assertThat(addedMatcher).isNotEqualTo(negatedViewMatcher);336 }337 @Test338 public void addMatcher_negatesMatcher_when_negateNextMatcher_isTrue() {339 final Start.Matcher matcher = Cortado.view();340 final Cortado cortado = matcher.getCortado();341 assertThat(cortado.matchers).hasSize(0);342 assertThat(cortado.negateNextMatcher).isFalse();343 // no matchers added, negateNextMatcher is false344 org.hamcrest.Matcher<View> viewMatcher = ViewMatchers.withText("test");345 org.hamcrest.Matcher<View> negatedViewMatcher = Matchers.not(viewMatcher);346 cortado.negateNextMatcher = true;347 cortado.addMatcher(viewMatcher);348 assertThat(cortado.matchers).hasSize(1);349 // one matcher added350 assertThat(cortado.negateNextMatcher).isFalse();351 // negateNextMatcher is back to false352 final Matcher<? super View> addedMatcher = cortado.matchers.get(0);353 Utils.assertThat(addedMatcher).isNotEqualTo(viewMatcher);354 Utils.assertThat(addedMatcher).isEqualTo(negatedViewMatcher);355 }356 // assignableFromClass357 @Test358 public void view_returnsProperMatcher() {359 //given360 final Cortado.OrAnd.Matcher matcher = Cortado.view().withText("test");361 final Matcher<? super View> espressoMatcher = matcher.getCortado().matchers.get(0);362 //when363 //then364 Utils.assertThat(matcher).isEqualTo(espressoMatcher);365 }366 @Test367 public void textView_returnsProperMatcher() {368 //given369 final Cortado.OrAnd.Matcher matcher = Cortado.textView().withText("test");370 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(371 ViewMatchers.isAssignableFrom(TextView.class),372 matcher.getCortado().matchers.get(0));373 //when374 //then375 Utils.assertThat(matcher).isEqualTo(expectedEspressoMatcher);376 }377 @Test378 public void editText_returnsProperMatcher() {379 //given380 final Cortado.OrAnd.Matcher matcher = Cortado.editText().withText("test");381 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(382 ViewMatchers.isAssignableFrom(EditText.class),383 matcher.getCortado().matchers.get(0));384 //when385 //then386 Utils.assertThat(matcher).isEqualTo(expectedEspressoMatcher);387 }388 @Test389 public void button_returnsProperMatcher() {390 //given391 final Cortado.OrAnd.Matcher matcher = Cortado.button().withText("test");392 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(393 ViewMatchers.isAssignableFrom(Button.class),394 matcher.getCortado().matchers.get(0));395 //when396 //then397 Utils.assertThat(matcher).isEqualTo(expectedEspressoMatcher);398 }399 @Test400 public void imageView_returnsProperMatcher() {401 //given402 final Cortado.OrAnd.Matcher matcher = Cortado.imageView().withText("test");403 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(404 ViewMatchers.isAssignableFrom(ImageView.class),405 matcher.getCortado().matchers.get(0));406 //when407 //then408 Utils.assertThat(matcher).isEqualTo(expectedEspressoMatcher);409 }410 @Test411 public void imageButton_returnsProperMatcher() {412 //given413 final Cortado.OrAnd.Matcher matcher = Cortado.imageButton().withText("test");414 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(415 ViewMatchers.isAssignableFrom(ImageButton.class),416 matcher.getCortado().matchers.get(0));417 //when418 //then419 Utils.assertThat(matcher).isEqualTo(expectedEspressoMatcher);420 }421 @Test422 public void onView_returnsProperViewInteraction() {423 //given424 final Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onView().withText("test");425 final Matcher<? super View> espressoMatcher = viewInteraction.getCortado().matchers.get(0);426 //when427 final Matcher<View> rawMatcher = viewInteraction.getCortado().get();428 //then429 Utils.assertThat(rawMatcher).isEqualTo(espressoMatcher);430 }431 @Test432 public void onTextView_returnsProperViewInteraction() {433 //given434 final Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onTextView().withText("test");435 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(436 ViewMatchers.isAssignableFrom(TextView.class),437 viewInteraction.getCortado().matchers.get(0));438 //when439 final Matcher<View> rawMatcher = viewInteraction.getCortado().get();440 //then441 Utils.assertThat(rawMatcher).isEqualTo(expectedEspressoMatcher);442 }443 @Test444 public void onEditText_returnsProperViewInteraction() {445 //given446 final Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onEditText().withText("test");447 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(448 ViewMatchers.isAssignableFrom(EditText.class),449 viewInteraction.getCortado().matchers.get(0));450 //when451 final Matcher<View> rawMatcher = viewInteraction.getCortado().get();452 //then453 Utils.assertThat(rawMatcher).isEqualTo(expectedEspressoMatcher);454 }455 @Test456 public void onButton_returnsProperViewInteraction() {457 //given458 final Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onButton().withText("test");459 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(460 ViewMatchers.isAssignableFrom(Button.class),461 viewInteraction.getCortado().matchers.get(0));462 //when463 final Matcher<View> rawMatcher = viewInteraction.getCortado().get();464 //then465 Utils.assertThat(rawMatcher).isEqualTo(expectedEspressoMatcher);466 }467 @Test468 public void onImageView_returnsProperViewInteraction() {469 //given470 final Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onImageView().withText("test");471 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(472 ViewMatchers.isAssignableFrom(ImageView.class),473 viewInteraction.getCortado().matchers.get(0));474 //when475 final Matcher<View> rawMatcher = viewInteraction.getCortado().get();476 //then477 Utils.assertThat(rawMatcher).isEqualTo(expectedEspressoMatcher);478 }479 @Test480 public void onImageButton_returnsProperViewInteraction() {481 //given482 final Cortado.OrAnd.ViewInteraction viewInteraction = Cortado.onImageButton().withText("test");483 final Matcher<View> expectedEspressoMatcher = Matchers.allOf(484 ViewMatchers.isAssignableFrom(ImageButton.class),485 viewInteraction.getCortado().matchers.get(0));486 //when487 final Matcher<View> rawMatcher = viewInteraction.getCortado().get();488 //then489 Utils.assertThat(rawMatcher).isEqualTo(expectedEspressoMatcher);490 }491}...

Full Screen

Full Screen

Source:JsonPathMatcher.java Github

copy

Full Screen

...23 @Override24 protected boolean matchesSafely(String source, Description mismatch) {25 return parse(source, mismatch)26 .and(findElement)27 .matching(elementContents);28 }29 public void describeTo(Description description) {30 description.appendText("Json with path '").appendText(jsonPath).appendText("'")31 .appendDescriptionOf(elementContents);32 }33 @Factory34 public static Matcher<String> hasJsonPath(final String jsonPath) {35 return new JsonPathMatcher(jsonPath, any(JsonElement.class));36 }37 @Factory38 public static Matcher<String> hasJsonElement(final String jsonPath, final Matcher<String> contentsMatcher) {39 return new JsonPathMatcher(jsonPath, elementWith(contentsMatcher));40 }41 private Condition<JsonObject> parse(String source, Description mismatch) {42 try {43 return matched(new JsonParser().parse(source).getAsJsonObject(), mismatch);44 } catch (JsonSyntaxException e) {45 mismatch.appendText(e.getMessage());46 }47 return notMatched();48 }49 private static Matcher<JsonElement> elementWith(final Matcher<String> contentsMatcher) {50 return new TypeSafeDiagnosingMatcher<JsonElement>() {51 @Override52 protected boolean matchesSafely(JsonElement element, Description mismatch) {53 return jsonPrimitive(element, mismatch).matching(contentsMatcher);54 }55 public void describeTo(Description description) {56 description.appendText("containing ").appendDescriptionOf(contentsMatcher);57 }58 private Condition<String> jsonPrimitive(JsonElement element, Description mismatch) {59 if (element.isJsonPrimitive()) {60 return matched(element.getAsJsonPrimitive().getAsString(), mismatch);61 }62 mismatch.appendText("element was ").appendValue(element);63 return notMatched();64 }65 };66 }67 private static Condition.Step<JsonElement, JsonElement> findElementStep(final String jsonPath) {...

Full Screen

Full Screen

Source:EmployeeDaoIntTest.java Github

copy

Full Screen

1package net.madvirus.spring4.chap15.hr.dao;2import static org.hamcrest.Matchers.equalTo;3import static org.hamcrest.Matchers.greaterThan;4import static org.hamcrest.Matchers.hasSize;5import static org.hamcrest.Matchers.notNullValue;6import static org.hamcrest.Matchers.nullValue;7import static org.hamcrest.MatcherAssert.*;8import java.text.ParseException;9import java.text.SimpleDateFormat;10import java.util.Date;11import java.util.List;12import net.madvirus.spring4.chap15.conf.SpringAppConfig;13import net.madvirus.spring4.chap15.hr.model.Employee;14import org.junit.Test;15import org.junit.runner.RunWith;16import org.springframework.beans.factory.annotation.Autowired;17import org.springframework.test.context.ContextConfiguration;18import org.springframework.test.context.TestExecutionListeners;19import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;20import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;21import org.springframework.test.context.support.DirtiesContextTestExecutionListener;22import org.springframework.test.context.transaction.TransactionalTestExecutionListener;23import com.github.springtestdbunit.DbUnitTestExecutionListener;24import com.github.springtestdbunit.annotation.DatabaseSetup;25@ContextConfiguration(classes = SpringAppConfig.class)26@RunWith(SpringJUnit4ClassRunner.class)27@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,28 DirtiesContextTestExecutionListener.class,29 TransactionalTestExecutionListener.class,30 DbUnitTestExecutionListener.class })31@DatabaseSetup("classpath:/Employee.xml")32public class EmployeeDaoIntTest {33 @Autowired34 private EmployeeDao employeeDao;35 @Test36 public void insert() throws Exception {37 Employee emp = new Employee();38 emp.setNumber("2345678901");39 emp.setName("유종필");40 emp.setBirtyYear(1978);41 emp.setJoinedDate(createDate("2014-06-04"));42 Long newId = employeeDao.insert(emp);43 assertThat(newId, notNullValue());44 assertThat(newId, greaterThan(6L));45 }46 47 @Test48 public void givenExistingEmployeeNumber_then_SelectByEmployeeNumber_Return_MatchingEmployee() {49 Employee emp = employeeDao.selectByEmployeeNumber("1234567891");50 assertThat(emp, notNullValue());51 }52 @Test53 public void givenNoneExistingEmployeeNumber_then_SelectByEmployeeNumber_Return_MatchingEmployee() {54 Employee emp = employeeDao.selectByEmployeeNumber("9999999999");55 assertThat(emp, nullValue());56 }57 @Test58 public void givenNoCcondition_then_returnAll() throws Exception {59 SearchCondition cond = new SearchCondition();60 List<Employee> list = employeeDao.selectList(cond);61 assertThat(list, hasSize(6));62 }63 @Test64 public void givenNameLike_then_returnMatching() throws Exception {65 SearchCondition cond = new SearchCondition();66 cond.setNameKeyword("범");67 List<Employee> list = employeeDao.selectList(cond);68 assertThat(list, hasSize(1));69 Employee emp = list.get(0);70 assertThat(emp.getName(), equalTo("최범균"));71 }72 @Test73 public void givenEmpNumber_then_returnMatching() throws Exception {74 SearchCondition cond = new SearchCondition();75 cond.setEmpNumber("1234567891");76 List<Employee> list = employeeDao.selectList(cond);77 assertThat(list, hasSize(1));78 Employee emp = list.get(0);79 assertThat(emp.getName(), equalTo("홍길동"));80 }81 @Test82 public void givenFromJoinedDate_then_returnMatching() throws Exception {83 SearchCondition cond = new SearchCondition();84 cond.setFromJoinedDate(createDate("2013-01-01"));85 List<Employee> list = employeeDao.selectList(cond);86 assertThat(list, hasSize(5));87 }88 @Test89 public void givenToJoinedDate_then_returnMatching() throws Exception {90 SearchCondition cond = new SearchCondition();91 cond.setFromJoinedDate(createDate("2013-12-31"));92 List<Employee> list = employeeDao.selectList(cond);93 assertThat(list, hasSize(3));94 }95 @Test96 public void givenFromTo_then_returnMathcing() throws Exception {97 SearchCondition cond = new SearchCondition();98 cond.setFromJoinedDate(createDate("2013-01-01"));99 cond.setToJoinedDate(createDate("2013-12-31"));100 List<Employee> list = employeeDao.selectList(cond);101 assertThat(list, hasSize(2));102 }103 private Date createDate(String dateString) throws ParseException {104 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");105 return format.parse(dateString);106 }107}...

Full Screen

Full Screen

Source:Matchers.java Github

copy

Full Screen

...19 public boolean matchesSafely(T bean, Description mismatch) {20 return propertyOn(bean,"present", mismatch)21 .and(Matchers::withReadMethod)22 .and(withPropertyValue(bean))23 .matching(IS_VALID_MATCHER, mismatchMessage);24 }25 @Override26 public void describeTo(Description description) {27 description.appendText("isPresent()");28 }29 }30 public static <T> org.hamcrest.Matcher<T> isEmpty() {31 return new IsEmpty<>();32 }33 private static class IsEmpty<T> extends TypeSafeDiagnosingMatcher<T> {34 @Override35 public boolean matchesSafely(T bean, Description mismatch) {36 return propertyOn(bean,"empty", mismatch)37 .and(Matchers::withReadMethod)38 .and(withPropertyValue(bean))39 .matching(IS_VALID_MATCHER, mismatchMessage);40 }41 @Override42 public void describeTo(Description description) {43 description.appendText("isEmpty()");44 }45 }46 private static <T> Condition<PropertyDescriptor> propertyOn(T bean, String propertyName, Description mismatch) {47 PropertyDescriptor property = PropertyUtil.getPropertyDescriptor(propertyName, bean);48 if (property == null) {49 mismatch.appendText("Not a valid Optional Class");50 return notMatched();51 }52 return matched(property, mismatch);53 }...

Full Screen

Full Screen

Source:IsJsonStringMatching.java Github

copy

Full Screen

...28import org.hamcrest.Description;29import org.hamcrest.Matcher;30import org.hamcrest.TypeSafeDiagnosingMatcher;31/**32 * Matcher for matching Json strings33 *34 * <p>This is useful as an entry point to matching larger structures without needing35 * to use Jackson explicitly.36 *37 * <pre>38 * <code>39 * String myJson = "{\"key\": 1234}";40 * assertThat(myJson, isJsonStringMatching(jsonObject().where("key", jsonInt(1234)));41 * </code>42 * </pre>43 */44public final class IsJsonStringMatching extends TypeSafeDiagnosingMatcher<String> {45 private static final ObjectMapper MAPPER = new ObjectMapper();46 public static Matcher<String> isJsonStringMatching(final Matcher<JsonNode> matcher) {47 return new IsJsonStringMatching(matcher);48 }49 private final Matcher<JsonNode> matcher;50 private IsJsonStringMatching(final Matcher<JsonNode> matcher) {51 this.matcher = requireNonNull(matcher, "matcher");52 }53 @Override54 protected boolean matchesSafely(final String string, final Description description) {55 return parseJsonNode(string, description)56 .matching(matcher);57 }58 private Condition<JsonNode> parseJsonNode(final String string,59 final Description mismatchDescription) {60 if (string == null) {61 mismatchDescription.appendText(" but JSON string was null");62 return notMatched();63 }64 try {65 final JsonNode jsonNode = MAPPER.readTree(string);66 return matched(jsonNode, mismatchDescription);67 } catch (IOException e) {68 mismatchDescription.appendText(" but the string was not valid JSON ")69 .appendValue(e.getMessage());70 return notMatched();...

Full Screen

Full Screen

Source:HamcrestCondition.java Github

copy

Full Screen

...26 *27 * // assertion will fail28 * assertThat(&quot;bc&quot;).is(aStringContainingA);</code></pre>29 *30 * By static-importing the {@link #matching(Matcher)} method you can do:31 * <pre><code class='java'> assertThat(&quot;abc&quot;).is(matching(containsString(&quot;a&quot;)));</code></pre>32 * @since 2.9.0 / 3.9.033 */34public class HamcrestCondition<T> extends Condition<T> {35 private Matcher<? extends T> matcher;36 /**37 * Constructs a {@link Condition} using the matcher given as a parameter.38 *39 * @param matcher the Hamcrest matcher to use as a condition40 */41 public HamcrestCondition(Matcher<? extends T> matcher) {42 this.matcher = matcher;43 as(describeMatcher());44 }45 /**46 * Constructs a {@link Condition} using the matcher given as a parameter.47 * <p>48 * Example:49 * <pre><code class='java'> import static org.assertj.core.api.Assertions.assertThat;50 * import static org.assertj.core.api.HamcrestCondition.matching;51 * import static org.hamcrest.core.StringContains.containsString;52 *53 * assertThat(&quot;abc&quot;).is(matching(containsString(&quot;a&quot;)));</code></pre>54 *55 * @param <T> the type the condition is about56 * @param matcher the Hamcrest matcher to use as a condition57 * @return the built {@link HamcrestCondition}58 */59 public static <T> HamcrestCondition<T> matching(Matcher<? extends T> matcher) {60 return new HamcrestCondition<>(matcher);61 }62 /**63 * {@inheritDoc}64 */65 @Override66 public boolean matches(T value) {67 return matcher.matches(value);68 }69 private String describeMatcher() {70 Description d = new StringDescription();71 matcher.describeTo(d);72 return d.toString();73 }...

Full Screen

Full Screen

Source:Condition.java Github

copy

Full Screen

...4 public interface Step<I, O> {5 Condition<O> apply(I i, Description description);6 }7 public abstract <U> Condition<U> and(Step<? super T, U> step);8 public abstract boolean matching(Matcher<T> matcher, String str);9 private Condition() {10 }11 public final boolean matching(Matcher<T> match) {12 return matching(match, "");13 }14 public final <U> Condition<U> then(Step<? super T, U> mapping) {15 return and(mapping);16 }17 public static <T> Condition<T> notMatched() {18 return NOT_MATCHED;19 }20 public static <T> Condition<T> matched(T theValue, Description mismatch) {21 return new Matched(theValue, mismatch);22 }23 private static final class Matched<T> extends Condition<T> {24 private final Description mismatch;25 private final T theValue;26 private Matched(T theValue2, Description mismatch2) {27 super();28 this.theValue = theValue2;29 this.mismatch = mismatch2;30 }31 @Override // org.hamcrest.Condition32 public boolean matching(Matcher<T> matcher, String message) {33 if (matcher.matches(this.theValue)) {34 return true;35 }36 this.mismatch.appendText(message);37 matcher.describeMismatch(this.theValue, this.mismatch);38 return false;39 }40 @Override // org.hamcrest.Condition41 public <U> Condition<U> and(Step<? super T, U> next) {42 return next.apply(this.theValue, this.mismatch);43 }44 }45 private static final class NotMatched<T> extends Condition<T> {46 private NotMatched() {47 super();48 }49 @Override // org.hamcrest.Condition50 public boolean matching(Matcher<T> matcher, String message) {51 return false;52 }53 @Override // org.hamcrest.Condition54 public <U> Condition<U> and(Step<? super T, U> step) {55 return notMatched();56 }57 }58}...

Full Screen

Full Screen

Source:JsonPathSegmentTest.java Github

copy

Full Screen

...20 @Test public void21 returns_the_child_of_a_JsonObject() {22 final JsonObject parent = new JsonObject();23 parent.add("child", child);24 assertTrue("child", next(parent, "child").matching(sameInstance(child)));25 }26 @Test public void27 returns_not_matched_for_wrong_property() {28 final JsonObject parent = new JsonObject();29 assertFalse("not child", next(parent, "child").matching(any(JsonElement.class)));30 }31 @Test public void32 returns_the_element_of_a_JsonArray() {33 final JsonArray parent = new JsonArray();34 parent.add(child);35 assertTrue("first element", next(parent, "0").matching(sameInstance(child)));36 }37 @Test public void38 returns_not_matched_for_array_out_of_bounds() {39 final JsonArray parent = new JsonArray();40 parent.add(child);41 assertFalse("out of bounds", next(parent, "2").matching(any(JsonElement.class)));42 }43 @Test public void44 returns_not_matched_for_array_index_notANumber() {45 final JsonArray parent = new JsonArray();46 assertFalse("not a number", next(parent, "xx").matching(any(JsonElement.class)));47 }48 private Condition<JsonElement> next(JsonElement parent, String pathSegment) {49 return new JsonPathSegment(pathSegment, "path so far").apply(parent, description);50 }51}...

Full Screen

Full Screen

matching

Using AI Code Generation

copy

Full Screen

1import static org.hamcrest.MatcherAssert.assertThat;2import static org.hamcrest.Matchers.*;3import java.util.Arrays;4import java.util.List;5import org.hamcrest.Condition;6import org.hamcrest.Matcher;7import org.hamcrest.TypeSafeMatcher;8import org.junit.Test;9public class HamcrestConditionTest {10 public void testCondition() {11 List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);12 assertThat(list, hasItem(new Condition<Integer>() {13 public boolean matches(Integer value) {14 return value % 2 == 0;15 }16 }));17 }18 public void testCondition2() {19 List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);20 assertThat(list, hasItem(new Condition<Integer>() {21 public boolean matches(Integer value) {22 return value % 2 == 0;23 }24 public String toString() {25 return "an even value";26 }27 }));28 }29 public void testCondition3() {30 List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);31 assertThat(list, hasItem(new Condition<Integer>() {32 public boolean matches(Integer value) {

Full Screen

Full Screen

matching

Using AI Code Generation

copy

Full Screen

1import static org.hamcrest.MatcherAssert.assertThat;2import static org.hamcrest.Matchers.*;3public class ConditionClassTest {4 public void testConditionClass() {5 assertThat("foo", new Condition<String>() {6 public boolean matches(String value) {7 return value.startsWith("f");8 }9 });10 }11}12package com.baeldung.java.hamcrest;13import org.hamcrest.Condition;14import org.junit.Test;15import static org.hamcrest.MatcherAssert.assertThat;16import static org.hamcrest.Matchers.*;17public class ConditionClassTest {18 public void testConditionClass() {19 assertThat("foo", matches(new Condition<String>() {20 public boolean matches(String value) {21 return value.startsWith("f");22 }23 }));24 }25}26package com.baeldung.java.hamcrest;27import org.hamcrest.Condition;28import org.junit.Test;29import static org.hamcrest.MatcherAssert.assertThat;30import static org.hamcrest.Matchers.*;31public class ConditionClassTest {32 public void testConditionClass() {33 assertThat("foo", matches(new Condition<String>() {34 public boolean matches(String value) {35 return value.endsWith("o");36 }37 }));38 }39}40package com.baeldung.java.hamcrest;41import org.hamcrest.Condition;42import org.junit

Full Screen

Full Screen

matching

Using AI Code Generation

copy

Full Screen

1import static org.hamcrest.MatcherAssert.assertThat;2import static org.hamcrest.Matchers.containsString;3public class ConditionExample {4 public void testCondition() {5 String str = "Hello World";6 assertThat(str, containsString("World"));7 }8}9import static org.hamcrest.MatcherAssert.assertThat;10import static org.hamcrest.Matchers.containsString;11public class ConditionExample {12 public void testCondition() {13 String str = "Hello World";14 assertThat(str, containsString("World"));15 }16}17import static org.hamcrest.MatcherAssert.assertThat;18import static org.hamcrest.Matchers.containsString;19public class ConditionExample {20 public void testCondition() {21 String str = "Hello World";22 assertThat(str, containsString("World"));23 }24}25import static org.hamcrest.MatcherAssert.assertThat;26import static org.hamcrest.Matchers.containsString;27public class ConditionExample {28 public void testCondition() {29 String str = "Hello World";30 assertThat(str, containsString("World"));31 }32}33import static org.hamcrest.MatcherAssert.assertThat;34import static org.hamcrest.Matchers.containsString;35public class ConditionExample {36 public void testCondition() {37 String str = "Hello World";38 assertThat(str, containsString("World"));39 }40}

Full Screen

Full Screen

matching

Using AI Code Generation

copy

Full Screen

1assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));2assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));3assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));4assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));5assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));6assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));7assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));8assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));9assertThat("abc123xyz", Condition.matching("abc.*xyz", "starts with abc and ends with xyz"));

Full Screen

Full Screen

JUnit Tutorial:

LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.

JUnit Tutorial Chapters:

Here are the detailed JUnit testing chapters to help you get started:

  • Importance of Unit testing - Learn why Unit testing is essential during the development phase to identify bugs and errors.
  • Top Java Unit testing frameworks - Here are the upcoming JUnit automation testing frameworks that you can use in 2023 to boost your unit testing.
  • What is the JUnit framework
  • Why is JUnit testing important - Learn the importance and numerous benefits of using the JUnit testing framework.
  • Features of JUnit - Learn about the numerous features of JUnit and why developers prefer it.
  • JUnit 5 vs. JUnit 4: Differences - Here is a complete comparison between JUnit 5 and JUnit 4 testing frameworks.
  • Setting up the JUnit environment - Learn how to set up your JUnit testing environment.
  • Getting started with JUnit testing - After successfully setting up your JUnit environment, this chapter will help you get started with JUnit testing in no time.
  • Parallel testing with JUnit - Parallel Testing can be used to reduce test execution time and improve test efficiency. Learn how to perform parallel testing with JUnit.
  • Annotations in JUnit - When writing automation scripts with JUnit, we can use JUnit annotations to specify the type of methods in our test code. This helps us identify those methods when we run JUnit tests using Selenium WebDriver. Learn in detail what annotations are in JUnit.
  • Assertions in JUnit - Assertions are used to validate or test that the result of an action/functionality is the same as expected. Learn in detail what assertions are and how to use them while performing JUnit testing.
  • Parameterization in JUnit - Parameterized Test enables you to run the same automated test scripts with different variables. By collecting data on each method's test parameters, you can minimize time spent on writing tests. Learn how to use parameterization in JUnit.
  • Nested Tests In JUnit 5 - A nested class is a non-static class contained within another class in a hierarchical structure. It can share the state and setup of the outer class. Learn about nested annotations in JUnit 5 with examples.
  • Best practices for JUnit testing - Learn about the best practices, such as always testing key methods and classes, integrating JUnit tests with your build, and more to get the best possible results.
  • Advanced Use Cases for JUnit testing - Take a deep dive into the advanced use cases, such as how to run JUnit tests in Jupiter, how to use JUnit 5 Mockito for Unit testing, and more for JUnit testing.

JUnit Certification:

You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Condition

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful