Home > Net >  Mockito returns a list of null elements except of the first, although I've initialized every el
Mockito returns a list of null elements except of the first, although I've initialized every el

Time:11-16

I wanted to check if mock JPA repository will return all the elements. I've created 3 entities and initialized them, then created list and put these models into this list. After that, wrote statment:

when(pasteboxJPARepository.findAll()).thenReturn(entities);

And it seems, that after calling findAll() method, I'll get list of entities. Yes, I get it, but all the elements're null except of the first, don't know what it can be(the console screen at the end).

Test class

@SpringBootTest
public class PasteboxServiceTests {

@Autowired
PasteboxService pasteboxService;
@MockBean
PasteboxJPARepository pasteboxJPARepository;

@Test
void testFindAll(){

    LocalDateTime now = LocalDateTime.now();
    LocalDateTime expTime = now.plusSeconds(45);
    LocalDateTime expTime1 = now.plusSeconds(40);
    LocalDateTime expTime2 = now.plusSeconds(35);

    PasteboxEntity entity = new PasteboxEntity();
    entity.setId(0L);
    entity.setHash("hash");
    entity.setData("Here's my text #1.");
    entity.setLifeTime(expTime);
    entity.setPublic(true);

    PasteboxEntity entity1 = new PasteboxEntity();
    entity.setId(1L);
    entity.setHash("hash1");
    entity.setData("Here's my text #2.");
    entity.setLifeTime(expTime1);
    entity.setPublic(true);

    PasteboxEntity entity2 = new PasteboxEntity();
    entity.setId(2L);
    entity.setHash("hash2");
    entity.setData("Here's my text #3.");
    entity.setLifeTime(expTime2);
    entity.setPublic(true);

    List<PasteboxEntity> entities = new ArrayList<>();
    entities.add(entity);
    entities.add(entity1);
    entities.add(entity2);

    when(pasteboxJPARepository.findAll()).thenReturn(entities);

    assertEquals(3, pasteboxService.testMethodEntity().size());

}
}

Entity class:

@Data
@Entity
@Table(name = "paste_box_entities")
public class PasteboxEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @Column(name = "data")
    private String data;
    @Column(name = "hash")
    private String hash;
    @Column(name = "lifetime")
    private LocalDateTime lifeTime;
    @Column(name = "ispublic")
    private boolean isPublic;
}

Service class(here I wrote output of all the elements, to see what's going on there)

@Service
@RequiredArgsConstructor
public class PasteboxServiceImpl implements PasteboxService {


    private final PasteboxJPARepository pasteboxJPARepository;

public List<PasteboxEntity> testMethodEntity(){
    
    for(PasteboxEntity e : pasteboxJPARepository.findAll())
        System.out.println(e);
    
    return pasteboxJPARepository.findAll();

}
}

Repository:

@Repository
public interface PasteboxJPARepository extends JpaRepository<PasteboxEntity, Long> {
    PasteboxEntity findByHash(String hash);
    List<PasteboxEntity> findAll();
}

And in the console, we can see that, only the first element's not null, although I've initialized everyone.

enter image description here

I've tried to find same in the internet, but it seems nothing, so I'm here.

CodePudding user response:

You are setting only the fields of entity, not entity1 and entity2 in your test…

  • Related