spring-test

how to run/turn off selective tests based on profiles in spring boot

蓝咒 提交于 2019-12-05 03:39:58
I have a spring-boot application for which I am writing IT tests. The data for the tests comes from application-dev.properties when I activate dev profile Here is what I have for tests: @RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration public class ApplicationTests { @Autowired Environment env; @Test public void contextLoads() { System.out.println(Arrays.toString((env.getActiveProfiles()))); } } ServiceITTest public class ServiceITTest extends ApplicationTests { @value String username; @value String address; @Autowired MyService myService; @Test public void check_for_valid

Spring JUnit Test not loading full Application Context

霸气de小男生 提交于 2019-12-05 02:11:25
Hi I am trying to so spring junit test cases... and I require my full application context to be loaded. However the junit test does not initialize the full application context. Test class: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) public class MongoDbRepositoryTest { @Value("${spring.datasource.url}") private String databaseUrl; @Inject private ApplicationContext appContext; @Test public void testCRUD() { System.out.println("spring.datasource.url:" + databaseUrl); showBeansIntialised(); assertEquals(1, 1); } private void

Use one spring boot context through all SpringBootTests

余生长醉 提交于 2019-12-05 02:03:52
问题 I want to be able to cache application context through different classes with tests using junit. Test classes are declared this way: @SpringBootTest @RunWith(SpringRunner.class) public class SomeIntegrationTest { } I saw this question Reuse spring application context across junit test classes but in this case I don't use any xml and I want to start context completely, not just few beans from it, so @SpringBootTest is more suitable than @ContextConfiguration , if I got it right. 回答1: Ruslan,

What is difference between @SpyBean and @MockBean in Mockito?

我们两清 提交于 2019-12-05 01:16:40
What is the difference between the @SpyBean and @MockBean annotations in Mockito? I have already gone through the JavaDoc but didn't get the difference. If possible please give an example when to use MockBean and when SpyBean . 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 on that mocked thing. Whereas a spy wraps around an existing object of your class

JUnit rollback transaction with @Async method

只愿长相守 提交于 2019-12-05 01:10:18
问题 I am writing an integration test using SpringJUnit4ClassRunner . I have a base class: @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration({ /*my XML files here*/}) @Ignore public class BaseIntegrationWebappTestRunner { @Autowired protected WebApplicationContext wac; @Autowired protected MockServletContext servletContext; @Autowired protected MockHttpSession session; @Autowired protected MockHttpServletRequest request; @Autowired protected MockHttpServletResponse

test suite inside spring context

淺唱寂寞╮ 提交于 2019-12-05 00:17:48
问题 Is it possible to run test suite with loaded spring context, something like this @RunWith(Suite.class) @SuiteClasses({ Test1.class, Test2.class }) @ContextConfiguration(locations = { "classpath:context.xml" }) <------ public class SuiteTest { } The code above obviously wont work, but is there any way to accomplish such behavior? This is currently how spring context is used in my test suite: @BeforeClass public static void setUp() { final ConfigurableApplicationContext context = loadContext

Spring overriding primary bean with non-primary bean

ε祈祈猫儿з 提交于 2019-12-04 22:52:09
I am trying to override a Spring bean during a test declared in a test configuration with the use of @Primary. One declaration is in the src/main/java path, the other, the primary, is in src/test/java path. However, Spring is intentionally replacing the primary bean with the the non-primary bean, the one I don't want to use for the test. If I simply comment out the production (src/main/java) configuration bean, it uses the primary test (src/main/test) bean in the test configuration as desired. (Clearly I can't comment out code every time I want to run a test.) From the logs: o.s.b.f.s

Writing tests to verify received msg in jms listener (Spring-Boot)

孤街醉人 提交于 2019-12-04 22:11:55
问题 I want to write test for something like below; There is a listener called state-info-1 in src/main . It does some changes to any message it gets and publishes the new message on activemq topic state-info-2 . I will build a dummy message and publish on to activemq topic state-info-1 . Finally verify that, the received message on topic state-info-2 is like i expected. My Listeners are like; @JmsListener(destination = "state-info-1", containerFactory = "connFactory") public void receiveMessage

How can I initialize a Spring applicationContext just once for all tests

筅森魡賤 提交于 2019-12-04 20:17:35
I have a set of tests based which need a spring context. For fast test execution I want to make sure that the Spring context is initialized just once, then all the tests should be run against this context, then it should shut down. I already tried the following approaches: Use @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(MyAnnotatedConfig.class) to initialize the spring context Use a @RunWith(SpringJUnit4ClassRunner.class) and @TestExecutionListeners({MyTestExecutionListener.class}) with a handwritten test execution listener that initializes the spring context and injects

Is it OK to use SpringRunner in unit tests?

故事扮演 提交于 2019-12-04 17:41:30
We are arguing about this approach with my colleagues. They say to use SpringRunner only on integration or functional levels. The question is what pros and cons of using it in level below? For example I have simple bean: public class RewardDurationCalculator { private Clock clock; public OptionalLong calculate(DurationType durationType, List<Pass> passes) { long now = Instant.now(clock).getEpochSecond(); switch (durationType) { case FULL_PASS: return getCurrentPassDuration(passes, now); case TILL_THE_END_OF_THE_CURRENT_ACTIVE_PASS: return getTimeInCurrentPassLeft(passes, now); } return