Jdbc Test

import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;

@RunWith(SpringRunner.class)
@JdbcTest  //annotated for JDBC test
@AutoConfigureTestDatabase(replace=Replace.NONE)  // used configuration DB server
												  // The original one is embedded DB
@Import(SomeDao.class)  //Tested DAO class
public class SomeDaoTest {

	@Autowired
	private SomeDao dao; // auto inject
	
	@Test
	public void testFindOneOrgin() {
		assertTrue("Sandbox".equals(dao.findOneOrgin("A077").orElse("")));
	}
	
	@Test
	public void testInsertOne() {
		assertTrue(dao.insertVender("A006", "1000")==1);
	}
	
	@Test
	public void testFindVender() {
		testInsertOne();
		assertThat(dao.findVender(10))
		.isInstanceOf(Vender.class)
		.hasFieldOrPropertyWithValue("id", 10);
	}
}

Service Test

@RunWith(SpringRunner.class)
@SpringBootTest(classes= {ValidationAutoConfiguration.class})
public class NothingServiceTest {

	@Autowired
	private NothingService service;
	
	@Test
	public void testDoSomeThing() {
		service.doSomeThing("cadfdffbbdfbn");
	}


    @Configuration	
	@ComponentScan(useDefaultFilters=false,includeFilters= {@Filter(type = FilterType.ASSIGNABLE_TYPE,
	        classes = NothingService.class)})
    static class {
    
    }
}

Json Test

import static org.assertj.core.api.Assertions.*;

@RunWith(SpringRunner.class)
@JsonTest
public class MyObjectTest {
	
	@Autowired
	private JacksonTester<MyObject> json;
	private MyObject obj;
	
	@Before
	public void setUp() throws Exception {
		obj=MyObject.builder()
				.id(99)
				.message("This is the message.")
				.iteme(Child.builder().item("item001").price(203.4F).build())	
				.iteme(Child.builder().item("item002").price(100F).build())							
				.build();
	}
	
	@Test
	public void testSerialize() throws IOException {			
		JsonContentAssert jca=assertThat(json.write(obj));
		jca.extractingJsonPathNumberValue("@.ID").isEqualTo(99);
		jca.extractingJsonPathStringValue("@.MSG").isEqualTo("This is the message.");
		jca.extractingJsonPathArrayValue("@.LIST").asList().hasSize(2);
	
	}

	@Test
	public void testSerialize() throws IOException {	
		log.trace(json.write(obj).getJson()); // print JSON string
		assertThat(json.write(obj)) //object to JSON
		.isEqualToJson("Child.json"); //compare with JSON file
	}

	@Test
	public void testDeserialize() throws IOException {
		ObjectContentAssert<MyObject> ca=assertThat(json.read("MyObject.json"));
		ca.isEqualToComparingFieldByField(obj);
	}

}

MVC Test

import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;

@RunWith(SpringRunner.class) //  extension of JUnit's for spring test
@WebMvcTest(OneController.class) // controller for test  
@Log4j2
@AutoConfigureRestDocs
public class OneControllerTest {

	@Autowired
	private MockMvc mvc; // mock for MVC test
	
	@Autowired
	private ObjectMapper objectMapper; 
	
	@Test
	public void testTest() throws Exception {
		mvc.perform(get("/one/test")) // make the request
		.andExpect(content().string("AAA")) // expect result
		.andDo(document("{ClassName}/{methodName}")); // generate snippet for DOC
	}

	@Test
	public void testValid() throws Exception {
	
		MyObject obj=MyObject.builder().id(99).message("This is MSG.")
				.iteme(Child.builder().item("item001").build())
				.build();
		
		mvc.perform(post("/one/obj/{ID}",20) //path variable
				.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE) // media type
				.param("name", "ABC") // URL  parameter
				.content(objectMapper.writeValueAsString(obj)) // JSON for HTTP body
				).andExpect(status().isOk())
		.andDo(print());
	}
	
	
	@Test
	public void testFindOne() throws Exception {
		
		MyObject obj=MyObject.builder().id(99).message("This is MSG.").build();
		
		mvc.perform(post("/one/obj/{ID}",20) //path variable
				.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE) // media type
				.param("name", "ABC") // URL  parameter
				.content(objectMapper.writeValueAsString(obj)) // JSON for HTTP body
				).andExpect(status().isOk()) // expect http 200 status
				.andDo(document("{ClassName}/{methodName}")); // generate snippet for DOC
	}
}

Exclude some class at SpringBootApplication

@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeTest {

	@SpringBootApplication(scanBasePackages="xxx.xxx.package")
	@EnableFeignClients
	@ComponentScan(	excludeFilters= {@Filter(type=FilterType.ASSIGNABLE_TYPE,value=StartApp.class),
			@Filter(type=FilterType.ASSIGNABLE_TYPE,value=Some.class)})
	static class FillterConfig{
		
	}
    
}