spring-test

Spring Boot Integration test random free port

断了今生、忘了曾经 提交于 2019-12-14 00:37:23
问题 I am able to get Spring Boot integration to generate a random free port to launch itself on. But I also need a free port for Redis. @ContextConfiguration(classes = {MyApplication.class}, loader = SpringApplicationContextLoader.class) @WebIntegrationTest(randomPort = true, value = "server.port:0") @ActiveProfiles(profiles = {"local"}) public class SegmentSteps { private static final String HOST_TEMPLATE = "http://localhost:%s"; // Needs to be a random open port private static final int REDIS

Spring OAuth and Boot Integration Test

假装没事ソ 提交于 2019-12-14 00:24:09
问题 What is the best way to run Spring Boot integration tests agains a OAuth Resource server configured web application. I can think of two theoretical approaches: Mock the security context in the resource server without acutally calling the Authorization server. Embed the Authorization server as part of the test and redirect the authentication to it. I was wondering how others have approach this problem. 回答1: I use spring security 4.x @WithSecurityContext('user') annotation to create mock

@Autowired bean is null in Test Listener class

北慕城南 提交于 2019-12-13 04:59:24
问题 This question was asked before Using Autowired in a TestExecutionListener class for BeforeClass junit however it wasn't answered. I am facing the same problem but haven't figured out the solution Example: I am getting null mapper. public class CustomExecutionListener extends AbstractTestExecutionListener { @Autowired private Mapper mapper; @Override public void beforeTestClass(TestContext testContext) {} ... some code... } Test Class: Note: AppConfig contains the Mapper Bean defined. @RunWith

NoClassDefFoundError: Tests fail during Maven test phase when SureFire forkCount is greater than 0

本秂侑毒 提交于 2019-12-13 00:23:05
问题 Issue Java 9 / Java 10 Module Mode: Tests failing due to initializationError: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.test.context.junit4.SpringRunner when SureFire forkCount > 0 Spring Boot: 2.0.1.RELEASE Java: "10" 2018-03-20 (Vendor: Oracle Corporation) Maven: 3.5.3 maven-compiler-plugin: 3.7.0 maven-surefire-plugin: 2.21.0 macOS: 10.12.6 Relevant stack trace [INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ turbo --- [INFO] [INFO] -------

How to test this method with spring boot test?

喜你入骨 提交于 2019-12-13 00:20:06
问题 I want test the method like this @PostMapping(value = "/test") public String test(@Valid TestModel model) { return model.getUsername(); } and the TestModel is this @Getter @Setter public class TestModel { private MultipartFile[] image1; private MultipartFile[] image2; private MultipartFile[] image3; private String username; private String password; } I can use httpclient to test this but I don't think this is a good idea,so any other methods with spring test? 回答1: If, on the other hand you

Using @PostConstruct in a test class causes it to be called more than once

…衆ロ難τιáo~ 提交于 2019-12-12 19:51:09
问题 I am writing integration tests to test my endpoints and need to setup a User in the database right after construct so the Spring Security Test annotation @WithUserDetails has a user to collect from the database. My class setup is like this: @RunWith(value = SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc @WithUserDetails(value = "email@address.com") public abstract class IntegrationTests { @Autowired private MockMvc mockMvc

Error: No URL for ServletContext resource when running Spring integrated test

非 Y 不嫁゛ 提交于 2019-12-12 19:13:00
问题 I am running an integrated test for my Spring controller. Test includes a configuration file( view-configuration.xml ) that has tilesConfigurer bean for which I am getting the error No URL for ServletContext resource . Below is Error Trace, Configuration, Test & Project Structure: java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springframework.test

Test maximum upload file size with MockMultipartFile

最后都变了- 提交于 2019-12-12 10:39:10
问题 I create a file upload service with Spring Boot and test it with Spring Mock Mvc and MockMultipartFile. I want to test if an error is thrown when the maximum file size is exceeded. The following test fails because it receive a 200. RandomAccessFile f = new RandomAccessFile("t", "rw"); f.setLength(1024 * 1024 * 10); InputStream is = Channels.newInputStream(f.getChannel()); MockMultipartFile firstFile = new MockMultipartFile("data", "file1.txt", "text/plain", is); mvc.perform(fileUpload("/files

@SpringBootTest not creating inner beans while loading the context

五迷三道 提交于 2019-12-12 04:04:54
问题 I would like to learn why inner beans are not created while trying to test like below : RunWith(SpringRunner.class) @SpringBootTest(classes=MyTest.class) public class MyTest { @SpyBean A a; @Test public void myTest() { assertTrue(a.some()); } @Component class A { private B b; A(B dependency) { this.b = dependency; } boolean some() { return b.value(); } } @Configuration class B { boolean value() { return true; } } } Error: No qualifying bean of type 'com.example.MyTest$B' available: expected

Spring profiles on integration tests class

人盡茶涼 提交于 2019-12-11 23:23:00
问题 we have selenium tests which are ran by java test class. On local environment everything is ok, but I want to switch off those tests when run on jenkins. So I use: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebIntegrationTest("server.port=1234") @Profile("!jenkins") @ActiveProfiles("integrationtests") public class LoginAndEditProfileSeleniumTest { ... What works: running mvn clean test run all tests locally, with integrationtests