问题描述
想寻求有关我的单元测试如何关闭套接字的方法的帮助,并且outputstreamwriter将能够通过。现在,我的单元测试只能涵盖引发Exception的部分。
这是方法:
private final void CloseConnection() {
ErrorHandler objErrHdl = new ErrorHandler();
try {
out.close();
clientSocket.close();
} catch ( IOException e ) {
objErrHdl.LogError( this.getClass().getName(),"CloseConnection","Failed TO CLOSE CONNECTION",e.toString() );
} catch ( Exception e ) {
objErrHdl.LogError( this.getClass().getName(),e.toString() );
}
}
这是我的单元测试:
@Test
public void testCloseConnection() throws IOException,NoSuchMethodException,SecurityException,illegalaccessexception,IllegalArgumentException,InvocationTargetException {
serverSocket = new ServerSocket(6668);
Method method = Systematic.class.getDeclaredMethod("CloseConnection");
method.setAccessible(true);
method.invoke(systematic);
clientSocket = new Socket("127.0.0.1",6668);
clientSocket.close();
out.close();
}
@Test
public void testCloseConnectionException() throws NoSuchMethodException,InvocationTargetException,IOException {
Method method = Systematic.class.getDeclaredMethod("CloseConnection");
method.setAccessible(true);
method.invoke(systematic);
clientSocket = new Socket("0",33654);
}
@Test
public void testCloseConnectionIOException() throws NoSuchMethodException,IOException {
Method method = Systematic.class.getDeclaredMethod("CloseConnection");
method.setAccessible(true);
Socket s = (Socket) method.invoke(systematic);
Mockito.when(s).thenThrow(new IOException());
}
正如我所说,单元测试的testCloseConnectionException是覆盖我正在测试的CloseConnection方法的唯一成功方法。谢谢。
解决方法
我想您在业务代码和测试中使用的对象clientSocket
和out
不相同。
在测试中,您创建了新对象,但是这些对象不会传播到被测类中。
这是我对您的测试的主张:
正在测试的类,类似于您的
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class SomeClass {
private OutputStreamWriter out = null;
private Socket clientSocket = null;
private final void CloseConnection() {
try {
out.close();
clientSocket.close();
} catch (IOException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
}
}
并对其进行测试
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.net.Socket;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {
@Mock
private OutputStreamWriter out;
@Mock
private Socket clientSocket;
@InjectMocks
private SomeClass someClass;
@Test
public void testCloseConnection() throws Exception {
// run test method
Method method = SomeClass.class.getDeclaredMethod("CloseConnection");
method.setAccessible(true);
method.invoke(someClass);
// verify if action was taken
Mockito.verify(clientSocket).close();
Mockito.verify(out).close();
// and nothing else was not occurred
Mockito.verifyNoMoreInteractions(clientSocket,out);
}
@Test
public void testCloseConnectionIOException1() throws Exception {
// throw Exception on out.close()
Mockito.doThrow(new IOException()).when(out).close();
// run test method
Method method = SomeClass.class.getDeclaredMethod("CloseConnection");
method.setAccessible(true);
method.invoke(someClass);
// out.close() was called
Mockito.verify(out).close();
// and nothing else on out
Mockito.verifyNoMoreInteractions(out);
// no call on clientSocket - because out.close() throw Exception
Mockito.verifyNoInteractions(clientSocket);
}
}
在测试中,我们可以模拟java io类,没有必要再次测试那些类。我们要在现实世界中的某些情况下测试我们的应用程序和行为。