doctor who

1、We Should Not Use Inheritance In Our Tests 测试代码中不要使用继承。

2、we must configure our test cases in a clean and simple way.

We can improve the readability of our configuration by configuring all test cases in the test class. This means that we have to:

Add the required annotations (such as @RunWith) to the test class.Add the setup and teardown methods to the test class.If we modify our example test class by following these rules, its source code looks as follows:

3、Divide and ConquerDivide and Conquerimport org.junit.Before;import org.junit.runner.RunWith;import org.mockito.Mockito;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = {WebUnitTestContext.class})@WebAppConfigurationpublic class TodoControllerTest {private MockMvc mockMvc;@Autowiredprivate TodoService serviceMock;@Autowiredprivate WebApplicationContext webAppContext;@Beforepublic void setUp() {Mockito.reset(serviceMock);mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();}//Add test methods here}

In my opinion, the new configuration of our test cases looks a lot simpler and cleaner than the old configuration which was divided intoTodoControllerTest and AbstractControllerTest classes.

Unfortunately, nothing is free.

4、 Naming Matters

why we should pay attention to naming and describes a few useful naming conventions which will help us to write clean tests.

5、Beware of Magic

We must give good names to constants and constants classes. If we don’t do that, we aren’t leveraging the full potential of these techniques.We shouldn’t introduce new constants without figuring out what we want to achieve with that constant. The reality is often a lot more complex than the examples of this blog post. If we write code on autopilot, the odds are that we will miss the best solution to the problem at hand.6、it is a bad idea to create objects in the test method by using the new keywordIt is a bad idea to create the required objects in the test method by using thenew keyword because it makes our tests messy and hard to read.If we have to set only a handful of property values and our test data doesn’t have a lot of variation, we should create the required object by using a factory method.If we have to set a lot of property values and / or our test data has a lot of variation, we should create the required object by using a test data builder.7、Replace Assertions with a Domain-Specific Language8、Divide and ConquerWe learned that we can identify the logical concepts covered by a single unit test by identifying the situations when that test will fail.We learned that writing unit tests which test only one logical concept helps us to write transform our test cases into executable specifications, which identity the requirements of the tested method / component.

9、Mock external dependencies (and state)

10、Small Is Beautiful

具体介绍:9、

,,再回头,便生出无限羁绊。那是彼此的刺在对方心里留下的痕迹,

doctor who

相关文章:

你感兴趣的文章:

标签云: