Spring's #DirtiesContext leads to Hibernate exception - spring

I have a JUnit integration test which tests the stability of an Apache CXF webservice. The test inherits the webserviceTestclient which does a real webservice call to an endpoint. To simulate an error in the application, I mock an endpoint dependency to provoke an exception.
The challenge here is: How to reset the endpoint's dependency or how to recreate Spring's application context so that subsequent tests will run their tests properly with a clean context and unmocked beans. My best approach so far:
Versions used: Spring 3.2.3, Hibernate 4.2.5, Mockito 1.9.5
Method to test:
#Override
public boolean foo() throws FooException
{
try
{
return this.someService.foo();
}
catch (Exception exception)
{
throw this.convertToFooException(exception);
}
}
JUnit test:
public class IntegrationTest extends WebserviceTestbase
{
#InjectMocks
private ClassToTest classToTest;
#Mock
private SomeService someServiceMock;
#Test
#DirtiesContext
public void thisTestFails()
{
// given
when(this.someServiceMock.foo().thenThrow(new RuntimeException());
try
{
// when
this.webserviceTestclient.foo();
fail();
}
catch (FooException FooException)
{
// then test passed
}
}
}
When I execute the test above the test fails and I get the following stacktrace:
org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is javax.persistence.PersistenceException: unexpected error when rollbacking
at org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:543)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:846)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:823)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:588)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:297)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:192)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:395)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:91)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: javax.persistence.PersistenceException: unexpected error when rollbacking
at org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:109)
at org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:539)
... 25 more
Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.stat.spi.StatisticsImplementor]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:126)
at org.hibernate.internal.SessionFactoryImpl.getStatisticsImplementor(SessionFactoryImpl.java:1480)
at org.hibernate.internal.SessionFactoryImpl.getStatistics(SessionFactoryImpl.java:1476)
at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.afterTransaction(TransactionCoordinatorImpl.java:140)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.afterTransactionCompletion(JdbcTransaction.java:138)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:214)
at org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:106)
... 26 more
Hope that anyone can help.
Best regards!

As a works-for-me-in-a-dirty-way-solution, I now changed the dependency via reflection:
#Autowired
private ClassToTest classToTest;
private SomeService someServiceBean;
#Override
#Before
public void setup()
{
super.setup();
// remember the currect dependency and replace it with null to provoke a RuntimeException
this.someServiceBean = (SomeService ) ReflectionTools.getFieldValue(this.classToTest, "someService");
ReflectionTools.setFieldValue(this.classToTest, "someService", null);
}
#Test
public void thisTestNowPasses()
{
// given
try
{
// when
this.webserviceTestclient.foo();
fail();
}
catch (FooException FooException)
{
// then test passed
}
}
#Override
#After
public void cleanup()
{
super.cleanup();
// restore the dependency so other tests won't fail
ReflectionTools.setFieldValue(this.classToTest, "someService", this.someServiceBean);
}
Here are the ReflectionTools:
public class ReflectionTools
{
public static Object getFieldValue(Object instanceToInspect, String fieldName)
{
Object fieldValue = null;
try
{
Field declaredFieldToGet = instanceToInspect.getClass().getDeclaredField(fieldName);
declaredFieldToGet.setAccessible(true);
fieldValue = declaredFieldToGet.get(instanceToInspect);
declaredFieldToGet.setAccessible(false);
}
catch (Exception exception)
{
String className = exception.getClass().getCanonicalName();
String message = exception.getMessage();
String errorFormat = "\n\t'%s' caught when getting value of field '%s': %s";
String error = String.format(errorFormat, className, fieldName, message);
Assert.fail(error);
}
return fieldValue;
}
public static void setFieldValue(Object instanceToModify, String fieldName, Object valueToSet)
{
try
{
Field declaredFieldToSet = instanceToModify.getClass().getDeclaredField(fieldName);
declaredFieldToSet.setAccessible(true);
declaredFieldToSet.set(instanceToModify, valueToSet);
declaredFieldToSet.setAccessible(false);
}
catch (Exception exception)
{
String className = exception.getClass().getCanonicalName();
String message = exception.getMessage();
String errorFormat = "\n\t'%s' caught when setting value of field '%s': %s";
String error = String.format(errorFormat, className, fieldName, message);
Assert.fail(error);
}
}
}
Hope that this helps someone.

Related

Quarkus EventBus requestandforget - timeout error in logs

