Classes 2022

This commit is contained in:
marcin witkowski 2022-10-16 20:34:41 +02:00
parent 0d1a275eb2
commit 01ad4d8855
8 changed files with 119 additions and 4 deletions

13
pom.xml
View File

@ -63,6 +63,19 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,45 @@
package second.junit;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpQueryClass {
public String query() {
String result = "none";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet request = new HttpGet("https://api.gios.gov.pl/pjp-api/rest/station/findAll");
// add request headers
request.addHeader("custom-key", "programming");
try (CloseableHttpResponse response = httpclient.execute(request)) {
// Get HttpResponse Status
System.out.println(response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
Header headers = entity.getContentType();
System.out.println(headers);
if (entity != null) {
// return it as a String
result = EntityUtils.toString(entity);
System.out.println(result);
}
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return result;
}
}

View File

@ -0,0 +1,10 @@
package second.junit;
public class Main {
public static void main(final String... args) {
ProcessQuery processQuery = new ProcessQuery(new HttpQueryClass());
System.out.println(processQuery.process());
}
}

View File

@ -0,0 +1,17 @@
package second.junit;
import java.util.Locale;
public class ProcessQuery {
HttpQueryClass queryClass;
public ProcessQuery(HttpQueryClass queryClass) {
this.queryClass = queryClass;
}
public String process() {
return queryClass.query().toUpperCase();
}
}

View File

@ -1,5 +1,5 @@
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
log4j.rootLogger=INFO, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender

View File

@ -1 +0,0 @@
test resource file

View File

@ -1,4 +1,3 @@
package second.junit;
import org.junit.Assert;
@ -20,7 +19,7 @@ public class AdvanceMathTest {
@Test
public void additionTest() {
Integer a = math.addition(1, 4);
assertTrue(a == 5);
Assert.assertTrue(a == 5);
}
@Test

View File

@ -0,0 +1,32 @@
package second.junit;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
public class MockTest {
HttpQueryClass httpQueryClass;
@Before
public void setUp() {
System.out.println("Run setUp");
httpQueryClass = Mockito.mock(HttpQueryClass.class);
}
@Test
public void mockTestExample() {
Mockito.when(httpQueryClass.query()).thenReturn("test");
ProcessQuery processQuery = new ProcessQuery(httpQueryClass);
String result = processQuery.process();
assertThat(result).isEqualTo("TEST");
}
}