Я пытаюсь использовать mockito fwk для некоторых методов, которые я определил, для checkConnection и публикации любого документа в API.Так как я новичок в mockito, меня немного смущает, что и как мне планировать мой тест
Я только что попробовал основы mockito с помощью assertEquals, но я хотел знать, как лучше смоделировать свой сценарий.В приведенных ниже классах класс A использует HTTPSUrlConnection и проверяет только успешное соединение или нет; в классе B его метод, использующий объект соединения класса A, пытается отправить документ в API.Теперь, как мне смоделировать этот сценарий.
class A{
public void checkConnection(){
HttpsURLConnection con = null;
URL url = null;
try {
url = new URL(endPoint); //endpoint gets the value from properties file at runtime
} catch (MalformedURLException e) {
}
try {
con = (HttpsURLConnection) url.openConnection();
} catch (IOException e) {
}
String userpass = userName + ":" + password; //gets the value from properties file at runtime
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
con.setRequestProperty("Authorization", basicAuth);
con.setDoInput(true);
con.setDoOutput(true);
try {
con.setRequestMethod("POST");
} catch (ProtocolException e) {
}
}
}
class B{
public String send(){
String responseBody = null;
SenderResponse senderResponse = null;
try {
Resource r = new ClassPathResource("xslt.xsl");
File f = null;
f = new File(r.getURL().getPath());
Source xs = new StreamSource(f);
Resource r1 = new ClassPathResource("input.xml");
File f1 = null;
f1 = new File(r1.getURL().getPath());
Source xml = new StreamSource(f1)
TransformerFactory tf = TransformerFactory.newInstance();
tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Transformer trans = tf.newTransformer(xs);
StringWriter stringWriter = new StringWriter();
Result res = new StreamResult(stringWriter);
trans.transform(xml, res);
String result = stringWriter.toString();
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(result);
writer.flush();
writer.close();
if (HttpStatus.OK.value() == con.getResponseCode()) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
responseBody = response.toString();
} else {
}
} catch (TransformerFactoryConfigurationError | TransformerException e) {
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}
catch (Exception e) {
}
return responseBody;
}
}
Я просто хочу использовать существующие методы, определенные в обоих этих классах, вместо написания ложных кодов