簡體   English   中英

由於在模擬方法時未在Mockito中加載屬性,所以發生java.lang.NullPointerException

[英]java.lang.NullPointerException is occuring due to property is not loading in Mockito when mocking a method

我是Mockito的新手,由於無法從appication.propertie的文件中加載屬性,因此我面臨一個問題。

問題陳述:我正在嘗試模擬使用application.properties文件中的屬性的方法。 當控件到達要加載屬性值的行時,它顯示為null,並且由於此Mockito拋出java.lang.NullPointerException

我正在尋找的是在模擬方法時如何從application.properties文件加載屬性。 在這里我正在嘗試加載全局變量partsListGlobal 。請幫助我如何實現這一目標。

這是我下面的代碼片段。

@Service
public class ClimoDiagnosticReportServImpl implements ClimoDiagnosticReportService {

    @Value("${PARTS_LIST}")
    private String partsListGlobal;

    @Override
    public boolean getSomeResult() {
        String[] partsListLocal = getPartsList();

        List<String> partsList = Arrays.asList(partsListGlobal);

        if (partsList.contains("PART_X1"))
            return true;
        else
            return false;
    }

    public String[] getPartsList() {
        return partsListGlobal.split(",");// Here is the error occuring due to partsListGlobal is not loading the value from application.properties file.
    }
}

@RunWith(MockitoJUnitRunner.class)
public class ClimoDiagnosticReportServImplTest {

    @InjectMocks
    private ClimoDiagnosticReportServImpl serviceReference1;

    @Mock
    private ClimoDiagnosticReportServImpl serviceReference12;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getSomeResultTest() {

        boolean result1 = false;
        String[] strArray = new String[2];
        strArray[0] = "P1";
        strArray[1] = "P2";
        Mockito.when(serviceReference12.getPartsList()).thenReturn(strArray);
        boolean result2 = serviceReference1.getSomeResult();
        Assert.assertEquals(result1,result2);

    }
}

錯誤:

com.test.serviceimpl.ClimoDiagnosticReportServImpl.getSomeResult(ClimoDiagnosticReportServServimpl.java:57)上com.test.serviceimpl.ClimoDiagnosticReportServImpl.getPartsList(ClimoDiagnosticReportServImpl.java:68)處的java.lang.NullPointerException位於sun.reflect.NativeMethodAccessorImpl.invoke的ClimoDiagnosticReportServImplTest.java:74)(位於Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)的本地方法)(位於Sun.reflect.DelegatingMethodAccessorImpl.invoke(Java方法的DelegatingMethodAccessorImpl.java:43) org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)的org.junit.internal.runners.model.ReflectiveCallable.run(.lang.reflect.Method.invoke(Method.java:498)在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)在ReflectiveCallable.java:12) org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)上的.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java :78)在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)在org.junit.runners.ParentRunner $ 1.schedule(父母在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在org.junit.runners.ParentRunner $ 2在org.mockito的org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)的org.junit.runners.ParentRunner.run(ParentRunner.java:363)的.evaluate(ParentRunner.java:268) org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)的Runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)在org.eclipse.jdt.internal org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)上的.junit.runner.TestExecution.run(TestExecution.java:38),位於org.eclipse.jdt.internal.junit.runner org.eclipse.jdt.internal.junit.runner上的.RemoteTestRunner.runTests(RemoteTestRunner.java:678)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main上的.RemoteTestRunner.run(RemoteTestRunner.java:382) (RemoteTestRunner.java:192)

提前謝謝大家。

您沒有任何要在服務中進行模擬的依賴項。 因此,完全不需要Mockito。 您需要做的是設置私有String字段,該字段由Spring使用反射在應用程序中填充。

只需遵循使用cnstructor注入而不是字段注入的最佳實踐os,這將使您的代碼可測試(這是最佳實踐的原因之一):

@Service 
public class ClimoDiagnosticReportServImpl implements ClimoDiagnosticReportService {

    private String partsListGlobal;

    public ClimoDiagnosticReportServImpl(@Value("${PARTS_LIST}") String partsListGlobal) {
        this.partsListGlobal = partsListGlobal;
    }

    // ...
}

您的測試現在可以簡化為

public class ClimoDiagnosticReportServImplTest {

    @Test
    public void shouldReturnTrueIfPropertyContainsPartX1() {
        ClimoDiagnosticReportServImpl service = new ClimoDiagnosticReportServImpl("a,b,c,PART_X1,d");
        assertTrue(service.getSomeResult());
    }

    @Test
    public void shouldReturnFalseIfPropertyDoesNotContainPartX1() {
        ClimoDiagnosticReportServImpl service = new ClimoDiagnosticReportServImpl("a,b,c,d");
        assertFalse(service.getSomeResult());
    }
}

暫無
暫無

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

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