I am trying to create a JUnit test case for application's repository class.
Repository is as follows:
@Repository
public interface AddressRepo extends JpaRepository<SourceAddress, int>, JpaSpecificationExecutor<SourceAddress> {
@Query(value = "select * from Address ", nativeQuery = true)
List<Address> getAdressResults();
}
Entity class is as follows:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "ADDRESS")
public class Address implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
private int id;
@Column(name = "line1")
private String line1;
@Column(name = "line2")
private String line2;
}
I have tried the following but it return null pointer exception stating that repository is null:
@Mock
Repository repository;
@Test
public void testRepo(){
Address address = new Address();
address.setid(1));
address.setLine1("address line 1");
List<Address> addressList = new ArrayList<>();
addressList.add(address);
Mockito.when(repository.getAddressResults()).thenReturn(addressList);
List<Address> addresses = repository.getAddressResults();
assertThat(addresses.get(0).getLine1().equalsIgnoreCase("address line 1"));
}
How can I test this Repository?
Thanks