Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

mocking - Can't mock a class that having @RequiredArgsConstructor

I've been trying to mock a service class that has annotated with @RequiredArgsConstructor.

My Service class:

@Service
@RequiredArgsConstructor
public class MyService {
    
    private final CustomerService customerService;
    private final EmployeeService employeeService;
    
    @Autowired
    private Manager manager;
}

Test Class:

@Test
public class MyServiceTest {
    
    @Mock
    private CustomerService customerService;
    @Mock
    private EmployeeService employeeService;
        
    @InjectMocks
    private MyService myService;
    
    
    @BeforeTest
    public void beforeEach() throws Exception {
        MockitoAnnotations.initMocks(this);
    }
        
    @Test
    public void test() {
        String userId ="231";
        when(employeeService.get(userId)).thenReturn("1233");
        when(customerService.get(userId)).thenReturn("87889");
        myService.create(userId);
        //
    }
 }

When I run this test method, I'm getting the following exception. The execution never reaches the test method. It errors out just after finishing MockitoAnnotations.initMocks(this). I think it's because of @RequiredArgsConstructor.

java.lang.NullPointerException
    at org.testng.remote.strprotocol.TestResultMessage.<init>(TestResultMessage.java:129)
    at org.testng.remote.strprotocol.TestResultMessage.<init>(TestResultMessage.java:158)
    at org.testng.remote.strprotocol.RemoteTestListener1.onTestFailure(RemoteTestListener1.java:68)
    at org.testng.remote.strprotocol.RemoteTestListener1.onConfigurationFailure(RemoteTestListener1.java:98)
    at org.testng.internal.Invoker.runConfigurationListeners(Invoker.java:1841)
    at org.testng.internal.Invoker.handleConfigurationFailure(Invoker.java:333)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:236)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.TestRunner.beforeRun(TestRunner.java:637)
    at org.testng.TestRunner.run(TestRunner.java:605)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1062)
    at org.testng.TestNG.run(TestNG.java:974)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

What am I missing. Is this the right way to mock the service layer when it is annotated with @RequiredArgsConstructor. Can someone help me?

question from:https://stackoverflow.com/questions/65849670/cant-mock-a-class-that-having-requiredargsconstructor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...