spring-test

Spring MockMvcBuilders Security filter

情到浓时终转凉″ 提交于 2020-01-14 10:26:27
问题 I have manage to create REST API with spring mvc. My purpose was about to protect a resource with JWToken. Now i am trying to write three Test : 1. Get the Token with granted user/password Authentication Fail => test:OK 2. Get the Token with not granted user/password Authentication success => test:OK 3. Get the protected resource without providing the Token => test:fail because my rest service give the protected resource .... Here is my REST controller: @Component @RestController

Override @PropertySource with @TestPropertySource in Spring Boot

老子叫甜甜 提交于 2020-01-14 10:12:52
问题 I have my Spring Boot main class: @SpringBootApplication @PropertySource("file:/my/file/properties") public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } //main method } I'm reading properties from an external file (using @PropertySource ). Now, I have an integration test: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration

Using Spring mockMvc to test optional path variables

谁说我不能喝 提交于 2020-01-13 08:08:37
问题 I have a method in Spring MVC with optional path variable. I am trying to test it for a scenario when optional path variable is not provided. Snippet from Controller, resource URI to invoke- @RequestMapping(value = "/some/uri/{foo}/{bar}", method = RequestMethod.PUT) public <T> ResponseEntity<T> someMethod(@PathVariable("foo") String foo, @PathVariable(value = "bar", required = false) String bar) { LOGGER.info("foo: {}, bar: {}", foo, bar); } Snippet from my test using MockMvc- //inject

Using Spring mockMvc to test optional path variables

吃可爱长大的小学妹 提交于 2020-01-13 08:08:10
问题 I have a method in Spring MVC with optional path variable. I am trying to test it for a scenario when optional path variable is not provided. Snippet from Controller, resource URI to invoke- @RequestMapping(value = "/some/uri/{foo}/{bar}", method = RequestMethod.PUT) public <T> ResponseEntity<T> someMethod(@PathVariable("foo") String foo, @PathVariable(value = "bar", required = false) String bar) { LOGGER.info("foo: {}, bar: {}", foo, bar); } Snippet from my test using MockMvc- //inject

Testing @TransactionalEvents and @Rollback

孤街醉人 提交于 2020-01-11 06:34:08
问题 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

How to populate database only once before @Test methods in spring test?

半世苍凉 提交于 2020-01-09 12:51:08
问题 My next problem testing spring service layer with junit4 is: How to call script that populates database only once before all @Test methods: I want to execute this once before all @Tests: JdbcTestUtils.executeSqlScript(jdbcTemplate(), new FileSystemResource( "src/main/resources/sql/mysql/javahelp-insert.sql"), false); I tried to use @PostConstruct on my GenericServiceTest class(extended by test classes). It turned out that @PostConstruct is called every time before every @Test method.

Spring Test with JUnit 4 and Spring Data JPA: NoSuchMethodError org.hibernate.engine.spi.SessionFactoryImplementor.getProperties

你说的曾经没有我的故事 提交于 2020-01-05 04:52:21
问题 With SpringJUnit4ClassRunner , JUnit 4 and Spring Test I wrote unit test for Service which uses Spring Data JPA Repository and embedded HSQL database: @Ignore @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath*:unitTestFullConfig.xml") public class InMemoryDBFullTestBaseClass { } public final class ActorServiceImplTest extends InMemoryDBFullTestBaseClass { @Inject private ActorService service; @Test public final void saveActor () throws Exception { service.save(new

Spring Test with JUnit 4 and Spring Data JPA: NoSuchMethodError org.hibernate.engine.spi.SessionFactoryImplementor.getProperties

狂风中的少年 提交于 2020-01-05 04:51:29
问题 With SpringJUnit4ClassRunner , JUnit 4 and Spring Test I wrote unit test for Service which uses Spring Data JPA Repository and embedded HSQL database: @Ignore @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath*:unitTestFullConfig.xml") public class InMemoryDBFullTestBaseClass { } public final class ActorServiceImplTest extends InMemoryDBFullTestBaseClass { @Inject private ActorService service; @Test public final void saveActor () throws Exception { service.save(new

Create instance of Spring´s ParameterizedTypeReference in Kotlin

这一生的挚爱 提交于 2020-01-04 23:05:47
问题 I am trying to learn Kotlin, and test how it works with spring boot. My application is using a mongo database to store data and I have a Jersey resource for retrieving data. I am testing it using spring-boot-test and RestTestTemplate . The RestTestTemplate has an exchange method which takes a ParameterizedTypeReference . This class has a protected constructor. So the only way I managed to use it from Kotlin was like this: class ListOfPeople : ParameterizedTypeReference<List<Person>>() Here is

How to set 'headless' property in a Spring Boot test?

▼魔方 西西 提交于 2020-01-02 06:56:10
问题 I am testing using Spring Boot with JavaFX (Based on some excellent YouTube videos that explain this). To make it work with TestFX, I need to create the context like this: @Override public void init() throws Exception { SpringApplicationBuilder builder = new SpringApplicationBuilder(MyJavaFXApplication.class); builder.headless(false); // Needed for TestFX context = builder.run(getParameters().getRaw().stream().toArray(String[]::new)); FXMLLoader loader = new FXMLLoader(getClass().getResource(