request scoped beans in spring testing

Solution for Spring 3.2 or newer Spring starting with version 3.2 provides support for session/request scoped beans for integration testing. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfig.class) @WebAppConfiguration public class SampleTest { @Autowired WebApplicationContext wac; @Autowired MockHttpServletRequest request; @Autowired MockHttpSession session; @Autowired MySessionBean mySessionBean; @Autowired MyRequestBean myRequestBean; @Test public void requestScope() throws Exception { assertThat(myRequestBean) .isSameAs(request.getAttribute(“myRequestBean”)); assertThat(myRequestBean) .isSameAs(wac.getBean(“myRequestBean”, … Read more

Properly implementing Java modules in a Maven build with inter-module test dependencies

Based on your demo project, I was able to duplicate your error. That said, here are the revised changes I made, after my first failed attempt, to be able to build the project: I added the maven-compiler-plugin version 3.8.0 to all the modules. You need a version of 3.7 or higher to compile modules with … Read more

MockMVC how to test exception and response code in the same test case

If you have an exception handler and you want to test for a specific exception, you could also assert that the instance is valid in the resolved exception. .andExpect(result -> assertTrue(result.getResolvedException() instanceof WhateverException)) UPDATE (gavenkoa) Don’t forget to inject @ExceptionHandler annotated methods to the test context or exception will occur at .perform() instead of capturing … Read more

Testing main method by junit [duplicate]

To provide the input from a file, make a FileInputStream and set that as the System.in stream. You’ll probably want to set the original back after the main method has finished to make sure anything using it later still works (other tests, JUnit itself…) Here’s an example: @Test public void testMain() throws IOException { System.out.println(“main”); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)