spring-test

How can I use @IfProfileValue to test if a Profile is active?

旧城冷巷雨未停 提交于 2019-11-30 09:25:18
So confusingly @IfProfileValue has nothing to do with @Profile or @ActiveProfiles . @Profile tests to see if a profile is active, @ActiveProfiles sets them as active, and @IfProfileValue allows you to check things in Spring Environment . Wut? I'd deprecate all of them and add new ones @IfEnvironment , @IfProfile , and @ActivateProfiles . Commentary aside, how can I use @IfProfileValue to detect whether i have a profile active? I am not using Spring Boot on this project, at this time. Answers should show code, and we will assume that I want the test to run if the profile is activated as

How can I mock db connection in Spring Boot for testing purpose?

走远了吗. 提交于 2019-11-30 08:32:12
Situation: I am using Spring Cloud with Spring Boot in a microservice, that microservice is loading a DB config information to configure a connection. I created a test to get the rest interfaces using Swagger for documentation. I want to disable the loading of DB configuration because is not necessary. Here is the code: @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {Application.class, Swagger2MarkupTest.class}, loader = SpringApplicationContextLoader.class) @ActiveProfiles("test") public class Swagger2MarkupTest { @Autowired private

How to clean up mocks in spring tests when using Mockito

谁说我不能喝 提交于 2019-11-30 06:01:11
I'm pretty new to Mockito and have some trouble with clean up. I used to use JMock2 for unit tests. As far as I know, JMock2 preserves the expectations and other mock information in a context which will be rebuilt for every test method. Therefore every test method is not interfered by the others. I adopted the same strategy for spring tests when using JMock2, I found a potential problem with the strategies I used in my post : The application context is rebuilt for every test method and therefore slows the whole test procedure. I noticed many articles recommend using Mockito in spring tests and

Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

房东的猫 提交于 2019-11-30 05:48:12
I am testing my controller given below @Controller public class MasterController { @GetMapping("/") public String goLoginPage(){ return "index"; } } I am following this Spring documentation to test my controller. Now, I want to test my controller by just instantiating the web layer and not the whole Spring context as given in the documentation. Below is my code for the same. @RunWith(SpringRunner.class) @WebMvcTest public class MasterControllerTestWithWebLayer { @Autowired MockMvc mockMvc; @Autowired MasterController masterController; @Before public void setUp() throws Exception { } @After

How to set servlet path for every request through MockMvc

元气小坏坏 提交于 2019-11-30 05:35:41
问题 Is it possible to set the servlet path for all requests (get, post, put, delete) which go through the MockMvc? The Spring dispatch servlet is mapped to /rest/* But in my test I have to remove the /rest part in the url otherwise Spring test does not recognise the controller. EDIT @Sotirios: Something is possible like: public class MyWebTests { private MockMvc mockMvc; @Before public void setup() { mockMvc = standaloneSetup(new AccountController()) .defaultRequest(get("/") .contextPath("/app")

Spring dependency injection into Spring TestExecutionListeners not working

左心房为你撑大大i 提交于 2019-11-30 03:28:29
问题 How can I use Spring dependency injection into a TestExecutionListener class I wrote extending AbstractTestExecutionListener? Spring DI does not seem to work with TestExecutionListener classes. Example of issue: The AbstractTestExecutionListener: class SimpleClassTestListener extends AbstractTestExecutionListener { @Autowired protected String simplefield; // does not work simplefield = null @Override public void beforeTestClass(TestContext testContext) throws Exception { System.out.println(

How to use @ComponentScan together with test-specific ContextConfigurations in SpringJunit4TestRunner?

一笑奈何 提交于 2019-11-30 01:48:24
问题 I am testing a Spring Boot application. I have several test classes, each of which needs a different set of mocked or otherwise customized beans. Here is a sketch of the setup: src/main/java: package com.example.myapp; @SpringBootApplication @ComponentScan( basePackageClasses = { MyApplication.class, ImportantConfigurationFromSomeLibrary.class, ImportantConfigurationFromAnotherLibrary.class}) @EnableFeignClients @EnableHystrix public class MyApplication { public static void main(String[] args

How to use spring's MockMultipartHttpServletRequest? Getting “no multipart boundary was found”

放肆的年华 提交于 2019-11-29 20:57:15
问题 Clearly I'm not using this test fixture right. My servlet works just fine in tomcat, but when I try to use this mock, the multi-part boundary is not found. "the request was rejected because no multipart boundary was found". There is an answer here that shows how to use this using a text file, but that answer sets the boundary string explicitly and embeds the file as test. I would think I would not need to do with by hand with methods like mockrequest.addFile (...) What am I not setting here

Ehcache shutdown causing an exception while running test suite

我的梦境 提交于 2019-11-29 16:55:10
问题 I'm experiencing the following problem. I have a test suit in my project and each individual test runs fine. However when I run them as a suite I some of them fails with the following exception: Caused by: java.lang.IllegalStateException: The dao Cache is not alive (STATUS_SHUTDOWN) at net.sf.ehcache.Cache$CacheStatus.checkAlive(Cache.java:4269) at net.sf.ehcache.Cache.checkStatus(Cache.java:2703) at net.sf.ehcache.Cache.get(Cache.java:1576) at org.springframework.cache.ehcache.EhCacheCache

Java annotations - code simplifications

牧云@^-^@ 提交于 2019-11-29 16:08:57
I am looking for a way to simplify the following code. @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { // My configuration classes }) public class MyServiceTest { @Autowired private MyService service; @Test public void myTest() { Assert.assertTrue(service != null); } } I have many configuration classes and I don't want to put them into each test class. So I got the idea to create my own annotation: @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { // My configuration classes }) public @interface