How to mock remote REST API in unit test with Spring?
If you use Spring RestTemplate you can use MockRestServiceServer. An example can be found here REST Client Testing With MockRestServiceServer.
If you use Spring RestTemplate you can use MockRestServiceServer. An example can be found here REST Client Testing With MockRestServiceServer.
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.
A mock (no matter if we talk about ordinary objects or beans) is simply an “empty shell”. That mock object doesn’t have any relation to the underlying production code. It is an object that looks like being an object of class X. But none of the methods or fields that X has do “really” exist … Read more
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
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
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
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
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
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
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