java - spring/jsf控制器的JUnit测试——数据重置
问题描述
我遇到了以下问题。我有一个带有模型的控制器,其中包含一些字段、列表等。现在我有一个@Before
初始化一些测试数据的方法。不幸的是,像model.getList().add(element)
这个列表这样的每个调用都有新的引用并再次创建并且我的数据丢失了。我可以调试add
方法并检查它是否正确添加,然后在add
调用 next 时,列表有新的引用并且为空。由于这个问题,当涉及到@Test
方法时,他们应该调用一些控制器方法并对之前添加的数据进行操作,但是没有。我的控制器和模型的范围是@Scope(value = "view", proxyMode = ScopedProxyMode.TARGET_CLASS)
. 当我将其更改为@SessionScope
then 它工作正常,但我不得不将范围切换到view
因为申请要求。你能帮我解决这个问题吗?也许其他测试方式或任何其他想法?
这是我的控制器类:
@Controller
@Scope(value = "view", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class BaseController extends AbstractController<BaseModel> {
//some logic
}
AbstractController 的一部分:
@Autowired
protected T model;
@Override
public T getModel() {
return model;
}
模型类:
@Repository
@Scope(value = "view", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class BaseModel extends AbstractModel {
@PostConstruct
public void init() {
//initialization stuff
}
//data logic methods
}
最后是我的测试课:
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class)
@AutoConfigureTestDatabase
public class BaseControllerTest {
@Autowired
private WebApplicationContext context;
@Autowired
private BaseController controller;
@Autowired
private BaseModel model;
@Autowired
private LoggingDao loggingDao;
private MockMvc mvc;
private UsbDeviceDBO usbDeviceDBO1;
private UsbDeviceDBO usbDeviceDBO2;
private UsbDeviceDBO usbDeviceDBO3;
private UsbDeviceDBO usbDeviceDBO4;
private UsbDeviceDBO usbDeviceDBO5;
private UsbDeviceDBO usbDeviceDBO6;
@Before
public void before() {
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
FacesContext context = ContextMocker.mockFacesContext();
UIViewRoot uiViewRoot = Mockito.mock(UIViewRoot.class);
Mockito.when(context.getCurrentInstance().getViewRoot())
.thenReturn(uiViewRoot);
Mockito.when(
context.getCurrentInstance().getViewRoot().getLocale())
.thenReturn(new Locale("en"));
}
public void insertTestData() {
usbDeviceDBO1 = new UsbDeviceDBO("device1", EDeviceUsageMode.ALWAYS, "Custom", "comment1");
usbDeviceDBO2 = new UsbDeviceDBO("device2", EDeviceUsageMode.INSTALL, "Custom", "comment2");
usbDeviceDBO3 = new UsbDeviceDBO("device3", EDeviceUsageMode.ALWAYS, "Custom", "comment3");
usbDeviceDBO4 = new UsbDeviceDBO("USB\\class_01", EDeviceUsageMode.ALWAYS, "Class 01: Audio", "comment4");
usbDeviceDBO5 = new UsbDeviceDBO("USB\\VID_05BA&PID_000A", EDeviceUsageMode.ALWAYS, "[Diebold] (Digital Persona, Inc) Biometric Reader", "comment5");
usbDeviceDBO6 = new UsbDeviceDBO("USB\\VID_0596&PID_0001", EDeviceUsageMode.ALWAYS, "[Wincor Nixdorf] 3M Touch Controller", "comment5");
model.getUsbDeviceTable().add(usbDeviceDBO1);
model.getUsbDeviceTable().add(usbDeviceDBO2);
model.getUsbDeviceTable().add(usbDeviceDBO3);
model.getUsbDeviceTable().add(usbDeviceDBO4);
model.getUsbDeviceTable().add(usbDeviceDBO5);
model.getUsbDeviceTable().add(usbDeviceDBO6);
model.getUsbDeviceTable().clearSelection();
}
@Test
public void updateUsedDeviceIdsTest() {
insertTestData();
UsbDeviceDBO clonedDevice = new UsbDeviceDBO("device1", EDeviceUsageMode.ALWAYS, "Custom", "comment1");
controller.updateUsedDeviceIds(clonedDevice);
assertEquals(5, model.getUsedDeviceIds().size());
}
每次涉及updateUsedDeviceIdsTest
方法时,断言失败但应该有 5 个元素,但是当添加测试数据时,每次调用后model.getUsbDeviceTable().add(usbDeviceDBO1);
我都会获得对数据列表的新引用。因此,当我controller.updateUsedDeviceIds(clonedDevice);
开始时,列表中有 0 个元素。在我的模型中,常规对象而不是集合也会发生同样的情况。任何帮助,将不胜感激。
我正在使用 SpringBoot 2.0.2 + PrimeFaces 6.2,在 IntelliJ IDE、JSF 版本 3.2.1、JUnit 4.12 上运行
解决方案
推荐阅读
- python - 如何在 python 中只打印字符串的字母?
- android - 使用 Kotlin 将低功耗蓝牙添加到 Android 应用程序
- c - main() 前后的函数声明
- c# - 如何忽略文本框中的特定字符?
- python - 无法使用 conda 创建环境
- amazon-s3 - Amazon S3 的只读存储桶策略设置 - 用于流式传输音频片段
- node.js - 使用 Node 连接到 MongoDB 服务器在 localhost 上无响应
- python - 当我应该向其附加大量文本时的空列表
- sql - 我如何用来自 3 个不同表的数据填充我的表?
- java - Mysql 服务器将 LocalDateTime 保存在操作系统时间之外 3 小时