preliminary test preparation

This commit is contained in:
Marcin Hutek 2023-12-31 18:41:09 +01:00
parent 6c65e5b48b
commit a13be5d0c1
6 changed files with 58 additions and 76 deletions

View File

@ -42,6 +42,12 @@
<artifactId>ecj</artifactId>
<version>3.28.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,9 +1,8 @@
package org.example;
import jakarta.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
@Table(name="category")

View File

@ -1,7 +1,5 @@
package org.example;
import jakarta.persistence.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.time.ZonedDateTime;

View File

@ -1,9 +1,7 @@
package org.example;
import jakarta.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
@Table(name="language")

View File

@ -1,10 +1,8 @@
package org.example;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

View File

@ -1,68 +1,51 @@
//package org.example;
//
//import jdk.jfr.Event;
//import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.boot.MetadataSources;
//import org.hibernate.boot.registry.StandardServiceRegistry;
//import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
//import org.junit.Test;
//import org.junit.jupiter.api.AfterEach;
//import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.Disabled;
//
//import java.util.List;
//
//import static org.hamcrest.MatcherAssert.assertThat;
//
//public class HibernateExampleTest {
// private SessionFactory sessionFactory;
//
// @BeforeEach
// protected void setUp() {
// // A SessionFactory is set up once for an application!
// final StandardServiceRegistry registry =
// new StandardServiceRegistryBuilder()
// .build();
// try {
// sessionFactory =
// new MetadataSources(registry)
// .addAnnotatedClass(Event.class)
// .buildMetadata()
// .buildSessionFactory();
// }
// catch (Exception e) {
// // The registry would be destroyed by the SessionFactory, but we
// // had trouble building the SessionFactory so destroy it manually.
// StandardServiceRegistryBuilder.destroy(registry);
// }
// }
//
//// @AfterEach
//// protected void tearDown() throws Exception {
//// if ( sessionFactory != null ) {
//// sessionFactory.close();
//// }
//// }
//
// @Test
// public void postgres_fetch_actors() {
// try (Session session = sessionFactory.getCurrentSession()) {
// session.beginTransaction();
//
// List<Actor> actors = session.createQuery("select a from Actor a", Actor.class).list();
// actors.forEach(System.out::println);
//
//
// session.getTransaction().commit();
// }
// }
//
//
//// @Test
//// @Disabled
//// public void how_does_hibernate_work() {
//// Actor actor = null;
//// assertThat(actor.getFirstName()).isEqualTo("Penelope");
//// }
//}
import org.example.Queries;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
public class QueriesTest {
@Test
public void testGetFilmByLangYear() {
// Przygotowanie testowych danych
Session mockSession = mock(Session.class);
Queries queries = new Queries(mockSession);
Query<Object[]> mockQuery = mock(Query.class);
when(mockSession.createQuery(anyString(), eq(Object[].class))).thenReturn(mockQuery);
String language = "English";
String releaseYear = "2023";
// Test poprawnego zapytania
List<Object[]> expectedResult = List.of(
new Object[] { 1, "Film Title 1", "English" },
new Object[] { 2, "Film Title 2", "English" }
);
when(mockQuery.getResultList()).thenReturn(expectedResult);
List<Object[]> result = queries.getFilmByLangYear(language, releaseYear);
assertNotNull(result);
assertEquals(expectedResult.size(), result.size());
// Test sytuacji bez wyników
when(mockQuery.getResultList()).thenReturn(List.of());
List<Object[]> emptyResult = queries.getFilmByLangYear(language, releaseYear);
assertNotNull(emptyResult);
assertTrue(emptyResult.isEmpty());
// Inne testy, np. obsługa parametrów null
// ...
// Sprawdzenie, czy metoda została wywołana z oczekiwanymi parametrami
verify(mockQuery).setParameter("language", language);
verify(mockQuery).setParameter("releaseYear", releaseYear);
verify(mockSession).createQuery(anyString(), eq(Object[].class));
}
}