<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
@SpringBootTest
@RunWith(SpringRunner.class)
public class ExampleTest {
@Autowired
private ExampleService exampleService;
@Test
public void testExample() {
String result = exampleService.exampleMethod("input");
assertEquals("expected output", result);
}
}
@Controller
public class ExampleController {
@RequestMapping("/example")
public String testExample() {
return "expected output";
}
}
@WebMvcTest(controllers = ExampleController.class)
public class ExampleControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testExample() throws Exception {
mockMvc.perform(get("/example"))
.andExpect(status().isOk())
.andExpect(content().string("expected output"));
}
}
@DataJpaTest
public class ExampleRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private ExampleRepository exampleRepository;
@Test
public void testExample() {
ExampleEntity entity = new ExampleEntity();
entity.setName("test");
entityManager.persist(entity);
ExampleEntity result = exampleRepository.findByName("test");
assertEquals(entity, result);
}
}
@SpringBootTest(classes = ExampleApplication.class)
@RunWith(SpringRunner.class)
public class ExampleTest {
@Autowired
private ExampleService exampleService;
@Test
public void testExample() {
String result = exampleService.exampleMethod("input");
assertEquals("expected output", result);
}
}
@SpringBootTest
@TestPropertySource(properties = "app.message=test")
@RunWith(SpringRunner.class)
public class ExampleTest {
@Autowired
private ExampleService exampleService;
@Test
public void testExample() {
String result = exampleService.exampleMethod("input");
assertEquals("test", result);
}
}
@SpringBootTest
@TestPropertySource(locations = "classpath:test.properties")
@RunWith(SpringRunner.class)
public class ExampleTest {
@Autowired
private ExampleService exampleService;
@Test
public void testExample() {
String result = exampleService.exampleMethod("input");
assertEquals("test", result);
}
}