I’m trying to figure out how to test my code in a proper way and stuck somewhere in the middle.
What I use:
Spring-boot based Java web-application. Java based configuration. Test.
What I want:
I want to manage my testing in a proper way. I have a @RestController
annotated class what has two objects what I’d like to change in tests. It’s an entities provider class BooksService
and database configuration class DBConfig
. I want to change implementations for these classes in my tests in a simple and proper way. I don’t know how to do it correctly.
What kind of help I need:
- Working project that implements this approach is most preferable. Don’t spend too many words, the working code is the best way to understand it for me.
- If there is no a complete project there, so code snippets with descriptions will be useful too.
- Links to some good and simple tutorials about 1) Java based configurations 2) Replacing Java based configuration for tests.
Sorry if my thoughts are little bit messy, I’m pretty new in Java EE development and still didn’t figure out with all basic topics well.
EDIT:
Here is some code
DemoApplication
class
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
BooksController
controller class
@RestController
public class BooksController {
@Autowired
BooksProvider booksProvider;
@CrossOrigin
@RequestMapping("/books")
String allBooks() throws Exception {
return this.booksProvider.fetchAllBooksAsTring();
}
}
BooksProvider
service class
public class BooksProvider {
public String fetchAllBooksAsTring() {
return "[\"Marting Iden\", \"Capital\", \"Strong Wind\"]";
}
}
SimpleConfiguration
configuration class
@Configuration
public class SimpleConfiguration {
@Bean
public BooksProvider booksProvider() {
return new BooksProvider();
}
}