我在 Spring Boot 应用程序中也有一个 service 和 serviceImpl 。当我想测试它并尝试在 junit 测试类中模拟我的服务时,我得到了NullPointerException
错误。
这是我的服务 impl
package com.test;
import java.util.Date;
public class MyServiceImpl implements MyService {
@Override
public MyObject doSomething(Date date) {
return null;
}
}
这是我的测试课
package com.test;
import com.netflix.discovery.shared.Application;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertNull;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
class MyServiceImplTest {
@Mock
MyService myservice;
@Test
void doSomethingTest() {
assertNull(myservice.doSomething(null));
}
}