假设我有以下 SoapApplication 启动器:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties 中的一些属性在哪里
在测试中我有:
public abstract class SoapTest {
protected static ConfigurableApplicationContext server;
protected static HttpClient client;
@BeforeAll
public static void setUp() {
server = SpringApplication.run(Application.class,"--a=1","--b=2");
server.start();
}
@AfterAll
public static void tearDown() {
server.stop();
}
}
所以我对“--a=1”、“--b=2”不满意
我更喜欢设置 test.properties
我尝试过做这样的事情:
@Configuration
@EnableAutoConfiguration
@PropertySource("file:testdata/test.properties")
public class TestConfig {
}
和 SpringApplication.run(TestConfig.class, args);
但它仍然通过 application.properties 启动。
怎样才能做得好???
我想我不能使用来自 Override default Spring-Boot application.properties settings in Junit Test 的建议 虽然我使用的不是 Junit5(?)。
这样做了:
System.setProperty("spring.config.location", "文件:testdata/test.properties"); 服务器 = SpringApplication.run(Application.class);
正确吗?它对我有用,但可能在最佳实践中并没有多大用处?
最佳答案
如果您选择使用 Spring,我认为您应该考虑使用 Spring WebServices,您可以通过它对像这样的 WebServices 提供完整的测试支持
import static org...ws.test.server.RequestCreators.*;
import static org...ws.test.server.ResponseMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-ws-servlet.xml")
public class ServerTest {
@Autowired ApplicationContext ac;
MockWebServiceClient mockClient;
@Before
public void setUp() {
mockClient = MockWebServiceClient.createClient(ac);
}
@Test
public void test() {
//given
Source request = new StringSource("<MyRequest>...");
//when, then
Source response = new StringSource("<MyResponse>...");
mockClient.sendRequest(withPayload(request)).andExpect(payload(response));
}
}
关于java - Spring新手: how to override properties for Test application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40772726/