spring-test

How to load DBUnit test data once per case with Spring Test

China☆狼群 提交于 2019-11-26 21:36:42
问题 Spring Test helpfully rolls back any changes made to the database within a test method. This means that it is not necessary to take the time to delete/reload the test data before each test method. But if you use the @BeforeClass Junit annotation, then that forces the data loader to be static. A question that is explored here: Why must jUnit's fixtureSetup be static? If the data initialization method is static, so must the data connection methods and the data source..and on and on...forcing

How to execute @Sql before a @Before method

喜你入骨 提交于 2019-11-26 19:53:57
问题 I'm trying to combine the follow annotations: org.springframework.test.context.jdbc.Sql and org.junit.Before Like the follow code: @Test @Sql(scripts = "dml-parametro.sql") public void testData(){ Iterable<Parametro> parametros = parametroService.findAll(); List<Parametro> parametrosList = Lists.newArrayList(parametros); Assert.assertThat(parametrosList.size(), Is.is(1)); } @Before public void beforeMethod() { JdbcTestUtils.deleteFromTables(jdbcTemplate, "PARAMETRO"); } The code in the method

NoSuchFieldError when trying to run a jUnit test with Spring

亡梦爱人 提交于 2019-11-26 17:00:35
问题 So far I have two tests. One uses only jUnit framework and works fine. The other one uses spring-test library and creates this exception every time I try to run it. Any ideas what may cause the problem? Error java.lang.NoSuchFieldError: NULL at org.junit.runners.ParentRunner.<init>(ParentRunner.java:48) at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:59) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:104)

Reuse spring application context across junit test classes

霸气de小男生 提交于 2019-11-26 15:51:50
We've a bunch of JUnit test cases (Integration tests) and they are logically grouped into different test classes. We are able to load Spring application context once per test class and re-use it for all test cases in a JUnit test class as mentioned in http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html However, we were just wondering if there is a way to load Spring application context only once for a bunch of JUnit test classes. FWIW, we use Spring 3.0.5, JUnit 4.5 and use Maven to build the project. Yes, this is perfectly possible. All you have to

java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

你离开我真会死。 提交于 2019-11-26 13:05:02
问题 I have the following test class: @ActiveProfiles({ \"DataTC\", \"test\" }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class, PropertyPlaceholderConfiguration.class }) public class RegularDayToTimeSlotsTest { ... The issue seems to come from the BaseTestConfiguration class: @Configuration @ComponentScan(basePackages = { \"com.bignibou\" }, excludeFilters = { @Filter(type = FilterType.CUSTOM

How to unit test a Spring MVC controller using @PathVariable?

心不动则不痛 提交于 2019-11-26 12:49:52
问题 I have a simple annotated controller similar to this one: @Controller public class MyController { @RequestMapping(\"/{id}.html\") public String doSomething(@PathVariable String id, Model model) { // do something return \"view\"; } } and I want to test it with an unit test like this: public class MyControllerTest { @Test public void test() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI(\"/test.html\"); new AnnotationMethodHandlerAdapter() .handle(request

JUnit tests pass in Eclipse but fail in Maven Surefire

只愿长相守 提交于 2019-11-26 12:35:43
问题 I have written some JUnit tests using JUnit 4 and spring-test libraries. When I run the tests inside Eclipse then run fine and pass. But when I run them using Maven (during the build process), they fail giving a spring related error. I am not sure what is causing the problem, JUnit, Surefire or Spring. Here is my test code, spring configuration and the exception that I get from Maven: PersonServiceTest.java package com.xyz.person.test; import static com.xyz.person.util.FjUtil.toFjList; import

Disable security for unit tests with spring boot

孤者浪人 提交于 2019-11-26 09:02:58
问题 I\'m trying to create a simple spring boot web project with security. I can launch the application fine and the security is working fine. However, I have some components that I want to test without security (or test at all -- I cant get the test working at all). I get an exception indicating that it can\'t find an ObjectPostProcessor and thus can\'t bring up the container. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org