• Mockito测试 注解


    import static org.mockito.Mockito.*;
    import static org.junit.Assert.*;

    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.util.ReflectionTestUtils;

     @RunWith(MockitoJUnitRunner.class)

    @RunWith(SpringJUnit4ClassRunner.class)
    @PropertySource("test.properties")

    @ContextConfiguration(classes = {A.class, B.class})

    @RunWith(SpringRunner.class)
    @ActiveProfiles("test")

    xxx-test.properties

    单个文件

    @ContextConfiguration(Locations="../applicationContext.xml")

    @ContextConfiguration(classes = SimpleConfiguration.class)

    多个文件时,可用{}

    @ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })

    @ContextConfiguration(classes = {TestG10TempestContext.class, RatesTopologyContext.class})

    @EnableOAuth2Client

    @Configuration
    @PropertySource("test.properties")
    public class TestTempestContext {

    @Value("${xx.id}")
    private String id;


    @Bean
    public String str() {
    return "123";
    }

    }

    ReflectionTestUtils.setField(str, "name", "123");

    @EnableOAuth2Client

    package hello.hello_spock;
    
    import static org.junit.Assert.assertEquals;
    import static org.mockito.ArgumentMatchers.any;
    import static org.mockito.ArgumentMatchers.anyString;
    import static org.mockito.Mockito.mock;
    import static org.mockito.Mockito.when;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.junit.MockitoJUnitRunner;
    import org.springframework.http.HttpMethod;
    import org.springframework.web.client.RequestCallback;
    import org.springframework.web.client.ResponseExtractor;
    import org.springframework.web.client.RestTemplate;
    
    @RunWith(MockitoJUnitRunner.class)
    public class RestTemplateTest {
    
      @SuppressWarnings("unchecked")
      @Test
      public void testRestTemplate() {
        RestTemplate restTemplate = mock(RestTemplate.class);
        when(restTemplate.execute(anyString(),
            any(HttpMethod.class), any(RequestCallback.class), any(ResponseExtractor.class)))
                .thenReturn("123");
    
        String aString=restTemplate.execute("http://www.baidu.com", HttpMethod.GET, x->System.out.println("a"), x-> "" );
        assertEquals(aString, "123");
      }
    
    
    }
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>hello</groupId>
        <artifactId>hello_spock</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>hello_spock</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>2.13.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
        </dependencies>
    
    
    </project>
  • 相关阅读:
    软件质量保证(SQA)
    软件质量保证(SQA)
    在应用程序中使用 Ajax 的时机
    3月18日工作日志88250
    Eclipse 4.0计划
    3月15日工作日志88250
    Eclipse 4.0计划
    3月18日工作日志88250
    在应用程序中使用 Ajax 的时机
    3月15日工作日志88250
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/8425986.html
Copyright © 2020-2023  润新知