JUnit, can I create an EntityManager or even a session without running the server?

import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
public class OrderTest {
@PersistenceContext(unitName = "WebPU")
private EntityManager em;

@BeforeAll
public static void setUpClass() {

}
@Test
public boolean testOrderCreation() {
List<Destination> destinationList = em.createQuery("SELECT dt FROM Destination dt WHERE dt.id=:id", Destination.class)
.setParameter("id", 1).getResultList();
Destination destination = destinationList.get(0);
if (destinationList.size() != 1) {
return false;
}
List<Product> productList = em.createQuery("SELECT pdt FROM Product pdt WHERE pdt.id=:id", Product.class)
.setParameter("id", 1).getResultList();

if (productList.size() != 1) {
return false;
}
Product product = productList.get(0);

Orders order = new Orders();
order.setProductId(product);
order.setDestination(destination);
order.setQty(10);
order.setCreatedAt(LocalDateTime.now());
order.setExpectedDate(LocalDateTime.now().plusDays(5));
order.setOrderStatus("not_shipped");
em.persist(order);
return true;
}
}
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
public class OrderTest {
@PersistenceContext(unitName = "WebPU")
private EntityManager em;

@BeforeAll
public static void setUpClass() {

}
@Test
public boolean testOrderCreation() {
List<Destination> destinationList = em.createQuery("SELECT dt FROM Destination dt WHERE dt.id=:id", Destination.class)
.setParameter("id", 1).getResultList();
Destination destination = destinationList.get(0);
if (destinationList.size() != 1) {
return false;
}
List<Product> productList = em.createQuery("SELECT pdt FROM Product pdt WHERE pdt.id=:id", Product.class)
.setParameter("id", 1).getResultList();

if (productList.size() != 1) {
return false;
}
Product product = productList.get(0);

Orders order = new Orders();
order.setProductId(product);
order.setDestination(destination);
order.setQty(10);
order.setCreatedAt(LocalDateTime.now());
order.setExpectedDate(LocalDateTime.now().plusDays(5));
order.setOrderStatus("not_shipped");
em.persist(order);
return true;
}
}
33 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @Rag...JN 🌌 🦡 👽 💰! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Rag...JN 🌌 🦡 👽 💰 🐊
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class OrderTestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(OrderTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}

System.out.println(result.wasSuccessful());
}
}
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class OrderTestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(OrderTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}

System.out.println(result.wasSuccessful());
}
}
szatkus
szatkus7mo ago
Yes
Rag...JN 🌌 🦡 👽 💰 🐊
ok so but now my test is failure
Rag...JN 🌌 🦡 👽 💰 🐊
how can I know why this is it?
szatkus
szatkus7mo ago
Because you return boolean for some reason.
Rag...JN 🌌 🦡 👽 💰 🐊
I just changed it to void
szatkus
szatkus7mo ago
Oh, I see.
Rag...JN 🌌 🦡 👽 💰 🐊
same class with void
public class OrderTest {
@PersistenceContext(unitName = "WebPU")
private EntityManager em;

@BeforeAll
public static void setUpClass() {

}
@Test
public void testOrderCreation() {
List<Destination> destinationList = em.createQuery("SELECT dt FROM Destination dt WHERE dt.id=:id", Destination.class)
.setParameter("id", 1).getResultList();
Destination destination = destinationList.get(0);
if (destinationList.size() != 1) {
System.out.println("couldn't find destination");
return ;
}
List<Product> productList = em.createQuery("SELECT pdt FROM Product pdt WHERE pdt.id=:id", Product.class)
.setParameter("id", 1).getResultList();

if (productList.size() != 1) {
System.out.println("couldn't find product");
return ;
}
Product product = productList.get(0);

Orders order = new Orders();
order.setProductId(product);
order.setDestination(destination);
order.setQty(10);
order.setCreatedAt(LocalDateTime.now());
order.setExpectedDate(LocalDateTime.now().plusDays(5));
order.setOrderStatus("not_shipped");
em.persist(order);
System.out.println("Insertion Success");
}
}
public class OrderTest {
@PersistenceContext(unitName = "WebPU")
private EntityManager em;

@BeforeAll
public static void setUpClass() {

}
@Test
public void testOrderCreation() {
List<Destination> destinationList = em.createQuery("SELECT dt FROM Destination dt WHERE dt.id=:id", Destination.class)
.setParameter("id", 1).getResultList();
Destination destination = destinationList.get(0);
if (destinationList.size() != 1) {
System.out.println("couldn't find destination");
return ;
}
List<Product> productList = em.createQuery("SELECT pdt FROM Product pdt WHERE pdt.id=:id", Product.class)
.setParameter("id", 1).getResultList();

if (productList.size() != 1) {
System.out.println("couldn't find product");
return ;
}
Product product = productList.get(0);

Orders order = new Orders();
order.setProductId(product);
order.setDestination(destination);
order.setQty(10);
order.setCreatedAt(LocalDateTime.now());
order.setExpectedDate(LocalDateTime.now().plusDays(5));
order.setOrderStatus("not_shipped");
em.persist(order);
System.out.println("Insertion Success");
}
}
szatkus
szatkus7mo ago
You're meddling with pure JUnit. That's unusual. Let me look this up. failure.getException().printStackTrace();
Rag...JN 🌌 🦡 👽 💰 🐊
ok null pointer exception is the cause
Rag...JN 🌌 🦡 👽 💰 🐊
but there is clearly a Destination with id 1
szatkus
szatkus7mo ago
Probably there's nothing there to interpret the PersistenceContext annotation.
Rag...JN 🌌 🦡 👽 💰 🐊
so it is not even there in the first place EntityManager is null
Rag...JN 🌌 🦡 👽 💰 🐊
Arquillian needed?
No description
szatkus
szatkus7mo ago
No idea, I wouldn't use a single thing that you're using except Java.
Rag...JN 🌌 🦡 👽 💰 🐊
why? The requirement is to use JUnit for the assignment
szatkus
szatkus7mo ago
Java EE is getting less and less popular and nowdays it's usually a red flag indicating a shitty project.
Rag...JN 🌌 🦡 👽 💰 🐊
yah we had a convo today in general
szatkus
szatkus7mo ago
I'd use JUnit, but you're apparently using JUnit 4, which I wouldn't pick, because JUnit 5 is more enjoyable to work with.
john
john7mo ago
idk really, the tech is too old
Rag...JN 🌌 🦡 👽 💰 🐊
yah but 5 has like a lot more configurations so I chose 4 because just one or two dependencies
szatkus
szatkus7mo ago
If you asked that a month ago I could've look up, because I had access to a Java EE project with JUnit tests.
Rag...JN 🌌 🦡 👽 💰 🐊
also I just started to work with JUnit 3 horus hours ago. read an article started to implement into the project where else can I apply JUnit beside the EJB because this thing requires a lot of configuration at last to get some marks
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server