如何使用Mockito模拟Context?

时间:2015-12-03 10:43:19

标签: android bluetooth mockito android-wifi android-context

我正在使用Context来访问WifiManager和BluetoothManager等系统级服务。如何使用Mockito模拟这个getApplicationContext()?

3 个答案:

答案 0 :(得分:22)

    Context context = mock(Context.class);

答案 1 :(得分:12)

让我们看看下面的课程:MockContext

如果您需要更多信息,请查看Official Testing Fundamentals页面

答案 2 :(得分:6)

如果要获取Kotlin和Mockito的上下文,可以按照以下方式进行操作:

         <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>com.sample</groupId>
                            <artifactId>kproject1</artifactId>
                        </dependency>

                       <dependency>
                           <groupId>com.sample</groupId>
                           <artifactId>kproject2</artifactId>
                       </dependency>
                    </requiresUnpack>

                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>