When trying to use Quarkus (version 2.9.2.Final) EventBus requestAndForget with a #ConsumeEvent method that returns void, the following exception occurs in the logs, even though the processing occurs without any problem.
OK
2022-06-07 09:44:04,064 ERROR [io.qua.mut.run.MutinyInfrastructure]
(vert.x-eventloop-thread-1) Mutiny had to drop the following
exception: (TIMEOUT,-1) Timed out after waiting 30000(ms) for a reply.
address: __vertx.reply.3, repliedAddress: receivedSomeEvent
The consumer code:
#ApplicationScoped
public class ConsumerManiac{
#ConsumeEvent(value = "receivedSomeEvent")
public void consume(SomeEvent someEvent ) {
System.out.println("OK");
}
}
The Producer code (a REST Endpoint):
public class SomeResource {
private final EventBus eventBus;
#Inject
public SomeResource (EventBus eventBus) {
this.eventBus = eventBus;
}
#POST
public Response send(#Valid SomeEvent someEvent) {
eventBus.requestAndForget("receivedSomeEvent", someEvent);
return Response.accepted().build();
}
}
If the consumer method is changed to return some value, then the exception in logs does not occur.
#ApplicationScoped
public class ConsumerManiac{
#ConsumeEvent(value = "receivedSomeEvent")
public String consume(SomeEvent someEvent ) {
System.out.println("OK");
return "ok";
}
}
Is there any missing piece of code that is missing so the exception does not occur (even though processing concludes without any problem)?
Reference: https://quarkus.io/guides/reactive-event-bus#implementing-fire-and-forget-interactions
Full stacktrace:
2022-06-07 09:44:04,064 ERROR [io.qua.mut.run.MutinyInfrastructure]
(vert.x-eventloop-thread-1) Mutiny had to drop the following
exception: (TIMEOUT,-1) Timed out after waiting 30000(ms) for a reply.
address: __vertx.reply.3, repliedAddress: receivedSomeEvent at
io.vertx.core.eventbus.impl.ReplyHandler.handle(ReplyHandler.java:76)
at
io.vertx.core.eventbus.impl.ReplyHandler.handle(ReplyHandler.java:24)
at
io.vertx.core.impl.VertxImpl$InternalTimerHandler.handle(VertxImpl.java:893)
at
io.vertx.core.impl.VertxImpl$InternalTimerHandler.handle(VertxImpl.java:860)
at io.vertx.core.impl.EventLoopContext.emit(EventLoopContext.java:50)
at
io.vertx.core.impl.DuplicatedContext.emit(DuplicatedContext.java:168)
at io.vertx.core.impl.AbstractContext.emit(AbstractContext.java:53)
at
io.vertx.core.impl.VertxImpl$InternalTimerHandler.run(VertxImpl.java:883)
at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98)
at
io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:170)
at
io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at
io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:503) at
io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at
io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at
io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)
I had to return arbitrary value to avoid this exception.

spring-retry : "Getting Retry exhausted after last attempt with no recovery path" with original exception on non retryable method

I am trying to implement spring-retry(version - 1.3.1) in my spring boot application. I have to retry webservice operation to read the record if not found in first request.
sample code:
#Retryable(include = {IllegalArgumentException.class}, backoff = #Backoff(500), maxAttempts = 3, recover ="readFallback")
Object read(String Id);
#Recover
Object readFallback(RuntimeException e, String Id);
void deletePayment(String paymentId);
Problem :
I am getting correct response from read method(annotated with #Retryable) in exception scenario but I am getting RetryExhaustedException with nested original exception when I am getting exception on my delete method. As you see, delete method doesn't annotated with #Retryable . Delete method is in different package.
**Sample exception response ** : "Retry exhausted after last attempt with no recovery path; nested exception is exception.NotFoundException: Not found"
Expected : Delete method should not be impacted by #Retryable. Can someone help me to find what am i missing or doing wrong. I have tried but unable to not found the solution of this problem on internet.
Thanks in advance !
Works as expected for me:
#SpringBootApplication
#EnableRetry
public class So71546747Application {
public static void main(String[] args) {
SpringApplication.run(So71546747Application.class, args);
}
#Bean
ApplicationRunner runner(SomeRetryables retrier) {
return args -> {
retrier.foo("testFoo");
try {
Thread.sleep(1000);
retrier.bar("testBar");
}
catch (Exception e) {
e.printStackTrace();
}
};
}
}
#Component
class SomeRetryables {
#Retryable
void foo(String in) {
System.out.println(in);
throw new RuntimeException(in);
}
#Recover
void recover(String in, Exception ex) {
System.out.println("recovered");
}
void bar(String in) {
System.out.println(in);
throw new RuntimeException(in);
}
}
testFoo
testFoo
testFoo
recovered
testBar
java.lang.RuntimeException: testBar
at com.example.demo.SomeRetryables.bar(So71546747Application.java:52)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:789)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:166)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698)
at com.example.demo.SomeRetryables$$EnhancerBySpringCGLIB$$e61dd199.bar(<generated>)
at com.example.demo.So71546747Application.lambda$0(So71546747Application.java:26)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:768)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:310)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at com.example.demo.So71546747Application.main(So71546747Application.java:17)
Please provide an MCRE that exhibits the behavior you see so we can see what's wrong.

