Spring MockMvc verify body is empty

There’s a cleaner way: andExpect(jsonPath(“$”).doesNotExist()) Note that you can’t use isEmpty because it checks for an empty value, and assumes the existence of the attribute. When the attribute doesn’t exist, isEmpty throws an exception. Whereas, doesNotExist verifies that the attribute doesn’t exist, and when used with $, it checks for an empty JSON document.

How to write Junit test for mapstruct abstract mapper injected via Spring

In response to @Richard Lewan comment here is how I declared my test class for the abstract class ConfigurationMapper using 2 subMappers @RunWith(SpringRunner.class) @SpringBootTest(classes = {ConfigurationMapperImpl.class, SubMapper1Impl.class, SubMapper2Impl.class}) public class ConfigurationMapperTest { You use the Impl generated classes in the SpringBootTest annotation and then inject the class you want to test: @Autowired private ConfigurationMapper configurationMapper; … Read more

MockMvc no longer handles UTF-8 characters with Spring Boot 2.2.0.RELEASE

Yes. This is problem from 2.2.0 spring-boot. They set deprecation for default charset encoding. .getContentAsString(StandardCharsets.UTF_8) – good but in any response would be populated ISO 8859-1 by default. In my project I updated current created converter: @Configuration public class SpringConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.stream() .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter) … Read more

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

I’d call what you’re after an integration test based on the terminology in the Spring reference manual. How about doing something like: import static org.springframework.test.web.ModelAndViewAssert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({/* include live config here e.g. “file:web/WEB-INF/application-context.xml”, “file:web/WEB-INF/dispatcher-servlet.xml” */}) public class MyControllerIntegrationTest { @Inject private ApplicationContext applicationContext; private MockHttpServletRequest request; private MockHttpServletResponse response; private HandlerAdapter handlerAdapter; private MyController controller; … Read more

Spring beans redefinition in unit test environment

I would propose a custom TestClass and some easy rules for the locations of the spring bean.xml: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { “classpath*:spring/*.xml”, “classpath*:spring/persistence/*.xml”, “classpath*:spring/mock/*.xml”}) @Transactional @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class, DirtiesContextTestExecutionListener.class}) public abstract class AbstractHibernateTests implements ApplicationContextAware { /** * Logger for Subclasses. */ protected final Logger log = LoggerFactory.getLogger(getClass()); /** * The {@link ApplicationContext} that was … Read more

Configure specific in memory database for testing purpose in Spring

Spring profiles can be used for this. This would be a specific way: Have environment specific properties files: application.properties: spring.profiles.active: dev application-dev.properties spring.jpa.database: MYSQL spring.jpa.hibernate.ddl-auto: update spring.datasource.url: jdbc:mysql://localhost:3306/dbname spring.datasource.username: username spring.datasource.password: password application-test.properties spring.jpa.database: HSQL Have both MySQL and H2 drivers in pom.xml, like this: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>test</scope> </dependency> … Read more

@RunWith(SpringRunner.class) vs @RunWith(MockitoJUnitRunner.class)

The SpringRunner provides support for loading a Spring ApplicationContext and having beans @Autowired into your test instance. It actually does a whole lot more than that (covered in the Spring Reference Manual), but that’s the basic idea. Whereas, the MockitoJUnitRunner provides support for creating mocks and spies with Mockito. However, with JUnit 4, you can … Read more

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

One of your @Configuration classes is obviously annotated with @EnableWebMvc. That’s how DelegatingWebMvcConfiguration ends up in your stack trace, since it is imported by @EnableWebMvc. So although you think you don’t need a WebApplicationContext (and hence a ServletContext), you in fact do need it simply because you are loading an application context with @EnableWebMvc. You … Read more

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