I am familiar with the basic principles of TDD:
- Write down the tests; they will not be executed due to lack of implementation.
- Write a basic implementation for passing the tests.
- Refactoring code
However, I got a little confused as to where the interfaces and implementation fit. I am creating a Spring web application in my free time, and instead of blazing cannons, I would like to understand how I can better test interfaces / implementations, take this simple code example that I created here
public class RunMe { public static void main(String[] args) {
I created the UserService
interface, in the end, there will be a real implementation of this that will query the database, however, in order for the application to be disconnected, I replaced the DummyUserService
implementation, which will just return some static data.
Question: How can I implement a testing strategy for the above?
I could create a test class called DummyUserServiceTest
and verify that when getUserById()
called it will return James
, it seems pretty simple if not a waste of time (?).
Subsequently, I could also create a test class RealUserService
that will verify that getUserById()
returns the username from the database. This is the part that confuses me a little, does it essentially not exceed the border of a unit test and become a more complex test (with a hit on the database)?
Question (slightly improved): when using interfaces with dummy / encoded and real implementations, which parts should be checked per unit and which parts can be left unchecked?
I spent several hours in this thread on this topic last night and basically found either tutorials on what TDD is, or examples of using JUnit, but nothing in the area of ββadvising on what really needs to be tested. It is possible, however, that I was not looking hard enough or not looking for the right thing ...