Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the class of a field of type T?
    primarykey
    data
    text
    <p>I write JUnit tests for some Spring MVC Controllers. The initialization of the JUnit test is common for all my Controllers tests, so I wanted to create an abstract class that does this initialization.</p> <p>Thus, I created the following code:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:spring/applicationContext-test.xml", "classpath*:spring/spring-mvc-test.xml" }) @Transactional public abstract class AbstractSpringMVCControllerTest&lt;T&gt; { @Autowired protected ApplicationContext applicationContext; protected MockHttpServletRequest request; protected MockHttpServletResponse response; protected HandlerAdapter handlerAdapter; protected T controller; @SuppressWarnings("unchecked") @Before public void initContext() throws SecurityException, NoSuchFieldException { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); handlerAdapter = applicationContext.getBean(AnnotationMethodHandlerAdapter.class); // Does not work, the problem is here... controller = applicationContext.getBean(T); } } </code></pre> <p>The idea is to create, for each controller I want to test a JUnit test class that extends my <code>AbstractSpringMVCControllerTest</code>. The type given in the <code>extends</code> declaration is the class of the <strong>Controller</strong>.</p> <p>For example, if I want to test my <code>AccountController</code>, I will create the <code>AccountControllerTest</code> class like that:</p> <pre><code>public class AccountControllerTest extends AbstractSpringMVCControllerTest&lt;AccountController&gt; { @Test public void list_accounts() throws Exception { request.setRequestURI("/account/list.html"); ModelAndView mav = handlerAdapter.handle(request, response, controller); ... } } </code></pre> <p>My problem is located in the last line of the <code>initContext()</code> method of the abstract class. This abstract class declares the <code>controller</code> object as a <code>T</code> object, but how can say to the Spring Application Context to return the bean of type <code>T</code>?</p> <p>I've tried something like that:</p> <pre><code> Class&lt;?&gt; controllerClass = this.getClass().getSuperclass().getDeclaredField("controller").getType(); controller = (T) applicationContext.getBean(controllerClass); </code></pre> <p>but <code>controllerClass</code> returns the <code>java.lang.Object.class</code> class, not <code>AccountController.class</code>.</p> <p>Of course, I can create a <code>public abstract Class&lt;?&gt; getControllerClass();</code> method, which will be overriden by each JUnit Controller test class, but I prefer to avoid this solution.</p> <p>So, any idea?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload