Git Product home page Git Product logo

Comments (6)

linfan avatar linfan commented on May 12, 2024

可以为空,我在Demo里加了访问无参数方法的示例,可作为参考
https://github.com/alibaba/testable-mock/blob/master/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoPrivateAccessTest.java

如果可以的话,不妨将出错的代码脱敏提供一下

from testable-mock.

hedaoming avatar hedaoming commented on May 12, 2024
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringCloudApplication.class)
@ActiveProfiles(profiles = "dev")
public class DependentServiceTest {

    @Autowired
    private DependentService dependentService;

 
    @Test
    public void testPrivateMethod() {
        //boolean result = PrivateAccessor.invoke(dependentService, "userHadJoin", "111"); // 没问题
        boolean result = PrivateAccessor.invoke(dependentService, "userHadJoin", null); // 空指针 Failed to invoke private method "userHadJoin": java.lang.NullPointerException
        //boolean result = PrivateAccessor.invoke(dependentService, "userHadJoin", "111", null);// 空指针 Failed to invoke private method "userHadJoin": java.lang.NullPointerException
        System.out.println(result);
    }
}


@Component
public class DependentService {

    public Integer submitGuess(String userId) {
        System.out.println("substring():" + userId.substring(0, 2));
        System.out.println("length():" + userId.length());
        // 查询DB
        if (userHadJoin(userId)) {
            return 0;
        }
        insertJoin(userId);
        return 1;
    }

    private Integer insertJoin(String userId) {
        //插入数据
        return 1;
    }

    private boolean userHadJoin(String userId) {
        // 查询DB
        return false;
    }

    private boolean userHadJoin(String userId, Object other) {
        // 查询DB
        return false;
    }
}

完整代码如上, 任一个参数传null时,都会导致空指针, 有些业务场景会传参数为null的情况
PrivateAccessor.invoke(dependentService, "userHadJoin", "111", null);

from testable-mock.

linfan avatar linfan commented on May 12, 2024

@hedaoming 抱歉过了几天才发现你的回复。
包含null参数报NullPointerException的情况确实是个BUG,会尽快修复🌷

from testable-mock.

linfan avatar linfan commented on May 12, 2024

已在0.4.10版本修复

from testable-mock.

murmuru avatar murmuru commented on May 12, 2024

您好,我使用的0.6.6的版本,这个问题依然会出现,对私有方法的调用入参有null值情况会有NPE.

from testable-mock.

linfan avatar linfan commented on May 12, 2024

最新的版本是0.7.5,试一下新版本?

from testable-mock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.