Showing posts with label JUnit Test. Show all posts
Showing posts with label JUnit Test. Show all posts

2023-04-06

[Solved] - object to JSON string exception


Relevant tags:
  1. Spring Boot 2.7.6
  2. JUnit 5
  3. JPA
 

Error Message:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.yourcompany.entity.Table$HibernateProxy$3enmhWuU["hibernateLazyInitializer"])


Reference URL: https://stackoverflow.com/a/67354659/6462295

Adding annotation below to your entity.

@com.fasterxml.jackson.annotation.JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
















[Solved] - LazyInitializationException: could not initialize proxy

 


org.hibernate.LazyInitializationException: could not initialize proxy [com.yourcompany.entity.Table#123456] - no Session


relevant tags:
  1. Spring Boot 2.7.6
  2. JUnit 5
  3. JPA

Resolved by both below:

1. Adding annotation below to your test function:
Reference URL: https://stackoverflow.com/a/72863346/6462295
@org.springframework.transaction.annotation.Transactional(readOnly = true)





2. Adding configuration into application.properties
Reference URL: https://stackoverflow.com/a/38690930/6462295

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true






2022-06-10

JUnit test - Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly

 

Exception snippet:

12:02:34.849|WARN |Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@1a84f40f] to process 'after' execution for test: method [public void test.com.tiongnam.repositories.TestInvoicePackageRepo.testListPossiblePackageWithInvoiceIdAndSummary()], instance [test.com.tiongnam.repositories.TestInvoicePackageRepo@ca2cd5e], exception [java.lang.IndexOutOfBoundsException: Index: 1, Size: 1]|main|org.springframework.test.context.TestContextManager
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.test.context.transaction.TransactionContext.endTransaction(TransactionContext.java:128)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:227)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:319)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:94)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
...
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:58)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
... 26 common frames omitted





@Rollback(false)
MyJUnitTestClass


Calling a @Transactional Repo



change
@Rollback(true)






2021-03-26

Using JUnit 5 in Spring Boot 1.5.18/1.5.19?

 

Source URL: https://stackoverflow.com/a/48031452

To migrate from JUnit 4 to JUnit 5 you can replace @RunWith(SpringRunner.class) with @ExtendWith(SpringExtension.class).

Unfortunately, spring-boot version 1.5.9-RELEASE is based on Spring 4 and the SpringExtension is only available since Spring 5.

However, there is a solution since it's possible to use JUnit 5 and SpringExtension in Spring 4 by using spring-test-junit5. Just check the instructions for setting up the dependencies.


Billson@20210326_pm034909 : verified that Spring 4 is still using by Spring Boot 1.5.18/1.5.19







after downgrade/upgrade code, then also need to make sure your test runner is using JUnit 4.







Note: Failed to paste printscreen with error:
'' failed to upload. Invalid response: Cannot read property 'Yd' of null





2021-01-09

@RunWith(SpringRunner.class) vs @RunWith(SpringJUnit4ClassRunner.class)

 




What is the difference between SpringJUnit4ClassRunner and SpringRunner



Source URL: https://stackoverflow.com/a/50522420

.

@RunWith(SpringRunner.class) tells JUnit to run using Spring’s testing support. SpringRunner is the new name for SpringJUnit4ClassRunner, it’s just a bit easier on the eye.

SpringRunner is only available on spring-test 4.3.

SpringRunner class extends SpringJUnit4ClassRunner.

Source code of SpringRunner is

package org.springframework.test.context.junit4;

import org.junit.runners.model.InitializationError;

public final class SpringRunner extends SpringJUnit4ClassRunner {

    public SpringRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }

}
.








2021-01-08

In Short: What's JUnit 5

 
Source URL: https://stackoverflow.com/a/58384499

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage




Source URL: https://junit.org/junit5/docs/current/user-guide/#overview-what-is-junit-5

.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform. Furthermore, the platform provides a Console Launcher to launch the platform from the command line and a JUnit 4 based Runner for running any TestEngine on the platform in a JUnit 4 based environment. First-class support for the JUnit Platform also exists in popular IDEs (see IntelliJ IDEAEclipseNetBeans, and Visual Studio Code) and build tools (see GradleMaven, and Ant).

JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform.

JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

Video Briefing



What's JUnit Platform?
You only need the launcher when you want to start a JUnit platform run programmatically, i.e. outside an IDE, build tool or console runner.

In other words: the launcher is the API being used by IDEs and build tools






2019-05-14

Spring 3.0 with JUnit Test 4


Reference URLs:

  1. https://yuangaopeng.com/2018/12/05/Spring%20Testing%20classpath/
  2. https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#integration-testing-annotations




For JUnit test with spring, either you load context configuration:

  1. manual parse xml [ApplicationContext _ctx = ClassPathXmlApplicationContext( "abc/xyz/spring-context-all.xml" ) ], access beans via _ctx.getBean()
  2. or annotation to let spring auto wired all your attributes of test cases.



And using annotation of @ContextConfiguration, I've preference to use classpath for reasons below:

  1. Spring JUnit test cases able to share xml info.
  2. multi developer environment competible.











Google Referrals