Skip to content

Commit 7d30017

Browse files
committed
StubWebApplicationContext supports AutowireCapableBeanFactory operations (as far as possible)
This is generally worthwhile but in particular fixes a regression with our Jackson SpringHandlerInstantiator in standalone MVC tests. Issue: SPR-13375
1 parent 3430f76 commit 7d30017

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Set;
2525
import javax.servlet.ServletContext;
2626

27+
import org.springframework.beans.BeanUtils;
2728
import org.springframework.beans.BeansException;
2829
import org.springframework.beans.TypeConverter;
2930
import org.springframework.beans.factory.BeanFactory;
@@ -43,20 +44,22 @@
4344
import org.springframework.core.env.StandardEnvironment;
4445
import org.springframework.core.io.Resource;
4546
import org.springframework.core.io.support.ResourcePatternResolver;
47+
import org.springframework.util.ClassUtils;
4648
import org.springframework.util.ObjectUtils;
4749
import org.springframework.web.context.WebApplicationContext;
4850
import org.springframework.web.context.support.ServletContextResourcePatternResolver;
4951

5052
/**
51-
* A mock WebApplicationContext that accepts registrations of object instances.
53+
* A stub WebApplicationContext that accepts registrations of object instances.
5254
*
53-
* <p>As registered object instances are instantiated and initialized
54-
* externally, there is no wiring, bean initialization, lifecycle events, as
55-
* well as no pre-processing and post-processing hooks typically associated with
56-
* beans managed by an {@link ApplicationContext}. Just a simple lookup into a
55+
* <p>As registered object instances are instantiated and initialized externally,
56+
* there is no wiring, bean initialization, lifecycle events, as well as no
57+
* pre-processing and post-processing hooks typically associated with beans
58+
* managed by an {@link ApplicationContext}. Just a simple lookup into a
5759
* {@link StaticListableBeanFactory}.
5860
*
5961
* @author Rossen Stoyanchev
62+
* @author Juergen Hoeller
6063
* @since 3.2
6164
*/
6265
class StubWebApplicationContext implements WebApplicationContext {
@@ -78,9 +81,6 @@ class StubWebApplicationContext implements WebApplicationContext {
7881
private final ResourcePatternResolver resourcePatternResolver;
7982

8083

81-
/**
82-
* Class constructor.
83-
*/
8484
public StubWebApplicationContext(ServletContext servletContext) {
8585
this.servletContext = servletContext;
8686
this.resourcePatternResolver = new ServletContextResourcePatternResolver(servletContext);
@@ -322,7 +322,7 @@ public String getMessage(MessageSourceResolvable resolvable, Locale locale) thro
322322

323323
@Override
324324
public ClassLoader getClassLoader() {
325-
return null;
325+
return ClassUtils.getDefaultClassLoader();
326326
}
327327

328328
@Override
@@ -366,65 +366,61 @@ public Object initializeBean(Object existingBean, String beanName) throws BeansE
366366

367367
@Override
368368
public <T> T createBean(Class<T> beanClass) {
369-
throw new UnsupportedOperationException();
369+
return BeanUtils.instantiate(beanClass);
370370
}
371371

372372
@Override
373373
@SuppressWarnings("rawtypes")
374-
public Object createBean(Class beanClass, int autowireMode, boolean dependencyCheck) {
375-
throw new UnsupportedOperationException();
374+
public Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) {
375+
return BeanUtils.instantiate(beanClass);
376376
}
377377

378378
@Override
379379
@SuppressWarnings("rawtypes")
380-
public Object autowire(Class beanClass, int autowireMode, boolean dependencyCheck) {
381-
throw new UnsupportedOperationException();
380+
public Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) {
381+
return BeanUtils.instantiate(beanClass);
382382
}
383383

384384
@Override
385385
public void autowireBean(Object existingBean) throws BeansException {
386-
throw new UnsupportedOperationException();
387386
}
388387

389388
@Override
390389
public void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) {
391-
throw new UnsupportedOperationException();
392390
}
393391

394392
@Override
395393
public Object configureBean(Object existingBean, String beanName) {
396-
throw new UnsupportedOperationException();
394+
return existingBean;
397395
}
398396

399397
@Override
400398
public Object resolveDependency(DependencyDescriptor descriptor, String beanName) {
401-
throw new UnsupportedOperationException();
399+
throw new UnsupportedOperationException("Dependency resolution not supported");
402400
}
403401

404402
@Override
405403
public Object resolveDependency(DependencyDescriptor descriptor, String beanName,
406404
Set<String> autowiredBeanNames, TypeConverter typeConverter) {
407-
throw new UnsupportedOperationException();
405+
throw new UnsupportedOperationException("Dependency resolution not supported");
408406
}
409407

410408
@Override
411409
public void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException {
412-
throw new UnsupportedOperationException();
413410
}
414411

415412
@Override
416413
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) {
417-
throw new UnsupportedOperationException();
414+
return existingBean;
418415
}
419416

420417
@Override
421418
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) {
422-
throw new UnsupportedOperationException();
419+
return existingBean;
423420
}
424421

425422
@Override
426423
public void destroyBean(Object existingBean) {
427-
throw new UnsupportedOperationException();
428424
}
429425
}
430426

spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,8 +23,11 @@
2323
import javax.servlet.http.HttpServletRequest;
2424
import javax.servlet.http.HttpServletResponse;
2525

26+
import com.fasterxml.jackson.databind.JsonSerializer;
27+
import com.fasterxml.jackson.databind.ser.impl.UnknownSerializer;
2628
import org.junit.Test;
2729

30+
import org.springframework.http.converter.json.SpringHandlerInstantiator;
2831
import org.springframework.mock.web.test.MockHttpServletRequest;
2932
import org.springframework.stereotype.Controller;
3033
import org.springframework.web.bind.annotation.RequestMapping;
@@ -46,7 +49,7 @@
4649
*/
4750
public class StandaloneMockMvcBuilderTests {
4851

49-
@Test // SPR-10825
52+
@Test // SPR-10825
5053
public void placeHoldersInRequestMapping() throws Exception {
5154

5255
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
@@ -62,7 +65,7 @@ public void placeHoldersInRequestMapping() throws Exception {
6265
assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName());
6366
}
6467

65-
@Test // SPR-12553
68+
@Test // SPR-12553
6669
public void applicationContextAttribute() {
6770
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController());
6871
builder.addPlaceHolderValue("sys.login.ajax", "/foo");
@@ -96,6 +99,15 @@ public void addFilterPatternContainsNull() {
9699
builder.addFilter(new ContinueFilter(), (String) null);
97100
}
98101

102+
@Test // SPR-13375
103+
public void springHandlerInstantiator() {
104+
TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PersonController());
105+
builder.build();
106+
SpringHandlerInstantiator instantiator = new SpringHandlerInstantiator(builder.wac.getAutowireCapableBeanFactory());
107+
JsonSerializer serializer = instantiator.serializerInstance(null, null, UnknownSerializer.class);
108+
assertNotNull(serializer);
109+
}
110+
99111

100112
@Controller
101113
private static class PlaceholderController {

0 commit comments

Comments
 (0)