0

私は Mockito を初めて使用し、プロパティがappication.properties ファイルからロードされないために問題に直面しています。

問題文: 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.getPartsList(ClimoDiagnosticReportServImpl.java:68) で java.lang.NullPointerException com.test.serviceimpl.ClimoDiagnosticReportServImpl.getSomeResult(ClimoDiagnosticReportServImpl.java:57) でClimoDiagnosticReportServImplTest.java:74) で sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) で sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) で sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) で Java でorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) の .lang.reflect.Method.invoke(Method.java:498) org.junit.internal.runners.model.ReflectiveCallable.run( ReflectiveCallable。java:12) org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) で org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) で org.junit.internal .runners.statements.RunBefores.evaluate(RunBefores.java:26) で org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) で 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(ParentRunner.java: 71) org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) で org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) で org.junit.runners.ParentRunner$2.org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) で org.mockito.runners で org.junit.runners.ParentRunner.run(ParentRunner.java:363) で評価 (ParentRunner.java:268) org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) の .MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) org.eclipse.jdt.internal.junit.runner.TestExecution.run の(TestExecution.java:38) org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) で org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java) で:678) org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) で org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

よろしくお願いします。

4

1 に答える 1

0

サービスでモックする依存関係はありません。したがって、Mockito は完全に不要です。あなたがする必要があるのは、リフレクションを使用してSpringによってアプリケーションに入力されるプライベート文字列フィールドを設定することです。

フィールド インジェクションの代わりにコンストラクター インジェクションを使用するベスト プラクティス os に従うだけで、コードがテスト可能になります (これがベスト プラクティスである理由の 1 つです)。

@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());
    }
}
于 2018-12-29T14:11:30.723 に答える