spring-test

spring Should I use @DirtiesContext on every class

拥有回忆 提交于 2019-12-20 09:05:15
问题 I have several junit tests, @ContextConfiguration(locations = { "file:../business/src/test/resources/application-context-test.xml", "file:src/main/webapp/WEB-INF/confA.xml", "classpath:/mvc-dispatcher-servlet-test.xml"}) @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class ProductContentControllerTest { ... } Inside a class all tests have to run in the same context (which is the case). But I want all my tests classes to be independent. I was assuming that it was the

Test drive Hystrix Circuit Breaker configuration

允我心安 提交于 2019-12-19 11:39:09
问题 Our application is written in anit-fragile manner by implementing circuit breaker pattern using Hystrix. The whole of the application is created using test driven practice but is stuck at the point where we need to implement the circuit breaker strategy by configuring the same on the methods. Below is the sample configuration used by us - @HystrixCommand(commandProperties = { @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), @HystrixProperty(name = "circuitBreaker

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

谁说胖子不能爱 提交于 2019-12-19 10:09:14
问题 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

No transactional EntityManager available

和自甴很熟 提交于 2019-12-19 09:17:13
问题 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

@TestPropertySource is not loading properties

冷暖自知 提交于 2019-12-19 09:06:35
问题 I'm writing integration test for my spring boot application but when I try to override some properties using @TestPropertySource, it's loading the property file defined in the context xml but it's not overriding the properties defined in the annotation. @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {DefaultApp.class, MessageITCase.Config.class}) @WebAppConfiguration @TestPropertySource(properties = {"spring.profiles.active=hornetq", "test.url=http://www

@TestPropertySource is not loading properties

好久不见. 提交于 2019-12-19 09:06:08
问题 I'm writing integration test for my spring boot application but when I try to override some properties using @TestPropertySource, it's loading the property file defined in the context xml but it's not overriding the properties defined in the annotation. @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {DefaultApp.class, MessageITCase.Config.class}) @WebAppConfiguration @TestPropertySource(properties = {"spring.profiles.active=hornetq", "test.url=http://www

Testing with spring-test-mvc jsonpath returns null

六眼飞鱼酱① 提交于 2019-12-19 04:04:19
问题 I am using Spring's "spring-test-mvc" library to test web controllers. I have a very simple controller that returns a JSON array. Then in my test I have: @Test public void shouldGetAllUsersAsJson() throws Exception { mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON)) .andExpect(content().mimeType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("fName").exists()); } The above test returns: java.lang.AssertionError: No value for JSON path: fName To quickly check what I

How to run tests using a custom WebApplicationInitializer?

爷,独闯天下 提交于 2019-12-18 15:35:14
问题 class MyWebAppInitializer extends WebApplicationInitializer { def onStartup(servletContext: ServletContext): Unit = { ... } } @RunWith(classOf[SpringJUnit4ClassRunner]) @WebAppConfiguration @ContextConfiguration(classes = Array(classOf[MyConfig]), initializers=Array(classOf[MyWebAppInitializer])) // <<< ERROR class MyTest { ... } Complains about : annotation argument needs to be a constant; found: classOf[MyWebAppInitializer] UPDATE: @M. Deinum points out that only

Spring Test returning 401 for unsecured URLs

倾然丶 夕夏残阳落幕 提交于 2019-12-18 07:33:35
问题 I am using Spring for MVC tests Here is my test class @RunWith(SpringRunner.class) @WebMvcTest public class ITIndexController { @Autowired WebApplicationContext context; MockMvc mockMvc; @MockBean UserRegistrationApplicationService userRegistrationApplicationService; @Before public void setUp() { this.mockMvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity()) .build(); } @Test public void should_render_index() throws Exception { mockMvc.perform(get("/")) .andExpect(status

Spring MockMVC - How to mock custom validators running outside of controllers

北城以北 提交于 2019-12-18 07:12:49
问题 @UsernameAlreadyExists private String username; I have a custom validator that I created to ensure that duplicate usernames are caught by the application when account creation form submits. When I unit test the account creation controller using MockMVC, it fails as the validator depends on a service, so I get null pointer exception. How can I mock the validator or the service this validator depends on? I could not figure out how to make this work as the controller does not depend on the