spring-junit

Spring: Why is @PreDestroy not called at end of each test class?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 20:52:06
问题 I have an integration test class annotated as follows @WebAppConfiguration @ContextConfiguration(classes = {AppConfiguration.class}) @RunWith(SpringJUnit4ClassRunner.class) public class CacheConsumerTest { } Here's my AppConfiguration @Configuration @ComponentScan(basePackages = {"com.etc.etc.etc."}) @EnableWebMvc public class AppConfiguration { } For some reason, none of my @Component beans' @PreDestroy is getting called at the end of all tests in CacheConsumerTest . @PostConstruct is

unable to mock the resttemplate call using restclienttest

余生颓废 提交于 2020-04-30 06:56:05
问题 i want to mock my RestTemplate which uses RestTemplateBuilder. hence i am using restclienttest. unfortunately i am not able to mock the resttemplate call. when Sup supExpected = myService.getDetails("1234"), is called. its performing the complete backend call instead resulting what i have asked to do. instead of resulting the custom json string "SD", it is performing actual GET call and giving the response from backend. here is my jnuit code: @RunWith(SpringRunner.class) @RestClientTest

junit spring jms listener

落爺英雄遲暮 提交于 2020-01-23 12:49:20
问题 I want to unit test a simple jms listener code below @Component public class NotificationReader { @JmsListener(destination = "myAppQ") public void receiveMessage(NotificationMessage notificationMessage) { System.out.println("Received <" + notificationMessage.getStaffNumber() + ">"); } } From the junit, I am using jmsTemplate to pump the message into Active MQ. I want to test if the jms listener got invoked. I saw few solutions ( using counters ) like How to wait for @JMSListener annotated

spring Should I use @DirtiesContext on every class

拥有回忆 提交于 2019-12-20 09:05:15
问题 I have several junit tests, @ContextConfiguration(locations = { "file:../business/src/test/resources/application-context-test.xml", "file:src/main/webapp/WEB-INF/confA.xml", "classpath:/mvc-dispatcher-servlet-test.xml"}) @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class ProductContentControllerTest { ... } Inside a class all tests have to run in the same context (which is the case). But I want all my tests classes to be independent. I was assuming that it was the

How to test a controller with constructor injection by MockMvc

这一生的挚爱 提交于 2019-12-14 00:52:41
问题 I have a controller with constructor injection @RestController @RequestMapping("/user") public class MainController { private final UserMapper userMapper; // autowired by constructor below public MainController(UserMapper userMapper) { this.userMapper = userMapper; } @RequestMapping("/getChannels") public String index() { LoginUser user = userMapper.getUserByName("admin"); return "Channels: " + user.getChannels(); } } It's a simple class which is working fine. However when I tried to run a

spring Should I use @DirtiesContext on every class

不问归期 提交于 2019-12-02 17:37:58
I have several junit tests, @ContextConfiguration(locations = { "file:../business/src/test/resources/application-context-test.xml", "file:src/main/webapp/WEB-INF/confA.xml", "classpath:/mvc-dispatcher-servlet-test.xml"}) @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class ProductContentControllerTest { ... } Inside a class all tests have to run in the same context (which is the case). But I want all my tests classes to be independent. I was assuming that it was the default behavior, but when I run all the test together, it seems to run too fast. How does it work? Is the

Spring jUnit Testing properties file

谁都会走 提交于 2019-11-28 20:39:17
I have a jUnit Test that has its own properties file(application-test.properties) and its spring config file(application-core-test.xml). One of the method uses an object instantiated by spring config and that is a spring component. One of the members in the classes derives its value from application.properties which is our main properties file. While accessing this value through jUnit it is always null. I even tried changing the properties file to point to the actual properties file, but that doesnt seem to work. Here is how I am accessing the properties file object @Component @PropertySource(

Spring jUnit Testing properties file

蹲街弑〆低调 提交于 2019-11-27 11:40:26
问题 I have a jUnit Test that has its own properties file(application-test.properties) and its spring config file(application-core-test.xml). One of the method uses an object instantiated by spring config and that is a spring component. One of the members in the classes derives its value from application.properties which is our main properties file. While accessing this value through jUnit it is always null. I even tried changing the properties file to point to the actual properties file, but that