Source code for 'No Hibernate Session bound to thread'
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package org.baeldung.boot;
|
||||
|
||||
import org.baeldung.session.exception.Application;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@TestPropertySource("classpath:exception.properties")
|
||||
public class ApplicationTests {
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,12 @@ package org.baeldung.boot;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = DemoApplication.class)
|
||||
@SpringBootTest(classes = DemoApplication.class)
|
||||
@WebAppConfiguration
|
||||
public class DemoApplicationTests {
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.baeldung.boot.repository;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.baeldung.boot.ApplicationTests;
|
||||
import org.baeldung.boot.model.Foo;
|
||||
import org.baeldung.session.exception.repository.FooRepository;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional
|
||||
@TestPropertySource("classpath:exception-hibernate.properties")
|
||||
public class HibernateSessionTest extends ApplicationTests {
|
||||
@Autowired
|
||||
private FooRepository fooRepository;
|
||||
|
||||
@Test
|
||||
public void whenSavingWithCurrentSession_thenThrowNoException() {
|
||||
Foo foo = new Foo("Exception Solved");
|
||||
fooRepository.save(foo);
|
||||
foo = null;
|
||||
foo = fooRepository.get(1);
|
||||
|
||||
assertThat(foo, notNullValue());
|
||||
assertThat(foo.getId(), is(1));
|
||||
assertThat(foo.getName(), is("Exception Solved"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.baeldung.boot.repository;
|
||||
|
||||
import org.baeldung.boot.ApplicationTests;
|
||||
import org.baeldung.boot.model.Foo;
|
||||
import org.baeldung.session.exception.repository.FooRepository;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional
|
||||
public class NoHibernateSessionTest extends ApplicationTests {
|
||||
@Autowired
|
||||
private FooRepository fooRepository;
|
||||
|
||||
@Test(expected = HibernateException.class)
|
||||
public void whenSavingWithoutCurrentSession_thenThrowException() {
|
||||
Foo foo = new Foo("Exception Thrown");
|
||||
fooRepository.save(foo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.profiles.active=exception
|
||||
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
|
||||
@@ -0,0 +1,6 @@
|
||||
# Security
|
||||
security.user.name=admin
|
||||
security.user.password=password
|
||||
|
||||
spring.dao.exceptiontranslation.enabled=false
|
||||
spring.profiles.active=exception
|
||||
Reference in New Issue
Block a user