spring-test

Spring Boot. @DataJpaTest H2 embedded database create schema

和自甴很熟 提交于 2019-12-01 15:09:48
I have couple of entities in my data layer stored in particular schema. For example: @Entity @Table(name = "FOO", schema = "DUMMY") public class Foo {} I'm trying to setup H2 embedded database for integration testing of my data layer. I'm using @DataJpaTest annotation for my tests to get H2 embedded database configured automatically. However, the creation of tables fails because schema DUMMY is not created at DB initialization. Any ideas on how to create schema before creation of tables in test cases? I've tried to use @Sql(statements="CREATE SCHEMA IF NOT EXISTS DUMMY") but didn't succeed.

What is the best way to test that a spring application context fails to start?

对着背影说爱祢 提交于 2019-12-01 11:37:00
I use the spring-boot-starter-web and spring-boot-starter-test. Let's say I have a class for binding configuration properties: @ConfigurationProperties(prefix = "dummy") public class DummyProperties { @URL private String url; // getter, setter ... } Now I want to test that my bean validation is correct. The context should fail to start (with a specfic error message) if the property dummy.value is not set or if it contains an invalid URL. The context should start if the property contains a valid URL. (The test would show that @NotNull is missing.) A test class would look like this: @RunWith

Testing @TransactionalEvents and @Rollback

房东的猫 提交于 2019-12-01 08:57:55
I've been trying to test out @TransactionalEvents (a feature of Spring 4.2 https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2 ) with our existing Spring JUnit Tests (run via either @TransactionalTestExecutionListener or subclassing AbstractTransactionalUnit4SpringContextTests but, it seems like there's a forced choice -- either run the test without a @Rollback annotation, or the events don't fire. Has anyone come across a good way to test @TransactionalEvents while being able to @Rollback tests? Stéphane Nicoll is correct: if the TransactionPhase for your

Testing a custom RepositoryRestController that uses a PersistentEntityResourceAssembler

◇◆丶佛笑我妖孽 提交于 2019-12-01 06:53:22
问题 I have a RepositoryRestController that exposes resources for some persistent entities. I have a method on my controller that takes a PersistentEntityResourceAssembler to help me generate the resources automatically. @RepositoryRestController @ExposesResourceFor(Customer.class) @RequestMapping("/api/customers") public class CustomerController { @Autowired private CustomerService service; @RequestMapping(method = GET, value="current") public ResponseEntity getCurrent(Principal principal Long id

Issue with @WithUserDetails and spring boot 1.4 TestEntityManager

送分小仙女□ 提交于 2019-12-01 06:20:07
I have an issue with Spring Boot's TestEntityManager and @WithUserDetails annotation. Here is my test suite: public class AdvertisementAuthorizationTest extends AbstractIntegrationTest { private static final String IMPERSONATOR_EMAIL = "joe.hacker@gmail.com"; private final static String OWNER_OF_ADVERTISEMENT_EMAIL = "john.nice@gmail.com"; @Autowired private TestEntityManager testEntityManager; @Autowired private MockMvc mockMvc; private Advertisement advertisement; private UserAccount impersonator; private ObjectMapper mapper = new ObjectMapper(); @Before public void setUp() { advertisement =

No transactional EntityManager available

人走茶凉 提交于 2019-12-01 06:14:31
I am new to the jpa and spring world and I am currently doing some unit test on a simple method but keep getting this error message only when I run my test class in unit test mode: java.lang.IllegalStateException: No transactional EntityManager available at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223) at $Proxy19.unwrap(Unknown Source) at com.gemstone.integration.PersonDao.getPersonByUserNamePassword(PersonDao.java:59) at com.gemstone.integration.PersonDaoTest.getPersonByUserNamePassword_Exist

Spring-Boot module based integration testing

和自甴很熟 提交于 2019-12-01 05:16:21
I have a multi-module Spring-Boot project. I was wondering how I can set up integration testing just to test Spring Data JPA repositories? The following approach fails with this exception: HV000183: Unable to load 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath. Since this module does not depend on the web module, there is no web application that can be started. @RunWith(SpringJUnit4ClassRunner.class) @IntegrationTest @SpringApplicationConfiguration(classes = TestConfiguration.class) class CardInfoRepositoryIT { @Autowired CardInfoRepository

Issue with @WithUserDetails and spring boot 1.4 TestEntityManager

二次信任 提交于 2019-12-01 05:03:09
问题 I have an issue with Spring Boot's TestEntityManager and @WithUserDetails annotation. Here is my test suite: public class AdvertisementAuthorizationTest extends AbstractIntegrationTest { private static final String IMPERSONATOR_EMAIL = "joe.hacker@gmail.com"; private final static String OWNER_OF_ADVERTISEMENT_EMAIL = "john.nice@gmail.com"; @Autowired private TestEntityManager testEntityManager; @Autowired private MockMvc mockMvc; private Advertisement advertisement; private UserAccount

@IfProfileValue vs @ActiveProfiles in the context of Spring test

最后都变了- 提交于 2019-12-01 04:44:26
I am in reference to Spring test and the following annotations: @IfProfileValue @ActiveProfiles I currently use @ActiveProfiles in my application and I recently discovered the existence of the @IfProfileValue annotation which seems to provide similar functionnality. Can someone please explain what the differences are between those two annotations perhaps by providing usage examples that would contrast the two? As stated in the Javadoc, @IfProfileValue is used to indicate that a test is enabled for a specific testing profile or environment. Whereas, @ActiveProfiles is used to declare which

Inject @AuthenticationPrincipal when unit testing a Spring REST controller

落花浮王杯 提交于 2019-12-01 03:04:18
I am having trouble trying to test a rest endpoint that receives an UserDetails as a parameter annotated with @AuthenticationPrincipal . Seems like the user instance created in the test scenario is not being used, but an attempt to instantiate using default constructor is made instead: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.andrucz.app.AppUserDetails]: No default constructor found; REST endpoint: @RestController @RequestMapping("/api/items") class ItemEndpoint { @Autowired private ItemService itemService; @RequestMapping(path = "/{id}", method =