![]() |
|
[Java] Spring Boot Unit Testing - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Spring Boot Unit Testing (/showthread.php?tid=5758724) |
Spring Boot Unit Testing - SIM-1947090 - Nov 26, 2017 Hey, I am pretty much new to this JUnit testing thing :/ I tried a whole lot different methods, and none seemed to work. Specially reading this docs: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html I either suck at reading docs, or this one is not really complete or, I am just too stupid. So help me out :/ Im trying to test my services for now, and then try some other cool features in this area. But my tests just do not pass :O This is the service Im trying to test: https://pastebin.com/C4VWMQFj This is the service test: https://pastebin.com/H0LFq4Pe This is Spring Boot application: Code @SpringBootApplicationThis is config: Code @ConfigurationAs you can see from the pastebin, userRepository.save(user) returns null (assertion fails obviously) for some reason. What am I doing wrong? RE: Spring Boot Unit Testing - Dialog - Nov 26, 2017 How are you handling dependency injection? You should have a separate layer for your testing environment here, and use mockito for mocking objects. RE: Spring Boot Unit Testing - SIM-1947090 - Nov 26, 2017 (Nov 26, 2017 03:50 PM)Dialog Wrote: How are you handling dependency injection? You should have a separate layer for your testing environment here, and use mockito for mocking objects. Hmm, isn't this supposed to do the job, all the required connections, mocking and stuff: - inside of UserService (userRepository is autowired) Code @Autowiredand - insde of test UserService (both are MockBean-ed) - shouldnt this make a valid connection between two mocked objects? Code @MockBeanJust one more thing to note: I added this one and it's passed: verify(userService, times(1)).register(user); So, how can that line pass but still return null from userService.register(user) UPDATE: Maybe I got it. I had to manually create Mocks and Inject those. MockBean annotation doesnt seem to do that job :O (I thought it would) Looks like this get the job done (but Im not sure why @MockBean didnt work well): Code @InjectMocksRE: Spring Boot Unit Testing - ZeratulTheWizard - Nov 26, 2017 (Nov 26, 2017 04:02 PM)H1gh-T3ch Wrote: Hmm, isn't this supposed to do the job, all the required connections, mocking and stuff: If you're using JunitRunner or MockitoRunner as the RunWith, that might be an issue. You might need to use SpringRunner if you're using Spring annotations. RE: Spring Boot Unit Testing - SIM-1947090 - Nov 27, 2017 (Nov 26, 2017 05:44 PM)ZeratulTheWizard Wrote: If you're using JunitRunner or MockitoRunner as the RunWith, that might be an issue. You might need to use SpringRunner if you're using Spring annotations. Yes, I am using @RunWith(SpringRunner.class). And, with the latest updates, I can confirm that @MockBean actually does the same job, I dont need to manually setup mocks with @InjectMocks, @Mock and MockitoAnnotations.initMocks(this). But it appears that third-party library is causing some issues, which is Thymeleaf template engine. It for some reason fails to configure itself in tests, hence my tests fail big time (I probably need to manually configure it for tests or something). So I decided to not waste my time and use @InjectMocks, @Mock and MockitoAnnotations.initMocks(this) instead and continue along. |