我正在用Fitnesse測試測試Spring Controller方法。 在被測試的類中,applicationContext是Autowired(通過註釋)。 當從Fixture而不是Spring框架調用方法時,看起來自動裝配不起作用。 是否有另一種方法來檢索applicationContext?我無法使用XML文件,因爲我們正在使用註釋。我已經搜索谷歌「註釋Spring applicationContext Fitnesse爪哇」,但我沒有找到任何東西。 這裏是我的代碼:在Fitnesse測試中檢索applicationContext
@Controller
public class MySpringController {
@Autowired
private ApplicationContext applicationContext;
@RequestMapping(value = "/saveAllSecurityMaintenanceAllocations/",
method = {RequestMethod.POST })
public ResponseEntity myMethod(@RequestBody final List<String> inList,
final HttpServletRequest request) throws Exception {
if (applicationContext == null) {
System.out.println("applicationContext is null");
}
//functionality I need to test is in here and is using applicationContext
}
}
public class MyTestFixtureTest extends TableFixture {
List<String> aList = new ArrayList<String>();
public void setUp(){
aList.add("foo");
aList.add("foo2");
}
public void showResults(int fitnessRows){
MySpringController mySpringController = new MySpringController()
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
applicationContext = UnitTestConfig.createApplicationContext();
mySpringController.myMethod(aList, httpServletRequest)
}
}
「ApplicationContext已空」 正在打印。有誰知道如何模擬自動裝配或瞭解另一種在Fitnesse測試中獲取applicationContext的方法嗎?
雖然我沒有特定的解決方案,但我會爭辯說需要一個AppContext(因此在單元測試中需要它)是錯誤的方法。它擊敗了IoC的目的。你能夠重構你的代碼,以便它不需要AppContext嗎? – mauzel 2015-01-26 18:55:40
更不用說單元測試中應用環境中的接線使得這些單元測試極其緩慢。如果你的代碼庫有數千個單元測試,那就沒有趣味了。 – mauzel 2015-01-26 19:00:17
問題是我單元測試了一個使用applicationContext的方法。該方法使用多個表,就像應用程序中的其他方法一樣。 (時間,用戶信息,參考數據等) – 2015-01-26 19:25:51