Skip to content

Commit 7449394

Browse files
BAEL-4220 | A Guide to IllegalAccessError and when it happens (eugenp#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
1 parent c36d61f commit 7449394

6 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.exceptions.illegalaccesserror;
2+
3+
public class Class1 {
4+
5+
public void bar() {
6+
System.out.println("SUCCESS");
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.exceptions.illegalaccesserror;
2+
3+
public class Class2 {
4+
5+
public void foo() {
6+
Class1 c1 = new Class1();
7+
c1.bar();
8+
}
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.exceptions.illegalaccesserror;
2+
3+
public class IllegalAccessErrorExample {
4+
5+
interface Baeldung {
6+
public default void foobar() {
7+
System.out.println("This is a default method.");
8+
}
9+
}
10+
11+
class Super {
12+
private void foobar() {
13+
System.out.println("SuperClass method foobar");
14+
}
15+
}
16+
17+
class MySubClass extends Super implements Baeldung {
18+
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.exceptions.illegalaccesserror;
2+
3+
public class IllegalAccessErrorSolved {
4+
5+
interface BaeldungSolved {
6+
public default void foobar() {
7+
System.out.println("This is a default method.");
8+
}
9+
}
10+
11+
class SuperSolved {
12+
public void foobar() {
13+
System.out.println("SuperClass method foobar");
14+
}
15+
}
16+
17+
class MySubClassSolved extends SuperSolved implements BaeldungSolved {
18+
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.exceptions.illegalaccesserror;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class IllegalAccessErrorExampleUnitTest {
7+
8+
@Test()
9+
public void givenInterfaceDefaultMethOverriddenPrivateAccess_whenInvoked_thenIllegalAccessError() {
10+
Assertions.assertThrows(IllegalAccessError.class, () -> {
11+
new IllegalAccessErrorExample().new MySubClass().foobar();
12+
});
13+
}
14+
15+
@Test()
16+
public void givenClass1Class2_whenSameClassDefintion_thenNoIllegalAccessError() {
17+
Assertions.assertDoesNotThrow(() -> {
18+
new Class2().foo();
19+
});
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.exceptions.illegalaccesserror;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class IllegalAccessErrorSolvedUnitTest {
7+
8+
@Test()
9+
public void givenInterfaceDefaultMethOverriddenNonPrivateAccess_whenInvoked_thenNoIllegalAccessError() {
10+
Assertions.assertDoesNotThrow(() -> {
11+
new IllegalAccessErrorSolved().new MySubClassSolved().foobar();
12+
});
13+
}
14+
}

0 commit comments

Comments
 (0)