[英]initialise a service for mocking in java with mockito and junit 5
I'm trying to learn some integration testing whilst creating a project to make a game database, and have got a class that calls a service within it's methods我正在尝试学习一些集成测试,同时创建一个项目来制作游戏数据库,并且有一个 class 在其方法中调用服务
public DigitalGame findDigitalGameByTitleAndPlatform(DigitalGame digitalGame){
if(digitalGame == null){
return null;
}
return GamesJPAService.iDigitalGameRepository.findDigitalGameByTitleAndPlatform(digitalGame.getTitle(), digitalGame.getPlatform());
}
The GamesJpaService as shown below, is initialised as an @Service when the project is started as an application.当项目作为应用程序启动时,如下所示的 GamesJpaService 被初始化为@Service。 But is not initialised within the test class, no matter whether it is autowired,newed up, or any other method I have spotted to try and initialise it.但未在测试 class 中初始化,无论它是自动装配、更新还是我发现的任何其他尝试初始化它的方法。 for example using the @InjectMocks annotation例如使用@InjectMocks 注释
@Service
public class GamesJPAService implements IJpaServices {
public static IDigitalGameRepository iDigitalGameRepository;
@Autowired
private IDigitalGameRepository iDigitalGameRepo;
@PostConstruct
public void init(){
iDigitalGameRepository = this.iDigitalGameRepo;
}
}
The IDigitalGameRepository that it calls looks like:它调用的 IDigitalGameRepository 如下所示:
public interface IDigitalGameRepository extends JpaRepository<DigitalGame, String> {
DigitalGame findDigitalGameByTitleAndPlatform(String title, Platform platform);
}
I have ommited some methods from the proceeding code.我从前面的代码中省略了一些方法。 My test class also looks like (at the moment)我的测试 class 也看起来像(此刻)
@DisplayName("Digital Games Dao Test")
class DigitalGamesDaoTest {
Set<Peripheral> compatiblePeriphs = new HashSet<>();
Set<SupportedLanguages> supportedLanguages = new HashSet<>();
List<Mods> mods = new ArrayList<>();
List<DLC> dlc = new ArrayList<>();
DigitalGame olliolli = new DigitalGame("olliolli", "", "sports", "olliolli", new ReleaseDate(), new Platform(), compatiblePeriphs, new Developer(), new Publisher(), new Region(), supportedLanguages, new NoneEnglish(), mods, dlc, "", new SpecialEdition(), true, new Platform());
private IDigitalGamesDao digitalGamesDao;
@Mock
GamesJPAService gamesJpaService;
@BeforeEach
void setUp() {
digitalGamesDao = new DigitalGamesDao();
}
@Test
@DisplayName("insertDigitalGame should return false when digital game already in database")
public void insertDigitalGameShouldReturnFalseWhenDigitalGameInDatabase(){
EntityManager mockEntityManager = mock(EntityManager.class);
when(GamesJPAService.iDigitalGameRepository.findDigitalGameByTitleAndPlatform(olliolli.getTitle(), olliolli.getPlatform())).thenReturn(olliolli);
doReturn(olliolli).when(digitalGamesDao.findDigitalGameByTitleAndPlatform(olliolli));
when(digitalGamesDao.findDigitalGameByTitleAndPlatform(olliolli)).thenReturn(olliolli);
when(mockEntityManager.merge(olliolli)).thenReturn(null);
assertFalse(digitalGamesDao.insertDigitalGame(olliolli));
}
@Test
@DisplayName("insertDigitalGame should return true when digital game added to database")
public void insertDigitalGameShouldReturnTrueWhenDigitalGameNotInDatabase(){
}
and I am getting a null pointer exception,我得到一个 null 指针异常,
java.lang.NullPointerException: Cannot invoke "com.me.gamedatabasebackend.dao.gamesJPA.IDigitalGameRepository.findDigitalGameByTitleAndPlatform(String, com.me.gamedatabasebackend.model.platform.Platform)" because "com.me.gamedatabasebackend.dao.gamesJPA.GamesJPAService.iDigitalGameRepository" is null java.lang.NullPointerException:无法调用“com.me.gamedatabasebackend.dao.gamesJPA.IDigitalGameRepository.findDigitalGameByTitleAndPlatform(字符串,com.me.gamedatabasebackend.model.platformgamebasedPA.Platform)。”因为“com.platform.gamedatabasedpa.Platform.data” GamesJPAService.iDigitalGameRepository”是 null
As GamesJPAService is not being tested I need to know how to skip it and just return the value.由于 GamesJPAService 没有经过测试,我需要知道如何跳过它并只返回值。 So I need help to find a way for either, doing a component scan from my test class, or importing the GamesJPAService into it in a useable manner.所以我需要帮助找到一种方法,从我的测试 class 进行组件扫描,或者以可用的方式将 GamesJPAService 导入其中。
You need to annotate your test class with some think like that to make injection works你需要用一些这样的想法来注释你的测试 class 以使注入工作
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
DisplayName("Digital Games Dao Test")
class DigitalGamesDaoTest {
...
But you have a nullpointer because GamesJPAService.iDigitalGameRepository is null. That's a static property and the instance of GamesJPAService is a mock so the init() method is never call.但是你有一个空指针,因为 GamesJPAService.iDigitalGameRepository 是 null。这是一个 static 属性,GamesJPAService 的实例是一个模拟,因此永远不会调用 init() 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.