Unit Testing in Spring Boot
How to write end to end test using Spring Boot.
Key Annotation
- @SpringBootTest
- @LocalServerPort
- @Test
package tbp.unit.testing.mokito;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.junit4.SpringRunner;
import tbp.unit.testing.mokito.dao.UserDao;
import tbp.unit.testing.mokito.service.UserService;
/**
* @author rajan
*
*/
@RunWith(SpringRunner.class)
public class UnitTestingWithMockito {
@InjectMocks
UserService service;
@Mock
UserDao dao;
@Test
public void getUserIdTest() {
when(dao.getUserId("Rajanikanta")).thenReturn("123445Rajanikanta");
String id = service.getUseruserIdByName("Rajanikanta");
assertEquals("123445Rajanikanta", id);
}
}
- Clone this repository
- Import in to IDE
- Do a maven clean and build
- Clone this repo to your local machine.
Do maven update
Run the junit
To get started...
-
Option 1
- 🍴 Fork this repo!
-
Option 2
- 👯 Clone this repo to your local machine.
- HACK AWAY! 🔨🔨🔨
- 🔃 Create a new pull request .
- What is @SpringBootTest ?
- This annotation will load the context from main app.
Reach out to me If any issue !
- FaceBook at
Rajanikanta