spring-test

How to mock a static variable in java using JMock

喜夏-厌秋 提交于 2019-12-10 23:14:35
问题 I have a Unit testing problem where a class has a static variable which wants to load the Spring Application Ctx. This class DOES NOT come out of the Bean Factory and I cannot change this fact. static ApplicationContext applicationContext = ...; This works fine, but is hard to JMock , or atleast I don't know a way and until I can the Spring Ctx wants to start up. Not ideal for a unit test situation. Is there a work around that anyone knows? I have the option to change the static variable to

SpringWS Junit Test Exception: No endpoint can be found for request [SaajSoapMessage

青春壹個敷衍的年華 提交于 2019-12-10 19:06:29
问题 I am getting an exception when I try to run the Spring Web Service JUnit Test Using the Spring WebService ServerSide Integration Test using MockWebServiceClient. When I run the WebService Junit Test, I'm getting an exception: No endpoint can be found for request [SaajSoapMessagehttp://schemas.xmlsoap.org/soap/envelope/}Envelope] Spring_WS_ServletConfig @Configuration @EnableWs @EnableTransactionManagement // @ComponentScan({ "com.springws.endpoint", "com.mybatis.", "com.mapstruct" }) //

Make Spring Boot Recreate Test Databases

大憨熊 提交于 2019-12-10 15:54:41
问题 How can I get Spring Boot to recreate in-memory test databases from scratch between test classes? I've got several local integration tests annotated with @SpringApplicationConfiguration and @WebIntegrationTest that alter database state. I have marked each of these with @DirtiesContext . I was expecting the code to create the in-memory database would be part of the ApplicationContext lifecycle, and so a new one should get created in any subsequent tests. I can see in the logs that Flyway is

@Rollback(false) not working on @Before using SpringJUnit4ClassRunner

China☆狼群 提交于 2019-12-10 15:04:25
问题 In a JUnit test in my Spring application, I'd like to insert a lot of data in a setup method, and then use it to test against. However, whatever is done in the @Before method appears to be rolled back after each test, even if I annotate the method with @Rollback(false) Here's a simplified version of what I'm trying to do: public class TestClass { @Autowired MyService service; @Before public void setup() { if(service.getById(1) == null) { Thing thing = new Thing(); thing.setId(1); service

TestNG - @BeforeMethod for specific methods

放肆的年华 提交于 2019-12-10 14:15:44
问题 I'm using Spring Test with TestNG to test our DAOs, and I wanted to run a specific text fixture script before certain methods, allowing the modifications to be rolled back after every method so that the tests are free to do anything with the fixture data. Initially I thought that 'groups' would be fit for it, but I already realized they're not intended for that (see this question: TestNG BeforeMethod with groups ). Is there any way to configure a @BeforeMethod method to run only before

Can Spring Boot test classes reuse application context for faster test run?

China☆狼群 提交于 2019-12-10 12:32:48
问题 @ContextConfiguration location attribute does not make sense for Spring Boot integration testing. Is there any other way for reusing application context across multiple test classes annotated with @SpringBootTest ? 回答1: Yes. Actually it is default behavior. The link point to Spring Framework docs, which is used by Spring Boot under the hood. BTW, context is reused by default also when @ContextConfiguration is used as well. 回答2: @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) The

best way to integration test spring mvc

会有一股神秘感。 提交于 2019-12-10 11:56:56
问题 I have a Spring MVC 3.2 project that I would like to unit & integration tests. The problem is all the dependencies I have, makes testing extremely difficult even with Sprint-test. I have a controller like this: @Controller @RequestMapping( "/" ) public class HomeController { @Autowired MenuService menuService; // will return JSON @Autowired OfficeService officeService; @RequestMapping( method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE ) @ResponseBody public

How can I prevent my Spring Boot Batch application from running when executing test?

半腔热情 提交于 2019-12-10 09:27:18
问题 I have a Spring Boot Batch application that I'm writing integration tests against. When I execute a test, the entire batch application runs. How can I execute just the application code under test? Here's my test code. When it executes, the entire batch job step runs (reader, processor, and writer). Then, the test runs. @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = BatchApplication.class)) @TestExecutionListeners({ DependencyInjectionTestExecutionListener

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

随声附和 提交于 2019-12-10 02:56:13
问题 I'm using MapStruct, mapstruct-jdk8 version 1.1.0.Final and defining abstract class that I inject via Spring. I'm looking at how to be able to test them via Junit Test ? I've basicaly a main mapper that will use 2 sub mappers @Mapper(componentModel = "spring", uses = {SubMapper1.class, SubMapper2.class}) public abstract class MainMapper { @Mapping(target = "field1", qualifiedByName = {"MyMapper2Name", "toEntity"}) public abstract MyEntity toEntity(MyDto pDto); public MyDto fromEntity(MyEntity

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

喜你入骨 提交于 2019-12-10 02:14:06
问题 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 . 回答1: 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