How to use Interface Description class of org.hamcrest package

Best junit code snippet using org.hamcrest.Interface Description

Run junit automation tests on LambdaTest cloud grid

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

copy
1package org.hamcrest.object;
2
3import org.hamcrest.AbstractMatcherTest;
4import org.hamcrest.Matcher;
5
6import static org.hamcrest.MatcherAssert.assertThat;
7import static org.hamcrest.object.IsCompatibleType.typeCompatibleWith;
8
9public class IsCompatibleTypeTest extends AbstractMatcherTest {
10    public static class BaseClass {
11    }
12
13    public static class ExtendedClass extends BaseClass {
14    }
15
16    public interface BaseInterface {
17    }
18
19    public interface ExtendedInterface extends BaseInterface {
20    }
21
22    public static class ClassImplementingBaseInterface implements BaseInterface {
23    }
24
25    @Override
26    protected Matcher<?> createMatcher() {
27        return typeCompatibleWith(BaseClass.class);
28    }
29
30    public void testMatchesSameClass() {
31        assertThat(BaseClass.class, typeCompatibleWith(BaseClass.class));
32    }
33
34    public void testMatchesSameInterface() {
35        assertThat(BaseInterface.class, typeCompatibleWith(BaseInterface.class));
36    }
37
38    public void testMatchesExtendedClass() {
39        assertThat(ExtendedClass.class, typeCompatibleWith(BaseClass.class));
40    }
41
42    public void testMatchesClassImplementingInterface() {
43        assertThat(ClassImplementingBaseInterface.class, typeCompatibleWith(BaseInterface.class));
44    }
45
46    public void testMatchesExtendedInterface() {
47        assertThat(ExtendedInterface.class, typeCompatibleWith(BaseInterface.class));
48    }
49
50//    public void testDoesNotMatchIncompatibleTypes() {
51//        assertThat(BaseClass.class, not(compatibleType(ExtendedClass.class)));
52//        assertThat(Integer.class, not(compatibleType(String.class)));
53//    }
54
55    public void testHasReadableDescription() {
56        assertDescription("type < java.lang.Runnable", typeCompatibleWith(Runnable.class));
57    }
58}
59
Full Screen
copy
1package net.amygdalum.xrayinterface;
2
3import static org.hamcrest.CoreMatchers.containsString;
4import static org.hamcrest.CoreMatchers.equalTo;
5import static org.hamcrest.MatcherAssert.assertThat;
6
7import org.hamcrest.Description;
8import org.hamcrest.StringDescription;
9import org.junit.Test;
10
11public class XRayMatcherTest {
12
13	@Test
14	public void testDescribeTo() throws Exception {
15		XRayMatcher matcher = new XRayMatcher(UnlockedInterface.class);
16		Description description = new StringDescription();
17
18		matcher.describeTo(description);
19
20		assertThat(description.toString(), equalTo("can unlock features of <interface net.amygdalum.xrayinterface.XRayMatcherTest$UnlockedInterface>"));
21	}
22
23	@Test
24	public void testDescribeMismatchSafely() throws Exception {
25		XRayMatcher matcher = new XRayMatcher(UnlockedInterface.class);
26		Description description = new StringDescription();
27
28		matcher.describeMismatch(Object.class, description);
29
30		assertThat(description.toString(), containsString("cannot map following members in <class java.lang.Object>: "));
31		assertThat(description.toString(), containsString("void setStr(String)"));
32		assertThat(description.toString(), containsString("int getI()"));
33	}
34
35	interface UnlockedInterface {
36
37		void setStr(String str);
38
39		int getI();
40	}
41
42}
43
Full Screen
copy
1    private String foo;
2    public String getFoo(){...}
3    public void setFoo(String foo){...}; 
4
Full Screen
copy
1     *combo 1* : JPA + JPA Provider(Hibernate) 
2     *combo 2* : only Hiberante which does not need any interface 
3
Full Screen
copy
1interface Forgiver {
2    void forgive();
3}
4
5abstract class GodLike implements Forgiver {
6    abstract void forget();
7    final void forgive() {
8        forget();
9    }
10}
11
Full Screen
copy
1class HumanLike implements Forgiver {
2    void forgive() {
3       // forgive but remember    
4    }
5}
6
Full Screen
copy
1class AngelLike extends GodLike {
2    void forget() {
3       // forget to forgive     
4    }
5}
6
Full Screen
copy
1public abstract class CTC {
2
3    public int salary(int hra, int da, int extra)
4    {
5        int total;
6        total = hra+da+extra;
7        //incentive for specific performing employee
8        //total = hra+da+extra+incentive;
9        return total;
10    }
11}
12
13class Manger extends CTC
14{
15}
16
17
18class CEO extends CTC
19{
20}
21
22class Developer extends CTC
23{   
24}
25
Full Screen
copy
1public interface EmployeType {
2
3    public String typeOfEmployee();
4}
5
6class ContarctOne implements EmployeType
7{
8
9    @Override
10    public String typeOfEmployee() {
11        return "contract";
12    }
13
14}
15
16class PermanentOne implements EmployeType
17{
18
19    @Override
20    public String typeOfEmployee() {
21        return "permanent";
22    }
23
24}
25
Full Screen
copy
1package adventure;
2import java.awt.*;
3public interface Playable {
4    public void playSound(String s);
5    public Image loadPicture(String s);    
6}
7
Full Screen
copy
1public abstract class World...
2
3    public Playable owner;
4
5    public Playable getOwner() {
6        return owner;
7    }
8
9    public void setOwner(Playable owner) {
10        this.owner = owner;
11    }
12
Full Screen
copy
1import java.time.*;
2
3public interface TimeClient {
4    void setTime(int hour, int minute, int second);
5    void setDate(int day, int month, int year);
6    void setDateAndTime(int day, int month, int year,
7                               int hour, int minute, int second);
8    LocalDateTime getLocalDateTime();
9
10    static ZoneId getZoneId (String zoneString) {
11        try {
12            return ZoneId.of(zoneString);
13        } catch (DateTimeException e) {
14            System.err.println("Invalid time zone: " + zoneString +
15                "; using default time zone instead.");
16            return ZoneId.systemDefault();
17        }
18    }
19
20    default ZonedDateTime getZonedDateTime(String zoneString) {
21        return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
22    }
23}
24
Full Screen
copy
11.1.1. Abstract classes versus interfaces in Java 8
21.1.2. Conceptual Difference:
3
Full Screen
copy
11.2.1. What is Default Method?
21.2.2. ForEach method compilation error solved using Default Method
31.2.3. Default Method and Multiple Inheritance Ambiguity Problems
41.2.4. Important points about java interface default methods:
5
Full Screen
copy
11.3.1. Java Interface Static Method, code example, static method vs default method
21.3.2. Important points about java interface static method:
3
Full Screen
copy
1public interface OldInterface {
2    public void existingMethod();
3 
4    default public void newDefaultMethod() {
5        System.out.println("New default method"
6               + " is added in interface");
7    }
8}
9
Full Screen
copy
1public class OldInterfaceImpl implements OldInterface {
2    public void existingMethod() {
3     // existing implementation is here…
4    }
5}
6
Full Screen
copy
1OldInterfaceImpl obj = new OldInterfaceImpl ();
2// print ‚ÄúNew default method add in interface‚ÄĚ
3obj.newDefaultMethod(); 
4
Full Screen
copy
1public interface Iterable<T> {
2    public void forEach(Consumer<? super T> consumer);
3}
4
Full Screen
copy
1public interface Iterable<T> {
2    public default void forEach(Consumer
3                   <? super T> consumer) {
4        for (T t : this) {
5            consumer.accept(t);
6        }
7    }
8}
9
Full Screen
copy
1public interface InterfaceA {  
2       default void defaultMethod(){  
3           System.out.println("Interface A default method");  
4    }  
5}
6 
7public interface InterfaceB {
8   default void defaultMethod(){
9       System.out.println("Interface B default method");
10   }
11}
12 
13public class Impl implements InterfaceA, InterfaceB  {
14}
15
Full Screen
copy
1public class Impl implements InterfaceA, InterfaceB {
2    public void defaultMethod(){
3    }
4}
5
Full Screen
copy
1public class Impl implements InterfaceA, InterfaceB {
2    public void defaultMethod(){
3        // existing code here..
4        InterfaceA.super.defaultMethod();
5    }
6}
7
Full Screen
copy
1public interface MyData {
2
3    default void print(String str) {
4        if (!isNull(str))
5            System.out.println("MyData Print::" + str);
6    }
7
8    static boolean isNull(String str) {
9        System.out.println("Interface Null Check");
10
11        return str == null ? true : "".equals(str) ? true : false;
12    }
13}
14
Full Screen
copy
1public class MyDataImpl implements MyData {
2
3    public boolean isNull(String str) {
4        System.out.println("Impl Null Check");
5
6        return str == null ? true : false;
7    }
8    
9    public static void main(String args[]){
10        MyDataImpl obj = new MyDataImpl();
11        obj.print("");
12        obj.isNull("abc");
13    }
14}
15
Full Screen
copy
1public interface Payment
2{
3    void makePayment();//by default it is a abstract method
4}
5public class PayPal implements Payment
6{
7    public void makePayment()
8    {
9        //some logic for PayPal payment
10        //e.g. Paypal uses username and password for payment
11    }
12}
13public class CreditCard implements Payment
14{
15    public void makePayment()
16    {
17        //some logic for CreditCard payment
18        //e.g. CreditCard uses card number, date of expiry etc...
19    }
20}
21
Full Screen
copy
1public abstract class Burger
2{
3    public void packing()
4    {
5        //some logic for packing a burger
6    }
7    public abstract void price(); //price is different for different categories of burgers
8}
9public class VegBerger extends Burger
10{
11    public void price()
12    {
13        //set price for a veg burger.
14    }
15}
16public class NonVegBerger extends Burger
17{
18    public void price()
19    {
20        //set price for a non-veg burger.
21    }
22}
23
Full Screen
copy
1public interface SomeInterfaceOne {
2
3    void usualAbstractMethod(String inputString);
4
5    default void defaultMethod(String inputString){
6        System.out.println("Inside SomeInterfaceOne defaultMethod::"+inputString);
7    }
8}
9
Full Screen
copy
1public interface SomeInterfaceTwo {
2
3    void usualAbstractMethod(String inputString);
4
5    default void defaultMethod(String inputString){
6        System.out.println("Inside SomeInterfaceTwo defaultMethod::"+inputString);
7    }
8
9}
10
Full Screen
copy
1public interface LoginAuth{
2   public String encryptPassword(String pass);
3   public void checkDBforUser();
4}
5
Full Screen
copy
1public class DBMySQL implements LoginAuth{
2          // Needs to implement both methods
3}
4public class DBOracle implements LoginAuth{
5          // Needs to implement both methods
6}
7public class DBAbc implements LoginAuth{
8          // Needs to implement both methods
9}
10
Full Screen
copy
1public abstract class LoginAuth{
2   public String encryptPassword(String pass){
3            // Implement the same default behavior here 
4            // that is shared by all subclasses.
5   }
6
7   // Each subclass needs to provide their own implementation of this only:
8   public abstract void checkDBforUser();
9}
10
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 methods in Interface-Description

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)