spring / jsf控制器的JUnit测试-数据重置

时间:2018-06-28 13:24:58

标签: java spring jsf junit primefaces

我遇到了以下问题。我有一个控制器,其模型包含一些字段,列表等。现在我有一个@Before方法,用于初始化一些测试数据。不幸的是,对于每次呼叫,例如model.getList().add(element),此列表都有新的参考,并且再次创建,并且我的数据丢失了。我可以调试到add方法中并检查它是否正确添加,然后在调用下一个add时,该列表具有新的引用,并且为空。由于这个问题,当涉及@Test方法时,它们应调用某些控制器方法并对先前添加的数据进行操作,但没有任何方法。我的控制器和模型的范围是@Scope(value = "view", proxyMode = ScopedProxyMode.TARGET_CLASS)。当我将其更改为@SessionScope时,它可以正常工作,但是由于应用程序的要求,我不得不将范围切换到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个元素。在我的模型中,常规对象(而不是集合)也是如此。任何帮助将不胜感激。

我正在使用在IntelliJ IDE,JSF版本3.2.1,JUnit 4.12上运行的SpringBoot 2.0.2 + PrimeFaces 6.2

0 个答案:

没有答案