BAEL-4220 | A Guide to IllegalAccessError and when it happens (#10798)

* BAEL-4220 | A Guide to IllegalAccessError and when it happens

* BAEL-4220 | A Guide to IllegalAccessError and when it happens | fix tests

* BAEL-4220 | A Guide to IllegalAccessError and when it happens | fix tests

* BAEL-4220 | A Guide to IllegalAccessError and when it happens | BDD test names
This commit is contained in:
anmoldeep0123 2021-05-25 00:27:21 +05:30 committed by GitHub
parent 263bfcfdd1
commit fa55f989d6
6 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package com.baeldung.exceptions.illegalaccesserror;
public class Class1 {
public void bar() {
System.out.println("SUCCESS");
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.exceptions.illegalaccesserror;
public class Class2 {
public void foo() {
Class1 c1 = new Class1();
c1.bar();
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.exceptions.illegalaccesserror;
public class IllegalAccessErrorExample {
interface Baeldung {
public default void foobar() {
System.out.println("This is a default method.");
}
}
class Super {
private void foobar() {
System.out.println("SuperClass method foobar");
}
}
class MySubClass extends Super implements Baeldung {
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.exceptions.illegalaccesserror;
public class IllegalAccessErrorSolved {
interface BaeldungSolved {
public default void foobar() {
System.out.println("This is a default method.");
}
}
class SuperSolved {
public void foobar() {
System.out.println("SuperClass method foobar");
}
}
class MySubClassSolved extends SuperSolved implements BaeldungSolved {
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.exceptions.illegalaccesserror;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class IllegalAccessErrorExampleUnitTest {
@Test()
public void givenInterfaceDefaultMethOverriddenPrivateAccess_whenInvoked_thenIllegalAccessError() {
Assertions.assertThrows(IllegalAccessError.class, () -> {
new IllegalAccessErrorExample().new MySubClass().foobar();
});
}
@Test()
public void givenClass1Class2_whenSameClassDefintion_thenNoIllegalAccessError() {
Assertions.assertDoesNotThrow(() -> {
new Class2().foo();
});
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.exceptions.illegalaccesserror;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class IllegalAccessErrorSolvedUnitTest {
@Test()
public void givenInterfaceDefaultMethOverriddenNonPrivateAccess_whenInvoked_thenNoIllegalAccessError() {
Assertions.assertDoesNotThrow(() -> {
new IllegalAccessErrorSolved().new MySubClassSolved().foobar();
});
}
}