TDD就是测试驱动开发,以测试用例为主导,去开发项目,业务代码该怎么写还是怎么写,在实现UI之前,可以先实现Test用例,通过test来实现对业务场景的模拟,最终让你的代码更稳定。
在springboot框架里,我们在建立一个项目后,会同时建立一个测试项目,大叔觉得,这是一个很好的设计和习惯,对于.net开发人员来说,也应该这样,在建立一个项目后,同时也写一下它的单元测试!
public class MongodbTest {
@Autowired
MongoTemplate mongoTemplate;
@Test
public void add() {
List<TaxAmountConfig.AmountConfig> amountConfigs = ImmutableList.of(
TaxAmountConfig.AmountConfig.builder()
.rate(1)
.thresholdMax(5000)
.thresholdMin(3000)
.value(10)
.build()
);
TaxAmountConfig taxAmountConfig = TaxAmountConfig.builder()
.base(3000)
.accountPeriod(YearMonth.of(2017, 5))
.amountConfigs(amountConfigs)
.build();
mongoTemplate.insert(taxAmountConfig);
}
}
你的测试代码完全可以指定一种环境,如开发,生产,测试等,对哪个环境有效,可以通过@Profile来实现
@Configuration
@Profile("integTest")
public class MockClientTest {
@Bean
public MockClient mockClient() {
MockClient client = mock(MockClient.class);
when(client.balanceSheet(
anyString()))
.thenReturn("OK");
return client;
}
}