99import org .hibernate .orm .test .jpa .model .AbstractJPATest ;
1010import org .hibernate .orm .test .jpa .model .Item ;
1111import org .junit .jupiter .api .Test ;
12- import junit .framework .AssertionFailedError ;
1312
1413import jakarta .persistence .EntityNotFoundException ;
1514
1615import static org .junit .jupiter .api .Assertions .assertFalse ;
1716import static org .junit .jupiter .api .Assertions .assertNull ;
18- import static org .junit .jupiter .api .Assertions .fail ;
19-
17+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2018
2119/**
2220 * Test relation between proxies and get()/load() processing
@@ -30,34 +28,24 @@ public class JPAProxyTest extends AbstractJPATest {
3028 public void testEjb3ProxyUsage () {
3129 inTransaction (
3230 s -> {
33- Item item = s .getReference ( Item .class , new Long ( - 1 ) );
31+ Item item = s .getReference ( Item .class , - 1L );
3432 assertFalse ( Hibernate .isInitialized ( item ) );
35- try {
36- Hibernate .initialize ( item );
37- fail ( "proxy access did not fail on non-existent proxy" );
38- }
39- catch (EntityNotFoundException e ) {
40- // expected behavior
41- }
42- catch (Throwable t ) {
43- fail ( "unexpected exception type on non-existent proxy access : " + t );
44- }
33+ assertThrows (
34+ EntityNotFoundException .class ,
35+ () -> Hibernate .initialize (item ),
36+ "proxy access did not fail on non-existent proxy"
37+ );
4538
4639 s .clear ();
4740
48- Item item2 = s .getReference ( Item .class , new Long ( - 1 ) );
41+ Item item2 = s .getReference ( Item .class , - 1L );
4942 assertFalse ( Hibernate .isInitialized ( item2 ) );
5043 assertFalse ( item == item2 );
51- try {
52- item2 .getName ();
53- fail ( "proxy access did not fail on non-existent proxy" );
54- }
55- catch (EntityNotFoundException e ) {
56- // expected behavior
57- }
58- catch (Throwable t ) {
59- fail ( "unexpected exception type on non-existent proxy access : " + t );
60- }
44+ assertThrows (
45+ EntityNotFoundException .class ,
46+ item2 ::getName ,
47+ "proxy access did not fail on non-existent proxy"
48+ );
6149 }
6250 );
6351 }
@@ -67,7 +55,7 @@ public void testEjb3ProxyUsage() {
6755 */
6856 @ Test
6957 public void testGetSemantics () {
70- Long nonExistentId = new Long ( - 1 ) ;
58+ Long nonExistentId = - 1L ;
7159 inTransaction (
7260 session -> {
7361 Item item = session .get ( Item .class , nonExistentId );
@@ -81,19 +69,11 @@ public void testGetSemantics() {
8169 Item item = s .getReference ( Item .class , nonExistentId );
8270 assertFalse ( Hibernate .isInitialized ( item ) );
8371 // then try to get() it to make sure we get an exception
84- try {
85- s .get ( Item .class , nonExistentId );
86- fail ( "force load did not fail on non-existent entity" );
87- }
88- catch (EntityNotFoundException e ) {
89- // expected behavior
90- }
91- catch (AssertionFailedError e ) {
92- throw e ;
93- }
94- catch (Throwable t ) {
95- fail ( "unexpected exception type on non-existent entity force load : " + t );
96- }
72+ assertThrows (
73+ EntityNotFoundException .class ,
74+ () -> s .get ( Item .class , nonExistentId ),
75+ "force load did not fail on non-existent entity"
76+ );
9777 }
9878 );
9979 }
0 commit comments