Spring / DbUnit closes connection after one test

So, we need to write integration tests for our Java-configurated Spring Application (3.2.3) with an Oracle data base. There's a separate schema which gets populated by Spring-test-DbUnit and the first test runs without any problems. All the other following tests, no matter if they're in the same class or not, fail due to a closed connection:
java.sql.SQLRecoverableException: closed connection
at oracle.jdbc.driver.PhysicalConnection.createStatement(PhysicalConnection.java:3423)
at oracle.jdbc.driver.PhysicalConnection.createStatement(PhysicalConnection.java:3398)
at org.dbunit.database.statement.AbstractBatchStatement.<init>(AbstractBatchStatement.java:50)
at org.dbunit.database.statement.SimpleStatement.<init>(SimpleStatement.java:49)
at org.dbunit.database.statement.PreparedStatementFactory.createBatchStatement(PreparedStatementFactory.java:57)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:85)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:194)
at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:66)
at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:186)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:348)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Here's our data source:
#Override
#Bean
public DataSource dataSource()
{
String conString = "jdbc:oracle:thin:#localhost:1521:XE";
String username = "john";
String password = "doe";
Driver driver = new OracleDriver();
SimpleDriverDataSource dataSource = new SimpleDriverDataSource(driver, conString, username, password);
return dataSource;
}
And this is a sample integration test class:
#ContextConfiguration(classes = { IntegrationTestConfiguration.class })
#ActiveProfiles(Constants.PROFILE_INTEGRATION)
#RunWith(SpringJUnit4ClassRunner.class)
#TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class, ForeignKeyDisabler.class })
#DbUnitConfiguration(databaseConnection = "oracleConnection")
#DatabaseSetup("/database/snapshot/snapshot1.xml")
public class IntegrationTest extends AbstractTransactionalJUnit4SpringContextTests
{
#Autowired
private NewsService newsService;
#Test
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void testSpringConfiguration()
{
this.assertNewsSize(1);
News news1 = new News();
news1.setTitle("Test News 1");
News savedNews1 = this.newsService.save(news1);
Assert.assertTrue(savedNews1.getId() > 0);
News news2 = new News();
news2.setTitle("Test News 2");
News savedNews2 = this.newsService.save(news2);
Assert.assertTrue(savedNews2.getId() > 0);
News news3 = new News();
news3.setTitle("Test News 3");
News savedNews3 = this.newsService.save(news3);
Assert.assertTrue(savedNews3.getId() > 0);
this.assertNewsSize(4);
}
#Test
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void testSpringConfigurationAgain()
{
this.assertNewsSize(1);
News news1 = new News();
news1.setTitle("Test News 1");
News savedNews1 = this.newsService.save(news1);
Assert.assertTrue(savedNews1.getId() > 0);
News news2 = new News();
news2.setTitle("Test News 2");
News savedNews2 = this.newsService.save(news2);
Assert.assertTrue(savedNews2.getId() > 0);
News news3 = new News();
news3.setTitle("Test News 3");
News savedNews3 = this.newsService.save(news3);
Assert.assertTrue(savedNews3.getId() > 0);
this.assertNewsSize(4);
}
private void assertNewsSize(int newsSize)
{
List<News> allNews = this.newsService.getNews();
Assert.assertEquals(newsSize, allNews.size());
}
}
Might this be problem of the Spring's database connection behavior to close a connection after committing/rolling back a transaction? If yes, how can I solve this? My last try was to create transaction on class/method base but without success.
Thank you in advance.
I believe its connected to your DirtiesContextTestExecutionListener.class which is some kind of equivalent of
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html .
Test annotation which indicates that the ApplicationContext associated with a test is dirty and should be closed:
...
after each test method in the current test class, when declared at the
class level with class mode set to AFTER_EACH_TEST_METHOD
so probably spring is closing your beans after 1st test method. Maybe its easier to just clean up database in #AfterMethod method? It would run after each test.
Thanks to freakman, we've got a solution that seems to work for us:
We created our own classes
OurOwnDbUnitTestExecutionListener.java
OurOwnDbUnitRunner.java
based on Spring-test-DbUnit's
com.github.springtestdbunit.DbUnitTestExecutionListener.java
com.github.springtestdbunit.DbUnitRunner.java
classes. Note that the package name for our classes must be equal due to the use of package-scoped classes. Since the listener uses the runner in a non-configurable manner, we use our own DbUnit runner:
private static OurOwnDbUnitRunner runner = new OurOwnDbUnitRunner();
The runner is modified so it does not close the connections after completing a test method. Line 116 is removed:
// testContext.getConnections().closeAll();
Finally, we use our modified DbUnitTestExecutionListener in our test class:
#TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
OurOwnDbUnitTestExecutionListener.class, // <<< this does the trick
ForeignKeyDisabler.class })
If you're using spring-test-dbunit, another solution might be to provide a dbUnitDatabaseconnection that overwrites the close-method:
#Bean
public IDatabaseConnection dbUnitDatabaseConnection(DataSource dataSource) throws SQLException, DatabaseUnitException {
return new CustomDatabaseConnection(dataSource.getConnection());
}
private static class CustomDatabaseConnection extends DatabaseConnection {
public CustomDatabaseConnection(Connection connection) throws DatabaseUnitException {
super(connection);
}
#Override
public void close() {
// don't close the connection
}
}

