How to rewrite mockControl test to spring 4 - java

In spring 3.6 I am testing client side according to this example: http://javacrumbs.net/spring-ws-test/
Problem is this doesn work with spring 4. It throw exception:
java.lang.IncompatibleClassChangeError: Found interface org.springframework.test.context.TestContext, but class was expected
at net.javacrumbs.springws.test.simple.annotation.WsMockControlTestExecutionListener.prepareTestInstance(WsMockControlTestExecutionListener.java:47)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
how should I rewrite this test to spring 4 ?

First I try to debug this error with some more information so I create class:
import net.javacrumbs.springws.test.simple.annotation.WsMockControlTestExecutionListener;
import net.javacrumbs.springws.test.util.MockMessageSenderInjector;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.test.context.TestContext;
public class WsMockControlTestExecutionSpring4Listener extends WsMockControlTestExecutionListener {
private static final String MESSAGE_SENDER_BEAN_NAME = "net.javacrumbs.springws.test.simple.annotation.WsMockControlMockWebServiceMessageSender";
private static final String MOCK_CONTROL_BEAN_NAME = "net.javacrumbs.springws.test.simple.annotation.WsMockControl";
#Override
public void prepareTestInstance(TestContext testContext) throws Exception {
ConfigurableListableBeanFactory beanFactory = ((AbstractApplicationContext) testContext.getApplicationContext())
.getBeanFactory();
if (!beanFactory.containsBean(MOCK_CONTROL_BEAN_NAME)) {
beanFactory.registerSingleton(MOCK_CONTROL_BEAN_NAME, createWsMockControlFactoryBean());
beanFactory.registerSingleton(MESSAGE_SENDER_BEAN_NAME, createMessageSender());
new MockMessageSenderInjector().postProcessBeanFactory(beanFactory);
}
}
}
and in test I used this listener:
#TestExecutionListeners({ WsMockControlTestExecutionSpring4Listener.class,
DependencyInjectionTestExecutionListener.class })
and now the weird part. Tests are again working but I really dont understand why. I did nothing. Just override method prepareTestInstance with the exactly same code as in WsMockControlTestExecutionListener. Can someone explain me this "solution"

Related

Wrong overloaded method being called

