mackesmilian
mackesmilian

Reputation: 123

How do I retrieve an object from a database in spring boot for testing?

I am not very experienced with unit or integration tests. I have the following scenario: I simply want to get an object from my database to use for testing. The object will not be updated, I'm just too lazy to create it manually because it is a fairly long class and it would be quite cumbersome.

My structure for getting something from my database is a Service class which calls a repository interface. Right now my issue is that the repository is null when I run the test.

Using @DataJpaTest and @AutoConfigureTestDatabase(replace = Replace.NONE) I could get the connection to my db working, however the repository in the service class is null.

I tried mocking the repository interface and injecting it into the service class but then the object I get back is null which defeats the purpose, obviously.

I'd be glad if you could also link me some tutorial, because I couldn't find any which were relevant to my case. All other explanations described how to test queries themselves, which I don't want. I just need an object.

I apologize if I'm missing something glaringly obvious, I really am inexperienced when it comes to testing.

This is what I have:

myService:

@Service
@Transactional(readOnly = true)
public class MyService{

    @Autowired
    private MyRepository repo;

    public List<MyObj> findAll() {
    return repo.findAll();
    }

    public MyObj findMyObjByCode(String code) {
    return repo.findMyObjByCode(code);
    }
}

Test:

@ActiveProfiles("test")
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
class MyObjTest{
    
    @Test
    void factoryTest() {
    MyService service = new MyService ();
    MyObj obj = service.findMyObjByCode("code");
    MyDto dto = MyDto.from(obj);
    //assertions
    }
}

Upvotes: 0

Views: 1580

Answers (2)

berlin
berlin

Reputation: 526

When unit-testing, One does not need to deal with real data at all. Answering this question by giving you a way to read from the db would totally miss the point. If one is simply testing if an object exists or not, if it holds specific properties or not, what does it matter where the object came from? One could create a test object that is alike to one from the DB or simply use Mocks from the Mockito framework. In the step where one is supposed to retrieve a real object from the DB, one could simply return the Mock object instead.

Having said that, if you'd still like to use the DB, here is a tutorial from reading data from a DB in java.

CRUD app tutorial - https://www.callicoder.com/spring-boot-jpa-hibernate-postgresql-restful-crud-api-example/

Here is one to work with mocks - https://www.baeldung.com/mockito-series

Upvotes: 1

Krzysztof K
Krzysztof K

Reputation: 784

Do you know that @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.NONE) this will configure your in memory database?

In order to connect to db you need to start spring context with db access properties in test resources for example with annotation @SpringBootTest. Also don't mock jparepository if you want to use that class (other way it will return null or mocked response). When using @SpringBootTest you want to @Autowire services. If you need to mock some service you just need to annotate it with @MockBean

Upvotes: 1

Related Questions