ในแอปพลิเคชัน JSF 2.2 ฉันต้องการสร้างไฟล์ war สำหรับการทดสอบกับ Selenium ใน webtest.war นั้น ฉันต้องการแทนที่คลาสกลางที่เรียกว่า NodeCache ด้วยเวอร์ชันจำลองที่เรียกว่า WebtestNodeCache เพื่อไม่ให้ฐานข้อมูลและการขึ้นต่อกันภายนอกอื่น ๆ ออกจากการทดสอบ
NodeCache เป็นถั่วที่ได้รับการจัดการ:
@javax.faces.bean.ManagedBean(name = NodeCache.INSTANCE)
@javax.faces.bean.ApplicationScoped
public class NodeCache {
public static final String INSTANE = "nodecache";
// ...
}
หากต้องการแอบเข้าไปใน WebtestNodeCache ฉันใช้ ServletContextListener เช่นนี้:
public class WebtestContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
WebtestNodeCache nodeCache = new WebtestNodeCache();
ServletContext context = event.getServletContext();
context.setAttribute(NodeCache.INSTANCE, nodeCache);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {}
}
ในบิลด์ปกติ WebtestContextListener และ WebtestNodeCache จะถูกแยกออกจากไฟล์ war และในบิลด์ทดสอบจะถูกรวมไว้ด้วย
ดูเหมือนว่าจะได้ผล: เมื่อฉันเข้าสู่ระบบ ฉันได้รับโหนดจำลองจาก WebtestNodeCache
นี่เป็นวิธีที่เชื่อถือได้ในการแทนที่ bean ในบริบทของแอปพลิเคชันหรือฉันเพิ่งโชคดี
มีวิธีที่ดีกว่าในการแอบเข้าไปในหุ่นทดสอบหรือไม่?