我想为我的课写一个单元测试。我也看到了this。我的课正在使用套接字。
我首先初始化Chat.class
chat = Chat.init(context);
chat.addListener(this);
然后我打电话给
chat.connect(String serverAddress, String appId, String severName);
之后
我打电话给getUserInfo
。当我收到此方法的响应时,我打电话给了chatListener.callOnUserInfo
。
我知道,如果我有上下文,应该使用InstrumentedTest。
这是我的测试班
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest extends ChatAdapter {
private static Chat chat;
private static String TOKEN = "bebc31c4ead6458c90b607496dae25c6";
private static ChatListener chatListener;
private ChatAdapter chatAdapter;
private static Context appContext;
@Test
public void useAppContext() {
// Context of the app under test.
appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.fanap.podchat.test", appContext.getPackageName());
}
@BeforeClass
public static void init() {
chat = Chat.init(appContext).addListener(chatListener);
chat.connect("ws://172.16.106.26:8003/ws",
"Chat", "server", TOKEN, "http://172.16.110.76",
"http://172.16.106.26:8080");
}
@Test
public void getUserInfo() {
chat.getUserInfo();
onUserInfo("");
}
}