I am trying to use the contains Matcher with a List argument that has this signature:
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>>
contains(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {
However, my code actually ends up calling the varargs version with this signature:
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>>
contains(E... items) {
Why is this happening? I am using the exact same code as the Hamcrest documentation. I am using JUnit 4.8.1 and Hamcrest 1.3.
Full code:
package com.mycompany.myapp;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Test;
public class Test1
{
#Test
public void test() {
Assert.assertThat(Arrays.asList("foo", "bar"),
contains(Arrays.asList(equalTo("foo"), equalTo("bar"))));
}
}
The test should pass, but instead fails with:
java.lang.AssertionError:
Expected: iterable containing [<["foo", "bar"]>]
got: <[foo, bar]>
at org.junit.Assert.assertThat(Assert.java:778)
at org.junit.Assert.assertThat(Assert.java:736)
at com.mycompany.myapp.Test1.test(Test1.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

#After method being run first by JUnit

I am running into a strange issue with my JUnit test case: the #After method is invoked first by JUnit.
Here is the abstract class I use for my test:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = { TestServicePlusRepositoryIntegrationConfiguration.class })
#ActiveProfiles(Profiles.TEST)
#Transactional
public abstract class AbstractServicePlusRepositoryIntegrationTest {
}
Here is the problematic test case:
public class AdvertisementServiceSecurityTest extends AbstractServicePlusRepositoryIntegrationTest {
...
#Before
public void setUp() {
advertisement = advertisementFactory.createAdvertisement(OWNER_OF_ADVERTISEMENT_EMAIL);
impersonator = memberFactory.createMember(IMPERSONATOR_EMAIL, Role.ROLE_BASIC_CHILDCAREWORKER);
when(geolocationServiceMock.retrieveAddressFromReference("valid-reference")).thenReturn(new Address());
}
#After
public void tearDown() {//Executed first!
advertisementFactory.deleteAdvertisement(advertisement);
memberFactory.deleteMember(impersonator);
}
...
#Test(expected = AccessDeniedException.class)
#WithUserDetails(IMPERSONATOR_EMAIL)
public void deleteAdvertisementByIdWithInvalidMember() {
advertisementService.deleteAdvertisement(advertisement.getId());
}
What could cause the #After method to run first? FYI, I don't use JUnit 3.
Here is the full source code for the test case: https://gist.github.com/balteo/e8c2ea96db22d3f9c88e
It appears an exception was raised in the #Before method causing it not to complete:
2014-11-08 13:39:12.169 WARN 4513 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener#44c03695] to process 'before' execution of test method [public void com.bignibou.it.service.advertisement.AdvertisementServiceSecurityTest.deleteAdvertisementByIdWithInvalidMember()] for test instance [com.bignibou.it.service.advertisement.AdvertisementServiceSecurityTest#49438269]
java.lang.IllegalStateException: Unable to create SecurityContext using #org.springframework.security.test.context.support.WithUserDetails(value=joe.hacker#gmail.com)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:84)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.beforeTestMethod(WithSecurityContextTestExecutionListener.java:60)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:249)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.security.core.userdetails.UsernameNotFoundException: Username: joe.hacker#gmail.com not found!
at com.bignibou.service.security.MemberUserDetailsService.loadUserByUsername(MemberUserDetailsService.java:22)
at org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.createSecurityContext(WithUserDetailsSecurityContextFactory.java:48)
at org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.createSecurityContext(WithUserDetailsSecurityContextFactory.java:36)
at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:82)
... 24 common frames omitted
tearDown
Using Spring's #BeforeTransaction see javadocs here allowed the test to run and pass:
#BeforeTransaction
public void setUp() {
System.out.println("setUp");
advertisement = advertisementFactory.createAdvertisement(OWNER_OF_ADVERTISEMENT_EMAIL);
impersonator = memberFactory.createMember(IMPERSONATOR_EMAIL, Role.ROLE_BASIC_CHILDCAREWORKER);
when(geolocationServiceMock.retrieveAddressFromReference("valid-reference")).thenReturn(new Address());
}
#AfterTransaction
public void tearDown() {
System.out.println("tearDown");
advertisementFactory.deleteAdvertisement(advertisement);
memberFactory.deleteMember(impersonator);
}

org.mockito.exceptions.misusing.InvalidUseOfMatchersException

I have this test
#Test
public void addVacancy() throws Exception{
Vacancy mockVacancy = Mockito.mock(Vacancy.class);
BindingResult mockResult= Mockito.mock(BindingResult.class);
RedirectAttributes mockRedirectAttributes=Mockito.mock(RedirectAttributes.class);
Model mockModel = Mockito.mock(Model.class);
ModelAndView modelAndView;
when(mockResult.hasErrors()).thenReturn(false);
VacancyService vacancyService = Mockito.mock(VacancyService.class);
vacancyMenuController.vacancyService = vacancyService;
modelAndView = vacancyMenuController.addVacancy(mockVacancy, mockResult, mockModel, mockRedirectAttributes);
Mockito.verify(vacancyService, Mockito.times(1)).add(Mockito.any(Vacancy.class));//good work
Mockito.verify(mockRedirectAttributes,Mockito.times(1)).addAttribute("message",anyString());// exception
}
for this method:
#RequestMapping("/addVacancy")
public ModelAndView addVacancy(#ModelAttribute("myVacancy") #Valid Vacancy vacancy,BindingResult result, Model model,RedirectAttributes redirectAttributes){
if(result.hasErrors()){
model.addAttribute("message","validation error");
return new ModelAndView("vacancyDetailsAdd");
}
vacancyService.add(vacancy);
ModelAndView mv = new ModelAndView("redirect:goToVacancyDetails");
mv.addObject("idVacancy", vacancy.getId());
redirectAttributes.addAttribute("message", "added correctly at "+ new Date());
return mv;
}
why this string is works good:
Mockito.verify(vacancyService, Mockito.times(1)).add(Mockito.any(Vacancy.class));//good work
but here I see exception
Mockito.verify(mockRedirectAttributes,Mockito.times(1)).addAttribute("message",anyString());// exception
full trace:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.epam.hhsystem.web.controllers.VacancyMenuControllerTest.addVacancy(VacancyMenuControllerTest.java:186)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at com.epam.hhsystem.web.controllers.VacancyMenuControllerTest.addVacancy(VacancyMenuControllerTest.java:186)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Your addAttribute() verify needs to have eq() around the String "message".
When you use one of the matchers you have to use it for all parameters in the method.
Mockito.verify(mockRedirectAttributes,Mockito.times(1)).addAttribute(eq("message"),anyString());
See the added eq.

unable to resolve java.lang.NullPointerException

I have three classes- class 'ABC' 'PQR' and 'XYZ'
class ABC
#Test
public void getModulesid() throws Exception {
Long GrpId= PQR.getExistingGroupId();
System.out.println(GrpId);
}
class PQR
public static Long getExistingGroupId() throws Exception {
Long GrpId;
List<MsecBusGroups> busGroupsList = new ArrayList<MsecBusGroups>();
busGroupsList=XYZ.getBusGroups();
if (busGroupsList!=null){
MsecBusGroups It1=busGroupsList.get(busGroupsList.size()-1);
GrpId=It1.getId();
return GrpId;
} else {
throw new Exception("BUSINESS GROUPS LIST RETURNED IS NULL");
}
}
class XYZ
public List<MsecBusGroups> getBusGroups() throws PersistenceException {
final String METHOD_NAME = "getBusGroups()";
LogHelper.logEntry(CLASS_NAME, METHOD_NAME);
List<MsecBusGroups> list = null;
try {
Query q = em.createNamedQuery("getBusGroups");
list = q.getResultList();
if (list == null) {
LogHelper.debug(CLASS_NAME, METHOD_NAME, "No Record Found");
}
...
}
When I run the test method in ABC class its throwing java.lang.NullPointerException.
java.lang.NullPointerException
at com.causeway.platform.security.model.actions.BusServiceActions.getExistingGroupId(BusServiceActions.java:202)
at com.causeway.platform.security.model.serviceImpl.SecurityBusServiceTest.testDeleteModule(SecurityBusServiceTest.java:1231)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I see that getBusGroups() is not static. This means that in busGroupsList=XYZ.getBusGroups(); XYZ is a variable. The NPE is probably thrown because XYZ is null.
securityBusService is probably null. Where is it initialized?

On accessing database through test I get play.exceptions.JPAException: The JPA context is not initialized

I am trying to access the database through a class using PlayFremwork and writing a test
import static org.junit.Assert.*;
import javax.persistence.EntityManager;
import models.com.vlist.entity.classes.Playlist;
import org.junit.Test;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;
import play.jobs.OnApplicationStart;
import play.mvc.Scope.Session;
public class PlaylistTest {
#Test
#Transactional
public void insertIntoPlaylist() {
Playlist playlist = new Playlist();
playlist.setId(1);
playlist.setName("test");
EntityManager em = JPA.em();
em.persist(playlist);
}
}
The error stacktrace is:
play.exceptions.JPAException: The JPA context is not initialized. JPA Entity Manager automatically start when one or more classes annotated with the #javax.persistence.Entity annotation are found in the application.
at play.db.jpa.JPA.get(JPA.java:22)
at play.db.jpa.JPA.em(JPA.java:51)
at PlaylistTest.insertIntoPlaylist(PlaylistTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
How can I resolve this issue while writing test?
Thank you
FIXED!! By extending FunctionalTest
public class PlaylistTest extends FunctionalTest {
#Test
#Transactional
public void insertIntoPlaylist() {
Playlist playlist = new Playlist();
playlist.setName("new_playlist_again");
EntityManager em = JPA.em();
em.persist(playlist);
}
}
public class PlaylistTest extends FunctionalTest {
#Test
#Transactional
public void insertIntoPlaylist() {
Playlist playlist = new Playlist();
playlist.setName("new_playlist_again");
EntityManager em = JPA.em();
em.persist(playlist);
}
}

Categories

Resources