How to use assumeThat method of org.junit.Assume class

Best junit code snippet using org.junit.Assume.assumeThat

Run junit automation tests on LambdaTest cloud grid

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

copy
1import domain.users.User;
2import org.junit.BeforeClass;
3import org.junit.Rule;
4import org.junit.experimental.theories.DataPoints;
5import org.junit.experimental.theories.Theories;
6import org.junit.experimental.theories.Theory;
7import org.junit.rules.ExpectedException;
8import org.junit.runner.RunWith;
9
10import static org.hamcrest.CoreMatchers.*;
11import static org.junit.Assume.assumeNotNull;
12import static org.junit.Assume.assumeThat;
13
14@RunWith(Theories.class)
15public class UsersTest {
16    private static User user1;
17    private static User user2;
18
19    @Rule
20    public ExpectedException thrown = ExpectedException.none();
21
22    @BeforeClass
23    public static void createUsers() {
24        user1 = new User("Nataliya", "svk", 56646, "56452ghh54");
25        user2 = new User("Alexander", "kim", 46843, "54746jm458");
26    }
27
28    @DataPoints
29    public static String[] usersNames() {
30        return new String[]{user1.getName(), user2.getName()};
31    }
32
33    @Theory
34    public void testUsersNames(String name) throws Exception {
35        System.out.println(String.format("Testing with name %s and login %s", name));
36        assumeNotNull(name);
37        assumeThat(name, notNullValue());
38    }
39
40    @DataPoints
41    public static String[] usersLogin() {
42        return new String[]{user1.getLogin(), user2.getLogin()};
43    }
44
45    @Theory
46    public void testUsersLogin(String login) throws Exception {
47        System.out.println(String.format("Testing with login %s ", login));
48        assumeNotNull(login);
49        assumeThat(login, notNullValue());
50    }
51
52    @DataPoints
53    public static int[] usersPassword() {
54        return new int[]{(int) user1.getPassword(), (int) user2.getPassword()};
55    }
56
57    @Theory
58    public void testUsersPassword(int password) throws Exception {
59        System.out.println(String.format("Testing with password %d ", password));
60        assumeNotNull(password);
61        assumeThat(password, notNullValue());
62    }
63}
Full Screen
copy
1package org.junit;
2
3import static java.util.Arrays.asList;
4import static org.hamcrest.CoreMatchers.everyItem;
5import static org.hamcrest.CoreMatchers.is;
6import static org.hamcrest.CoreMatchers.notNullValue;
7import static org.hamcrest.CoreMatchers.nullValue;
8
9import org.hamcrest.Matcher;
10import org.junit.internal.AssumptionViolatedException;
11
12/**
13 * A set of methods useful for stating assumptions about the conditions in which a test is meaningful.
14 * A failed assumption does not mean the code is broken, but that the test provides no useful information.
15 * The default JUnit runner treats tests with failing assumptions as ignored.  Custom runners may behave differently.
16 *
17 * For example:
18 * <pre>
19 * // only provides information if database is reachable.
20 * \@Test public void calculateTotalSalary() {
21 *    DBConnection dbc = Database.connect();
22 *    assumeNotNull(dbc);
23 *    // ...
24 * }
25 * </pre>
26 * These methods can be used directly: <code>Assume.assumeTrue(...)</code>, however, they
27 * read better if they are referenced through static import:<br/>
28 * <pre>
29 * import static org.junit.Assume.*;
30 *    ...
31 *    assumeTrue(...);
32 * </pre>
33 *
34 * @since 4.4
35 */
36public class Assume {
37    /**
38     * If called with an expression evaluating to {@code false}, the test will halt and be ignored.
39     */
40    public static void assumeTrue(boolean b) {
41        assumeThat(b, is(true));
42    }
43
44    /**
45     * The inverse of {@link #assumeTrue(boolean)}.
46     */
47    public static void assumeFalse(boolean b) {
48        assumeTrue(!b);
49    }
50
51    /**
52     * If called with an expression evaluating to {@code false}, the test will halt and be ignored.
53     *
54     * @param b If <code>false</code>, the method will attempt to stop the test and ignore it by
55     * throwing {@link AssumptionViolatedException}.
56     * @param message A message to pass to {@link AssumptionViolatedException}.
57     */
58    public static void assumeTrue(String message, boolean b) {
59        if (!b) throw new AssumptionViolatedException(message);
60    }
61
62    /**
63     * The inverse of {@link #assumeTrue(String, boolean)}.
64     */
65    public static void assumeFalse(String message, boolean b) {
66        assumeTrue(message, !b);
67    }
68
69    /**
70     * If called with one or more null elements in <code>objects</code>, the test will halt and be ignored.
71     */
72    public static void assumeNotNull(Object... objects) {
73        assumeThat(asList(objects), everyItem(notNullValue()));
74    }
75
76    /**
77     * Call to assume that <code>actual</code> satisfies the condition specified by <code>matcher</code>.
78     * If not, the test halts and is ignored.
79     * Example:
80     * <pre>:
81     *   assumeThat(1, is(1)); // passes
82     *   foo(); // will execute
83     *   assumeThat(0, is(1)); // assumption failure! test halts
84     *   int x = 1 / 0; // will never execute
85     * </pre>
86     *
87     * @param <T> the static type accepted by the matcher (this can flag obvious compile-time problems such as {@code assumeThat(1, is("a"))}
88     * @param actual the computed value being compared
89     * @param matcher an expression, built of {@link Matcher}s, specifying allowed values
90     * @see org.hamcrest.CoreMatchers
91     * @see org.junit.matchers.JUnitMatchers
92     */
93    public static <T> void assumeThat(T actual, Matcher<T> matcher) {
94        if (!matcher.matches(actual)) {
95            throw new AssumptionViolatedException(actual, matcher);
96        }
97    }
98
99    /**
100     * Call to assume that <code>actual</code> satisfies the condition specified by <code>matcher</code>.
101     * If not, the test halts and is ignored.
102     * Example:
103     * <pre>:
104     *   assumeThat(1, is(1)); // passes
105     *   foo(); // will execute
106     *   assumeThat(0, is(1)); // assumption failure! test halts
107     *   int x = 1 / 0; // will never execute
108     * </pre>
109     *
110     * @param <T> the static type accepted by the matcher (this can flag obvious compile-time problems such as {@code assumeThat(1, is("a"))}
111     * @param actual the computed value being compared
112     * @param matcher an expression, built of {@link Matcher}s, specifying allowed values
113     * @see org.hamcrest.CoreMatchers
114     * @see org.junit.matchers.JUnitMatchers
115     */
116    public static <T> void assumeThat(String message, T actual, Matcher<T> matcher) {
117        if (!matcher.matches(actual)) {
118            throw new AssumptionViolatedException(message, actual, matcher);
119        }
120    }
121
122    /**
123     * Use to assume that an operation completes normally.  If {@code t} is non-null, the test will halt and be ignored.
124     *
125     * For example:
126     * <pre>
127     * \@Test public void parseDataFile() {
128     *   DataFile file;
129     *   try {
130     *     file = DataFile.open("sampledata.txt");
131     *   } catch (IOException e) {
132     *     // stop test and ignore if data can't be opened
133     *     assumeNoException(e);
134     *   }
135     *   // ...
136     * }
137     * </pre>
138     *
139     * @param t if non-null, the offending exception
140     */
141    public static void assumeNoException(Throwable t) {
142        assumeThat(t, nullValue());
143    }
144
145    /**
146     * Attempts to halt the test and ignore it if Throwable <code>t</code> is
147     * not <code>null</code>. Similar to {@link #assumeNoException(Throwable)},
148     * but provides an additional message that can explain the details
149     * concerning the assumption.
150     *
151     * @param t if non-null, the offending exception
152     * @param message Additional message to pass to {@link AssumptionViolatedException}.
153     * @see #assumeNoException(Throwable)
154     */
155    public static void assumeNoException(String message, Throwable t) {
156        assumeThat(message, t, nullValue());
157    }
158}
159
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

Trigger assumeThat code on LambdaTest Cloud Grid

Execute automation tests with assumeThat 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)