SpringMVC- Mockito- Junit - NullPointerException when service class method is called from controller class

I am writing a web-application, by using spring MVC . I have made a service class to map things to and from the database using UI.
My problem is I am new to junit and mockito
when i am trying to write unit tests on Controller class, it always returns a NullPointerException. It is really annoying since I am unable to find the answer, so I hope you guys can help me out. Below I have posted my code.
My Controller class method on /update return success
#RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateFormData(#ModelAttribute CRDetails certGuide,
ModelMap model) {
crService.updateCertification(certGuide);
return "success";
}
My Service class method updates mongodb with modified data
public void updateCertification(CRDetails certGuide) { String userTypeSubmitted = certGuide.getCertGuidelines().getUserType(); System.out.println("updated by " + userTypeSubmitted); Update update = new Update(); Query query = new Query(Criteria.where("cr").is("CR1"));
CRDetails details = certMongoTemplate.findOne(query, CRDetails.class,
COLLECTION_NAME); CertificateGuidelines newCertGuide = new CertificateGuidelines();
if (details != null) { newCertGuide.setCertG(certGuide.getCertGuidelines().getCertG()); if (userTypeSubmitted.equals("Certification Team")
&& userTypeSubmitted != "") {
newCertGuide.setStatus("Pending with Document Developer"); } else {
newCertGuide.setStatus("Pending with Certification Team"); } newCertGuide.setHistoryCount(++count);
// converting it into mongo document. MongoConverter converter = certMongoTemplate.getConverter(); DBObject newRec = (DBObject) converter
.convertToMongoType(newCertGuide); update.set("certGuidelines", newRec); }
System.out.println("new cert guide " + newCertGuide.getCertG() + " "
+ newCertGuide.getStatus() + " "
+ newCertGuide.getHistoryCount() + " "
+ newCertGuide.getUserType());
certMongoTemplate.updateFirst(query, update, CRDetails.class); }
My controllerTest class which return NullPointerException.
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.web.server.MockMvc;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.View;
import com.cerner.docworks.controller.CRController;
import com.cerner.docworks.domain.CRDetails;
import com.cerner.docworks.service.CRService;
public class CRControllerTest {
/*#Mock private CRService service;*/
#Mock View mockView;
#InjectMocks
private CRController crController;
private MockMvc mockMvc;
#Before public void setup() {
}
#Test public void testUpdate() throws Exception { // CRService service = Mockito.mock(CRService.class); CRService crService = Mockito.spy(new CRService()); ModelMap model = Mockito.mock(ModelMap.class); CRDetails certGuide = Mockito.spy(new CRDetails());
CRController controller = new CRController();
// Mockito.when(service.updateCertification(certGuide)); // Mockito.when(model.addAttribute(Matchers.eq("certGuide"),any(String.class))).thenReturn(null); // Mockito.doThrow(new RuntimeException()).when(crService).updateCertification(certGuide); Mockito.doNothing().when(crService).updateCertification(certGuide); Mockito.mockingDetails(certGuide); // Mockito.doCallRealMethod().when(crService).updateCertification(certGuide); // Mockito.(crService)).updateCertification(certGuide);; String fileName = controller.updateFormData(certGuide, model);
System.out.println(fileName); // Mockito.verify(controller).updateFormData(certGuide, model); assertEquals("success", fileName); } }
Trace:
java.lang.NullPointerException at
com.cerner.docworks.controller.CRController.updateFormData(CRController.java:81)
at
com.cerner.docworks.controller.test.CRControllerTest.testUpdate(CRControllerTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483) at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at
org.junit.runners.ParentRunner.run(ParentRunner.java:363) at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
FYI,
CRDetails is a pojo class with
private String id;
private String cr;
private String desc;
private CertificateGuidelines certGuidelines;
private Date dueDate;
//private String userType;
private String solutionName;
FYI,
When i am debugging the code, i found mock is setting objects to null
If i do set details manually, test case pass but, i want to use mockito.
Thanks in advance for ur help.
You need to run the test with the MockitoJUnitRunner, e.g.:
#RunWith(MockitoJUnitRunner.class)
public class CRControllerTest {
// ...
This allows #InjectMocks and #Mock to work properly. I.e. mockView will be created as a mock of View and crController will be created with relevant #Mock-annotated fields.
You can refer to the documentation to see how the mocks are injected.

Spring4 + and Hibernate 4 Transactions

Using spring 4.0.6.RELEASE, Hibernate 4.3.6.Final and hsqldb 2.3.2. My integration test looks like the following;
#Test(expected = DataIntegrityViolationException.class)
public final void testDuplicateItems() {
final ServerEntity serverEntity1 = new ServerEntity("DuplicateItem");
opService.save(serverEntity1);
opService.save(serverEntity1);
}
This works as expected. However, when I run my standalone java component i can save the first item, the second item which is a duplicate is not saved but Im unable to catch the exception. Here is the log file
WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper: SQL Error: -104, SQLState: 23505
2014-08-27 14:52:06,843 ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper: integrity constraint violation: unique constraint or index violation; UK_NFU7LXMMDFVIR1WD08662085N table: SERVERENTITY
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [UK_NFU7LXMMDFVIR1WD08662085N]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:161)
at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:681)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:563)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy38.execute(Unknown Source)
at com.opserver.simpleapp.MainApp.start(MainApp.java:60)
at com.opserver.simpleapp.MainApp.main(MainApp.java:37)
... 6 more
Both the service and dao implementations have #Transactional at class level. I've a component class that is calling the service class, this component class is not transactional! The component class prints a response, does the session need to be flushed here?
Need to figure out why the save method in the dao is not throwing the exception, I can actually see it an id being created and then rolled back.
J
My component class is very basic;
boolean isValid = opServerService.loadXMLFile("Server.xml");
try{
if (isValid) {
System.out.println("Entity has been added");
} else {
System.out.println("Entity has not been added");
}
}catch (Exception ex){
System.out.println("that was a focked up");
}
The problem is that "Entity has been added" gets printed to console and then I see the above error in console.
DAO looks like this
#Override
#Transactional
public final void save(final ServerEntity serverEntity) throws DataIntegrityViolationException {
LOGGER.debug(">>start(serverEntity=" + serverEntity + ")");
Preconditions.checkNotNull(serverEntity);
this.getCurrentSession().save(serverEntity);
}
Service method with #Transactional at class level, looks like this
#Override
public final void save(ServerEntity serverEntity) {
opServerDao.save(serverEntity);
}
And Component looks like this
#Component
public class AddCommand implements Command {
#Autowired
OpService opService;
public AddServerCommand() {
super();
}
#Override
public void execute(String[] options) {
try{
boolean isValid = opService.save("Server.xml");
if (isValid) {
System.out.println("Entity has been added");
} else {
System.out.println("Entity has not been added");
}
}catch (Exception ex){
System.out.println("Exception found");
}
}
}
You should catch your exception in the component that's calling the service.
Found the solution, was missing from the applicationContext.xml. The wrapped try/catch around the opService now catches the exception. Need to implement my own custom exception handler but for now at least I know the component class is handling the exception which is being thrown from the service class.
Thanks for your help.
J

Resources