春季的bean在mockito测试中为null。

huangapple 未分类评论47阅读模式
英文:

spring bean is null in mockito test

问题

以下是您提供的代码部分的中文翻译:

我正在使用 Spring 5 和 Mockito但在测试过程中我遇到了一个问题我的 **bean** 变成了 **null**然后我得到了一个 NullPointerException我该如何修复这个问题
实际上我的 SpringContext.getBean 返回 null但当应用程序处于运行时模式时它却可以正常工作

    java.lang.NullPointerException
    at com.novinkish.batch.ParallerMakerTest.testSendData(ParallerMakerTest.java:53)
    .
    .
    Caused by: java.lang.NullPointerException
    at com.novinkish.batch.util.SpringContext.getBean(SpringContext.java:14)

我的测试类如下所示

    @RunWith(MockitoJUnitRunner.class)
public class ParallerMakerTest {

    @Mock
    private CommonRestCallerImpl restCallerImpl;

    @Test
    public void testSendData(){

        PersonDTO personDTO_1 = new PersonDTO("1111111111", "name_1", "L_name_1", "2000/05/09", (short) 0);
        PersonDTO personDTO_2 = new PersonDTO(4646L, "1111111111", "name_1", "L_name_1", "2000/05/09", (short) 1);
        List<PersonDTO> resultDtoList = new ArrayList<PersonDTO>();

        try {
            when(restCallerImpl.callService(ServiceNameJNDI.SAVE_PERSON, personDTO_1, PersonDTO.class)).thenReturn(personDTO_2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ForkJoinPool pool = new ForkJoinPool();
        PersonSessionParallerMaker maker = new PersonSessionParallerMaker(restCallerImpl, Arrays.asList(personDTO_1));
        pool.execute(maker);
        try {
            pool.awaitTermination(3, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        resultDtoList = (List<PersonDTO>) pool.invoke(maker);
        Assert.assertNotNull(resultDtoList);
        Assert.assertEquals(1, ((PersonDTO)resultDtoList.get(0)).getStatus().intValue());

    }
}

而我的类如下所示

public class PersonSessionParallerMaker extends RecursiveTask {

    private CommonRestCaller restCaller;

    private List<PersonDTO> initList = new ArrayList<PersonDTO>();
    private List<PersonDTO> resultList = new ArrayList<PersonDTO>();

    private PersonDTO target;

    public PersonSessionParallerMaker(CommonRestCaller restCaller, List<PersonDTO> initList) {
        this.initList = initList;
        this.restCaller = restCaller;
    }

    public PersonSessionParallerMaker(PersonDTO target) {
        this.target = target;
    }

    @Override
    protected Object compute() {

        /*MASTER 线程*/
        if (target == null) {
            for (PersonDTO personDTO : initList) {

                /*创建 FORK(子线程)*/
                PersonSessionParallerMaker parallerMaker = new PersonSessionParallerMaker(personDTO);
                invokeAll(parallerMaker);
                resultList.add((PersonDTO) parallerMaker.join());
            }

            return resultList;

        } else if (target.getStatus() == 0) {
            callService();
            return target;
        } else
            return null;
    }

    public void callService() {

        System.out.println("1.restCaller = " + restCaller);
        /*对单元测试*/
        if (restCaller == null) {
            System.out.println("2.restCaller = " + restCaller);
            restCaller = (CommonRestCaller) SpringContext.getBean(CommonRestCallerImpl.class);
            System.out.println("3.restCaller = " + restCaller);
        }
        try {
            System.out.println("target.toString() = " + target.toString());
            target = (PersonDTO) restCaller.callService(ServiceNameJNDI.SAVE_PERSON, target, PersonDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
            target.setStatus((short) 2);
        }
    }
}

请注意,我只翻译了您提供的代码部分,不包括您的问题描述。

英文:

I'm using spring 5 and mockito, but in a test process i have a problem where my bean is done null and i get a NullPointerException, how can i fix it?
infact, my SpringContext.getBean returns null but it work correctly when the application is in runtime mode.

java.lang.NullPointerException
at com.novinkish.batch.ParallerMakerTest.testSendData(ParallerMakerTest.java:53)
.
.
Caused by: java.lang.NullPointerException
at com.novinkish.batch.util.SpringContext.getBean(SpringContext.java:14)

my test class is like this:

@RunWith(MockitoJUnitRunner.class)

public class ParallerMakerTest {

@Mock
private CommonRestCallerImpl restCallerImpl;

@Test
public void testSendData(){

    PersonDTO personDTO_1 = new PersonDTO(&quot;1111111111&quot;, &quot;name_1&quot;, &quot;L_name_1&quot;, &quot;2000/05/09&quot;, (short) 0);
    PersonDTO personDTO_2 = new PersonDTO(4646L, &quot;1111111111&quot;, &quot;name_1&quot;, &quot;L_name_1&quot;, &quot;2000/05/09&quot;, (short) 1);
    List&lt;PersonDTO&gt; resultDtoList = new ArrayList&lt;PersonDTO&gt;();

    try {
        when(restCallerImpl.callService(ServiceNameJNDI.SAVE_PERSON, personDTO_1, PersonDTO.class)).thenReturn(personDTO_2);
    } catch (Exception e) {
        e.printStackTrace();
    }

    ForkJoinPool pool = new ForkJoinPool();
    PersonSessionParallerMaker maker = new PersonSessionParallerMaker(restCallerImpl, Arrays.asList(personDTO_1));
    pool.execute(maker);
    try {
        pool.awaitTermination(3, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    resultDtoList = (List&lt;PersonDTO&gt;) pool.invoke(maker);
    Assert.assertNotNull(resultDtoList);
    Assert.assertEquals(1, ((PersonDTO)resultDtoList.get(0)).getStatus().intValue());

}

and my class is:

    public class PersonSessionParallerMaker extends RecursiveTask {

private CommonRestCaller restCaller;

private List&lt;PersonDTO&gt; initList = new ArrayList&lt;PersonDTO&gt;();
private List&lt;PersonDTO&gt; resultList = new ArrayList&lt;PersonDTO&gt;();

private PersonDTO target;

public PersonSessionParallerMaker(CommonRestCaller restCaller, List&lt;PersonDTO&gt; initList) {
    this.initList = initList;
    this.restCaller = restCaller;
}

public PersonSessionParallerMaker(PersonDTO target) {
    this.target = target;
}

@Override
protected Object compute() {

    /*MASTER Thread*/
    if (target == null) {
        for (PersonDTO personDTO : initList) {

            /*CREATE FORK (SUB THREAD)*/
            PersonSessionParallerMaker parallerMaker = new PersonSessionParallerMaker(personDTO);
            invokeAll(parallerMaker);
            resultList.add((PersonDTO) parallerMaker.join());
        }

        return resultList;

    } else if (target.getStatus() == 0) {
        callService();
        return target;
    } else
        return null;
}

public void callService() {

    System.out.println(&quot;1.restCaller = &quot; + restCaller);
    /*For Unit Test*/
    if (restCaller == null) {
        System.out.println(&quot;2.restCaller = &quot; + restCaller);
        restCaller = (CommonRestCaller) SpringContext.getBean(CommonRestCallerImpl.class);
        System.out.println(&quot;3.restCaller = &quot; + restCaller);
    }
    try {
        System.out.println(&quot;target.toString() = &quot; + target.toString());
        target = (PersonDTO) restCaller.callService(ServiceNameJNDI.SAVE_PERSON, target, PersonDTO.class);
    } catch (Exception e) {
        e.printStackTrace();
        target.setStatus((short) 2);
    }
}

}

答案1

得分: 0

你在这里混淆了单元测试和集成测试。

你所编写的是一个单元测试。单元测试根据定义是与整个应用程序隔离运行的,因此您无法访问应用程序上下文。单元测试的目的是测试一个特定的类

如果你想测试整个应用程序的行为,你需要编写一个集成测试。
请参阅 https://www.baeldung.com/spring-boot-testing#integration-testing-with-springboottest 获取更多信息。

英文:

You are mixing up unit testing and integration testing here.

What you have written, is a unit test. Unit tests run - per definiton - isolated from the whole application, therefore you have no access to the application context. The purpose of a unit test is to test one specific class.

If you want to test the behaviour of your whole application, you have to write an integration test.
See https://www.baeldung.com/spring-boot-testing#integration-testing-with-springboottest for further reading.

huangapple
  • 本文由 发表于 2020年7月27日 14:24:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/63109714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定