I have a BookManagementService that uses the @Autowired implementation of three other services, like so So how do I mock the aforementioned services and their repo in the BookManagementServiceTest? When the test start running and it gets to the yearService layer, it throws a NullPointerEXception cause the year it receives is null The BookManagementServiceTest Answer You just have to mock
Tag: testing
Accidentally deleted my test folder in Spring boot application
As the title says I accidentally deleted my test folder in Spring boot application and created a folder named test under src folder. But now I can’t create java class in test folder.IDE does not recognize it as a java class. What can I do to fix this? Answer Create the java in the test directory. If you use IDEA,
Validation failed for query in tests but not in production
Problem description I have the following test class with a few tests. The setUp() method executes insert statements from a file. The first test simply makes a post request and expects an array of JSON objects in return with a 200(OK) status: Upon running the first test I get the following error: What I Tried Spring obviously does not like
Use a secondary h2 database for testing an API in Spring boot
I’m developing an API to manage a database in the company where I work, the problem is that when I have to run the different test I have to use the “real” dev database (in h2) where I have some real data. I thought about it and what I wanted to do is create a new h2 database that could
How to make an integration test with one transaction for all database calls and rollback it afterwards?
I’m, writing an integration test, annotated with @SpringBootTest. Suppose I create some product using pure SQL from my test, call calculation service that do some calculations based on it, and it calls another service to save those calculation results in different tables. What I need is to rollback all the changes made after test is finished. I read different questions,
How to test Service method with ModelMapper call
I’m writing some Unit Tests for my Service class, specifically, an update method that does exactly that, update an Entity with the given data from a request. The problem is, I’m using ModelMapper to map the request data to the entity and when the test goes through the mapping statement it doesn’t actually call the modelMapper but the mock ….
How to re run suite without using listener in testng
I had a requirement to re-run a complete suite from the TestNG test method itself if any of the test-case from the suite failed. Is there any way to call complete suite using an XML file or Test class within the Test method? The complete suite should re-run the after-class method or teardown test-case or last test-case @AfterClass or @AfterMethod
How to mock LocalDateTime.now() in java 8
Please, I am testing a functionality ABC that uses LocalDateTime.now(). In the methode ABC i’am comparing an entry date with the LocalDateTime.now() I want my test to be passed at any day so i have to …
How to ignore a method call inside a method that is being tested?
I am trying to make a test pass with mockmvc and it is failing with the following error message: Caused by: org.apache.kafka.common.config.ConfigException We have Kafka in our service layer as a …
Best practice for Unit Testing class which is mostly responsible to call methods of dependencies, but contains logic as well
Let’s assume I have StartCommandHandler which has responsibility to create some file with required files. But for doing this I have to give him a set of sub-responsibilities, like: Checks whether …