Run junit automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
package org.hamcrest.object;
import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.object.IsCompatibleType.typeCompatibleWith;
public class IsCompatibleTypeTest extends AbstractMatcherTest {
public static class BaseClass {
}
public static class ExtendedClass extends BaseClass {
}
public interface BaseInterface {
}
public interface ExtendedInterface extends BaseInterface {
}
public static class ClassImplementingBaseInterface implements BaseInterface {
}
@Override
protected Matcher<?> createMatcher() {
return typeCompatibleWith(BaseClass.class);
}
public void testMatchesSameClass() {
assertThat(BaseClass.class, typeCompatibleWith(BaseClass.class));
}
public void testMatchesSameInterface() {
assertThat(BaseInterface.class, typeCompatibleWith(BaseInterface.class));
}
public void testMatchesExtendedClass() {
assertThat(ExtendedClass.class, typeCompatibleWith(BaseClass.class));
}
public void testMatchesClassImplementingInterface() {
assertThat(ClassImplementingBaseInterface.class, typeCompatibleWith(BaseInterface.class));
}
public void testMatchesExtendedInterface() {
assertThat(ExtendedInterface.class, typeCompatibleWith(BaseInterface.class));
}
// public void testDoesNotMatchIncompatibleTypes() {
// assertThat(BaseClass.class, not(compatibleType(ExtendedClass.class)));
// assertThat(Integer.class, not(compatibleType(String.class)));
// }
public void testHasReadableDescription() {
assertDescription("type < java.lang.Runnable", typeCompatibleWith(Runnable.class));
}
}
package net.amygdalum.xrayinterface;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import org.hamcrest.Description;
import org.hamcrest.StringDescription;
import org.junit.Test;
public class XRayMatcherTest {
@Test
public void testDescribeTo() throws Exception {
XRayMatcher matcher = new XRayMatcher(UnlockedInterface.class);
Description description = new StringDescription();
matcher.describeTo(description);
assertThat(description.toString(), equalTo("can unlock features of <interface net.amygdalum.xrayinterface.XRayMatcherTest$UnlockedInterface>"));
}
@Test
public void testDescribeMismatchSafely() throws Exception {
XRayMatcher matcher = new XRayMatcher(UnlockedInterface.class);
Description description = new StringDescription();
matcher.describeMismatch(Object.class, description);
assertThat(description.toString(), containsString("cannot map following members in <class java.lang.Object>: "));
assertThat(description.toString(), containsString("void setStr(String)"));
assertThat(description.toString(), containsString("int getI()"));
}
interface UnlockedInterface {
void setStr(String str);
int getI();
}
}
private String foo;
public String getFoo(){...}
public void setFoo(String foo){...};
*combo 1* : JPA + JPA Provider(Hibernate)
*combo 2* : only Hiberante which does not need any interface
interface Forgiver {
void forgive();
}
abstract class GodLike implements Forgiver {
abstract void forget();
final void forgive() {
forget();
}
}
class HumanLike implements Forgiver {
void forgive() {
// forgive but remember
}
}
class AngelLike extends GodLike {
void forget() {
// forget to forgive
}
}
public abstract class CTC {
public int salary(int hra, int da, int extra)
{
int total;
total = hra+da+extra;
//incentive for specific performing employee
//total = hra+da+extra+incentive;
return total;
}
}
class Manger extends CTC
{
}
class CEO extends CTC
{
}
class Developer extends CTC
{
}
public interface EmployeType {
public String typeOfEmployee();
}
class ContarctOne implements EmployeType
{
@Override
public String typeOfEmployee() {
return "contract";
}
}
class PermanentOne implements EmployeType
{
@Override
public String typeOfEmployee() {
return "permanent";
}
}
package adventure;
import java.awt.*;
public interface Playable {
public void playSound(String s);
public Image loadPicture(String s);
}
public abstract class World...
public Playable owner;
public Playable getOwner() {
return owner;
}
public void setOwner(Playable owner) {
this.owner = owner;
}
import java.time.*;
public interface TimeClient {
void setTime(int hour, int minute, int second);
void setDate(int day, int month, int year);
void setDateAndTime(int day, int month, int year,
int hour, int minute, int second);
LocalDateTime getLocalDateTime();
static ZoneId getZoneId (String zoneString) {
try {
return ZoneId.of(zoneString);
} catch (DateTimeException e) {
System.err.println("Invalid time zone: " + zoneString +
"; using default time zone instead.");
return ZoneId.systemDefault();
}
}
default ZonedDateTime getZonedDateTime(String zoneString) {
return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
}
}
1.1.1. Abstract classes versus interfaces in Java 8
1.1.2. Conceptual Difference:
1.2.1. What is Default Method?
1.2.2. ForEach method compilation error solved using Default Method
1.2.3. Default Method and Multiple Inheritance Ambiguity Problems
1.2.4. Important points about java interface default methods:
1.3.1. Java Interface Static Method, code example, static method vs default method
1.3.2. Important points about java interface static method:
public interface OldInterface {
public void existingMethod();
default public void newDefaultMethod() {
System.out.println("New default method"
+ " is added in interface");
}
}
public class OldInterfaceImpl implements OldInterface {
public void existingMethod() {
// existing implementation is here…
}
}
OldInterfaceImpl obj = new OldInterfaceImpl ();
// print “New default method add in interface”
obj.newDefaultMethod();
public interface Iterable<T> {
public void forEach(Consumer<? super T> consumer);
}
public interface Iterable<T> {
public default void forEach(Consumer
<? super T> consumer) {
for (T t : this) {
consumer.accept(t);
}
}
}
public interface InterfaceA {
default void defaultMethod(){
System.out.println("Interface A default method");
}
}
public interface InterfaceB {
default void defaultMethod(){
System.out.println("Interface B default method");
}
}
public class Impl implements InterfaceA, InterfaceB {
}
public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){
}
}
public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){
// existing code here..
InterfaceA.super.defaultMethod();
}
}
public interface MyData {
default void print(String str) {
if (!isNull(str))
System.out.println("MyData Print::" + str);
}
static boolean isNull(String str) {
System.out.println("Interface Null Check");
return str == null ? true : "".equals(str) ? true : false;
}
}
public class MyDataImpl implements MyData {
public boolean isNull(String str) {
System.out.println("Impl Null Check");
return str == null ? true : false;
}
public static void main(String args[]){
MyDataImpl obj = new MyDataImpl();
obj.print("");
obj.isNull("abc");
}
}
boolean result = MyData.isNull("abc");
public interface Payment
{
void makePayment();//by default it is a abstract method
}
public class PayPal implements Payment
{
public void makePayment()
{
//some logic for PayPal payment
//e.g. Paypal uses username and password for payment
}
}
public class CreditCard implements Payment
{
public void makePayment()
{
//some logic for CreditCard payment
//e.g. CreditCard uses card number, date of expiry etc...
}
}
public abstract class Burger
{
public void packing()
{
//some logic for packing a burger
}
public abstract void price(); //price is different for different categories of burgers
}
public class VegBerger extends Burger
{
public void price()
{
//set price for a veg burger.
}
}
public class NonVegBerger extends Burger
{
public void price()
{
//set price for a non-veg burger.
}
}
public interface SomeInterfaceOne {
void usualAbstractMethod(String inputString);
default void defaultMethod(String inputString){
System.out.println("Inside SomeInterfaceOne defaultMethod::"+inputString);
}
}
public interface SomeInterfaceTwo {
void usualAbstractMethod(String inputString);
default void defaultMethod(String inputString){
System.out.println("Inside SomeInterfaceTwo defaultMethod::"+inputString);
}
}
SomeInterfaceOne and SomeInterfaceTwo
public interface LoginAuth{
public String encryptPassword(String pass);
public void checkDBforUser();
}
public class DBMySQL implements LoginAuth{
// Needs to implement both methods
}
public class DBOracle implements LoginAuth{
// Needs to implement both methods
}
public class DBAbc implements LoginAuth{
// Needs to implement both methods
}
public abstract class LoginAuth{
public String encryptPassword(String pass){
// Implement the same default behavior here
// that is shared by all subclasses.
}
// Each subclass needs to provide their own implementation of this only:
public abstract void checkDBforUser();
}
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.