簡體   English   中英

使用JUnit,Spring 4和Vaadin對單元進行單元測試時獲得NPE

[英]Getting NPE while unit testing a class using JUnit, Spring 4 and Vaadin

我正在嘗試對非常基本的類進行單元測試,該類從Vaadin框架擴展了一些類。 但是運行測試時出現NullPointerException

我需要模擬UI類嗎? 請指導。

錯誤日志:

into the enter method......

java.lang.NullPointerException
    at com.jpmorgan.tss.payhub.template.views.MainView.enter(MainView.java:46)
    at com.jpmorgan.tss.payhub.template.views.MainViewTest.testEnter(MainViewTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

MainViewTest.java

public class MainViewTest {

    @Mock
    ViewChangeListener.ViewChangeEvent viewChangeEvent = Mockito.mock(ViewChangeListener.ViewChangeEvent.class);

    @Test
    public void testEnter() {
        MainView instance = new MainView();
        instance.enter(viewChangeEvent);
        assertNotNull(instance);
    }

}

MainView.java(被測類)

@Component
@Scope("prototype")
@VaadinView(MainView.NAME)
@Title("Mortgage Banking")
@Theme("PymtTemplateUI")
public class MainView extends VerticalLayout implements View {

    @PostConstruct
    public void PostConstruct() {
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        System.out.println("into the enter method......");
        UI ui = UI.getCurrent();
        Navigator nav = ui.getNavigator();
        nav.navigateTo("homePageView");
    }

}

我需要模擬UI類嗎? 請指導。

是的,您需要。 實際上,這並不是一件小事,因為您也需要在Vaadin中模擬其他內容。 因此,如果您想在不運行應用程序的情況下進行基本的UI測試,即稱為無瀏覽器測試的技術,則可以研究該實驗性開源項目

https://github.com/mvysny/karibu-testing

還有一篇博客文章 ,描述了如何模擬UI和其他所需的Vaadin類的原理。

另外(由於上述方法不能完全滿足集成測試需求),或者您可以使用Selenium或Vaadin TestBench進行UI測試,即自動瀏覽器測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM