0

I am a beginner in testing and now I have a problem that I cannot overcome.

@SpringBootTest
ExampleMakerSpec extends Specification {

@Autowired @Subject ExampleMaker exampleMaker
@Autowired 
ExampleRepository exampleRepository

def EXAMPLE_VARIABLE = "Example"

@Transactional
def "example() trying to do somehthing" () {      
    when: "trying to make some examples"
        Examples examples = exampleMaker.createExamples(examples)

    then: "get examples sizes and saves them to database"
        examples.size == 7

My Example maker looks like that:

@Component
public class ExampleMaker {

    @Autowired
    ExampleRepository exampleRepository;

    public void createExamples() {
        exampleRepository.save(Examples);

    }


}

And CRUD repository:

@Repository
public interface exampleRepository extends CrudRepository<Example, Long> {

}

but I am always getting

java.lang.NullPointerException at line exampleRepository.save(Examples).

So for some reason the tests cannot find the repository. I need assistance here to understand what is missing.

Jamel
  • 33
  • 1
  • 4

1 Answers1

-2

You need to autowire the exampleRepository in your test class. So when your test class ExampleMakerSpec is created, Spring looks for and injects ExampleRepository

Atika R
  • 27
  • 4