How to use startsWith method of org.hamcrest.core.StringStartsWith class

Best junit code snippet using org.hamcrest.core.StringStartsWith.startsWith

Run junit automation tests on LambdaTest cloud grid

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

copy
1package org.hamcrest.core;
2
3import org.hamcrest.AbstractMatcherTest;
4import org.hamcrest.Matcher;
5
6import static org.hamcrest.core.StringStartsWith.startsWith;
7import static org.hamcrest.core.StringStartsWith.startsWithIgnoringCase;
8
9
10public class StringStartsWithTest extends AbstractMatcherTest {
11    static final String EXCERPT = "EXCERPT";
12    final Matcher<String> stringStartsWith = startsWith(EXCERPT);
13
14    @Override
15    protected Matcher<?> createMatcher() {
16        return stringStartsWith;
17    }
18
19    public void testMatchesStringAtStart() {
20        assertMatches(stringStartsWith, EXCERPT + "END");
21        assertDoesNotMatch(stringStartsWith, "START" + EXCERPT);
22        assertDoesNotMatch(stringStartsWith, "START" + EXCERPT + "END");
23        assertMatches(stringStartsWith, EXCERPT);
24        assertDoesNotMatch(stringStartsWith, EXCERPT.toLowerCase());
25        assertMatches(stringStartsWith, EXCERPT + EXCERPT);
26        assertDoesNotMatch(stringStartsWith, "EXCER");
27
28        assertDescription("a string starting with \"EXCERPT\"", stringStartsWith);
29        assertMismatchDescription("was \"Something else\"", stringStartsWith, "Something else");
30    }
31
32    public void testMatchesStringAtStartIgnoringCase() {
33        final Matcher<String> ignoreCase = startsWithIgnoringCase("EXCerPT");
34
35        assertMatches(ignoreCase, "exCerPT" + "END");
36        assertDoesNotMatch(ignoreCase, "START" + "EXCerpt");
37        assertDoesNotMatch(ignoreCase, "START" + "EXcerpT" + "END");
38        assertMatches(ignoreCase, "excERPT");
39        assertMatches(ignoreCase, "ExcerPT" + "EXCerpt");
40        assertDoesNotMatch(ignoreCase, "ExcER");
41
42        assertDescription("a string starting with \"EXCerPT\" ignoring case", ignoreCase);
43        assertMismatchDescription("was \"Something else\"", ignoreCase, "Something else");
44    }
45
46}
47
Full Screen
copy
1package com.mrv.yangtools.codegen.impl.postprocessor;
2
3import org.hamcrest.core.Every;
4import org.hamcrest.core.StringStartsWith;
5import org.junit.Assert;
6import org.junit.Test;
7
8import static org.hamcrest.CoreMatchers.*;
9import static org.hamcrest.core.IsNot.not;
10import static org.junit.Assert.assertEquals;
11
12/**
13 * @author bartosz.michalik@amartus.com
14 */
15public class PathPrunnerTest extends AbstractWithSwagger {
16
17
18    @Test
19    public void noChange() {
20        int orgPathsCnt = swagger.getPaths().size();
21        int orgDefCnt = swagger.getDefinitions().size();
22        new PathPrunner().accept(swagger);
23        assertEquals(orgPathsCnt, swagger.getPaths().size());
24        assertEquals(orgDefCnt, swagger.getDefinitions().size());
25
26    }
27
28    @Test
29    public void prunePathB() {
30        int orgPathsCnt = swagger.getPaths().size();
31        int orgDefCnt = swagger.getDefinitions().size();
32        new PathPrunner()
33                .prunePath("/b")
34                .accept(swagger);
35        assertEquals(orgPathsCnt - 4, swagger.getPaths().size());
36        assertEquals(orgDefCnt, swagger.getDefinitions().size());
37        Assert.assertThat(swagger.getPaths().keySet(), Every.everyItem(not(StringStartsWith.startsWith("/b"))));
38    }
39
40    @Test
41    public void prunePathBA() {
42        int orgPathsCnt = swagger.getPaths().size();
43        int orgDefCnt = swagger.getDefinitions().size();
44        new PathPrunner()
45                .prunePath("/b/propE")
46                .prunePath("/a")
47                .accept(swagger);
48        assertEquals(orgPathsCnt - 3, swagger.getPaths().size());
49        assertEquals(orgDefCnt, swagger.getDefinitions().size());
50        Assert.assertThat(swagger.getPaths().keySet(), Every.everyItem(not(StringStartsWith.startsWith("/b/propE"))));
51        Assert.assertThat(swagger.getPaths().keySet(), Every.everyItem(not(StringStartsWith.startsWith("/a"))));
52        Assert.assertThat(swagger.getPaths().keySet(), hasItem(StringStartsWith.startsWith("/b")));
53    }
54
55    @Test
56    public void pruneParent2() {
57
58        int orgDefCnt = swagger.getDefinitions().size();
59        new PathPrunner()
60                .withType("Parent1")
61                .accept(swagger);
62        assertEquals(2, swagger.getPaths().size());
63        Assert.assertThat(swagger.getPaths().keySet(), hasItems(
64                is("/a"),
65                is("/b")
66        ));
67        assertEquals(orgDefCnt, swagger.getDefinitions().size());
68    }
69
70
71}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in StringStartsWith

Trigger startsWith code on LambdaTest Cloud Grid

Execute automation tests with startsWith on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)