-1

Hi I wanna try to receive data from my repository but I can t retrieve nothing. I make autowired and on java class in: " src/main" it works but in the junit test no

I ve tried any method of Jpa repository but I receive always a "null" parameters

Junit Class

@RunWith(SpringJUnit4ClassRunner.class)
@DataJpaTest
class testClass {

    @Autowired
    private TestEntityManager entityManager;


    @Autowired
    private ClienteRepository clienteRepository;


    @Test
    public void test() {


       List<ClienteEntity> cliente = clienteRepository.findAll();
        System.out.println(cliente);

    }

Repository

@Repository
public interface ClienteRepository extends JpaRepository<ClienteEntity, Integer>{
    ClienteEntity findByEmail(@Param("email") String email);

Console with the system.out.printl in the console i have always this output:

    Hibernate: select clienteent0_.id as id1_0_, clienteent0_.cap as cap2_0_, clienteent0_.citta as citta3_0_, clienteent0_.cognome as cognome4_0_, clienteent0_.email as email5_0_, clienteent0_.indirizzo as indirizz6_0_, clienteent0_.nome as nome7_0_, clienteent0_.password as password8_0_, clienteent0_.token as token9_0_ from cliente clienteent0_
    []

    a empty list of ClientEntity
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
blackz7
  • 15
  • 3

3 Answers3

0

Based on @DataJpaTest docs:

If an embedded database is available on the classpath, it configures one as well

So double-check if there isn't any and follow this doc if needed:

If, however, you prefer to run tests against a real database you can use the @AutoConfigureTestDatabase

As your test may be using an embedded one based on the annotations of your entities and you are testing agains an empty DB.

Edit

If you look at the @AutoConfigureTestDatabase you can see for what properties the test will be looking at in terms of DB config:

@PropertyMapping(value="spring.test.database")
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
0

Depending on the situation, it may be necessary to check for other settings in the test scope.

In spring boot, if there is an embedded database, it is composed of that database in the test scope.

WonChul Heo
  • 242
  • 1
  • 12
  • In addition, if you want integration testing, it may be wise to use [`@SpringBootTest`](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications). – WonChul Heo Aug 05 '19 at 09:54
  • He wants to test only the repo level. He does not need the entire web server – Maciej Kowalski Aug 05 '19 at 10:23
  • hi, thanks for trying help me if i want use not a embedded DB? how can connect a real DB with test? i m trying with the same properties who i use on src/main – blackz7 Aug 05 '19 at 10:44
  • try `@AutoConfigureTestDatabase`. [doc](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test) – WonChul Heo Aug 05 '19 at 14:56
  • i resolved with : @AutoConfigureTestDatabase(replace = Replace.NONE) Thanks :D – blackz7 Aug 06 '19 at 10:27
0

Try using this annotations.

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {HCSRepositoryContext.class})
TanvirChowdhury
  • 2,498
  • 23
  • 28
  • hi thanks for trying help me, i ve tried but i have however the same problem java.lang.nullPointerException i think can t connect to my DB – blackz7 Aug 05 '19 at 10:48