我使用 Spring 將@Profile與測試相關的類與開發(fā)和產(chǎn)品分開。我在尋找一種在 pom.xml 中設置spring.profiles.activeto的方法時遇到了麻煩,僅出于目標。換句話說,如果 Maven 目標是我想運行這個:testtesttestmvn test仍然可以訪問帶有 @Profile("test") 注釋的類,而不是這樣:mvn test -Dspring.profiles.active=test因為它指定了運行兩次的“測試”性質。有可能嗎?更新:添加代碼以下兩項服務用于測試和開發(fā)/生產(chǎn)。兩者都實現(xiàn)相同的接口 MyServiceMyService測試環(huán)境:@Service@Profile("test")@ActiveProfiles("test")public class TestMyServiceImpl implements MyService { @Override public String myMethod(){ ... }}MyService 開發(fā)環(huán)境:@Servicepublic class DevMyServiceImpl implements MyService { @Override public String myMethod(){ ... }}自動裝配 MyService 的控制器:@RestController@RequestMapping public class MyController { @Autowired private MyService myService;@RequestMapping(value = /myendpoint, method = RequestMethod.POST) public @ResponseBody Response foo(@RequestBody String request) { Response response = new Response(); response.setResult(myService.myMethod()); return response; }}
1 回答

人到中年有點甜
TA貢獻1895條經(jīng)驗 獲得超7個贊
您可以使用注釋通過測試配置文件運行測試類,請按照以下步驟操作
第 1 步:@Profile("name")
用和注釋所有測試類@ActiveProfiles("name")
Profile
注釋用于拾取指定的配置文件ActiveProfiles
用于激活測試類的指定配置文件
第 2 步:創(chuàng)建application.properties
或application.yml
使用配置文件名稱(如application-test.yml)并將其放置在src/main/resources
或src/test/resources
與測試屬性一起放置
添加回答
舉報
0/150
提交
取消