I am running a simple JUnit test. The test is:
private HangmanModel model;
private WordsToGuess word;
public void setUp()
{
model = new HangmanModel();
word = new WordsToGuess();
}
#Test
public void addWordAndChoose()
{
WordsToGuess testWord = new WordsToGuess("ahoy");
model.addWord(testWord); <---- NullPointerException
String foundWord = model.randomWord();
assertEquals("Not found the word", testWord, foundWord);
}
In WordsToGuess, the constructor is:
public WordsToGuess(String w)
{
word = w;
}
In HangmanModel, the addWord method is:
private ArrayList<WordsToGuess> words;
words = new ArrayList<WordsToGuess>();
public void addWord(WordsToGuess w)
{
words.add(w);
}
This is a REALLY weird NullPointerException since everything should run perfectly fine. It is copied almost word for word from a similar project. Here is the Stack trace:
java.lang.NullPointerException
at testing.HangmanModelTest.addWordAndChoose(HangmanModelTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.runners.ParentRunner.run(ParentRunner.java:309)
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)
The #Before annotation is required before the setup method for JUnit 4
#Before
public void setUp() {
...
}
Related
I'm getting this error when I try to run a testCase from eclipse:
I'm using eclipse, Appium-Server-GUI-windows-1.22.3-4, Android 4:
java-client-8.2.1.jar
I need your help:
Code:
#Test
public void test() throws MalformedURLException{
//fail("Not yet implemented");
URL link = new URL("http://127.0.0.1:4723/wd/hub");
//AndroidDriver driver;
DesiredCapabilities capacidades = new DesiredCapabilities();
capacidades.setCapability("platformName", "Android");
capacidades.setCapability("appium:platformVersion", "13");
capacidades.setCapability("appium:appActivity", "com.BancaxcxcxActivity");
capacidades.setCapability("appium:appPackage", "com.banxxxv40");
capacidades.setCapability("appium:deviceName", "sdk_gphone64_x86_64");
// Inicio driver
AndroidDriver driver = new AndroidDriver(link ,capacidades);
}
error:
java.lang.IllegalAccessError: tried to access method org.openqa.selenium.remote.http.ClientConfig.<init>
(Ljava/net/URI;
Ljava/time/Duration;
Ljava/time/Duration;
Lorg/openqa/selenium/remote/http/Filter;
Ljava/net/Proxy;Lorg/openqa/selenium/Credentials;)
V from class io.appium.java_client.AppiumClientConfig
at io.appium.java_client.AppiumClientConfig.<init>(AppiumClientConfig.java:62)
at io.appium.java_client.AppiumClientConfig.defaultConfig(AppiumClientConfig.java:79)
at io.appium.java_client.remote.AppiumCommandExecutor.<init>(AppiumCommandExecutor.java:109)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:92)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:117)
at consolePruebasTest.TestMovil.test(TestMovil.java:49)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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)
I'm trying to do a initial conecction with emulated device.
java-client 8.2.1 has issues with resolving dependencies: https://github.com/appium/java-client/issues/1812
Upgrade to 8.3.0 and check.
This is my test method:
#RunWith(Parameterized.class)
#PrepareForTest(FilenameUtils.class)
public class Test {
//code
private final String datum;
private final String expectedResult;
public Test(String datum, String expectedResult){
this.datum = datum;
this.expectedResult = expectedResult;
}
#Parameters
public static Collection<Object[]> generateData(){
return Arrays.asList(new Object[][] {
{ ".jpg", "productimage..jpg" },
{ "jpg", "jpg.jpeg" },
{ "thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog.jpeg", "thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjump.jpeg" }
});
}
#Before
public void setUp() {
}
#After
public void tearDown() {
}
#Test
public void testSanitizeFilename(){
PowerMock.mockStaticPartial(FilenameUtils.class, "getExtension" , "getBaseName");
expect(FilenameUtils.getExtension(datum)).andReturn("jpeg").anyTimes();
expect(FilenameUtils.getBaseName(datum)).andReturn("productimage").anyTimes();
PowerMock.replay(FilenameUtils.class);
String result = FileUtil.sanitizeFilename(datum, defaultName, contentType);
PowerMock.verify(FilenameUtils.class);
assertEquals(result, expectedResult);
}
}
And this is the method to be tested:
public class FileUtil {
//code
public static String sanitizeFilename(String filename, ............) {
//code here
if (filename.length() > 100) {
FilenameUtils.getExtension(fileName);
FilenameUtils.getBaseName(fileName);
}
return fileName;
}
}
The code works only for the cases that the condition in FileUtil.sanitizeFilename is not true. If it is true (by passing a filename > 100 characters), then the following error occurs:
Unexpected method call FilenameUtils.getExtension("thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog.jpeg"):
java.lang.AssertionError
Unexpected method call FilenameUtils.getExtension("thequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydogthequickbrownfoxjumpsoverthelazydog.jpeg"):
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
at org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.invoke(EasyMockMethodInvocationControl.java:91)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)
at org.apache.commons.io.FilenameUtils.getExtension(FilenameUtils.java)
at com.eurodyn.ecatalogue.util.FileUtil.sanitizeFilename(FileUtil.java:235)
at com.eurodyn.ecatalogue.ejb.session.others.AsyncFileDownloadManagerBeanTest.testSanitizeFilename(AsyncFileDownloadManagerBeanTest.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
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.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
What is incorrect in my code?
EDIT:
I used Parameterized class in order to avoid the for loop.
I used PowerMock.mockStaticPartial for the static methods getBaseName and getExtension.
How it worked for me
#Test
public void testSanitizeFilename() {
PowerMock.mockStatic(FilenameUtils.class);
EasyMock.expect(FilenameUtils.getExtension(anyString())).andReturn("jpeg").anyTimes();
EasyMock.expect(FilenameUtils.getBaseName(anyString())).andReturn("jpeg").anyTimes();
PowerMock.replayAll();
for (Map.Entry<String, String> entry : fileUrls.entrySet()) {
System.out.println("KEY: " + entry.getKey());
String result = FileUtil.sanitizeFilename(entry.getKey(), entry.getValue());
assertEquals(result, entry.getValue());
PowerMock.verifyAll();
}
}
Note that I modified sanitizeFilename some to get working code for me. And I used
#Rule
public PowerMockRule rule = new PowerMockRule();
instead of RunWith - but as I had all your errors before this might fix your stuff as well.
Seems like mocking inside the loop was no good idea to begin with - once I moved the mocks out of the loop it got me somewhere.
I am using Jetty client API on my application and I want to test my following code using Junit and Mockito:
public void startHttpClient(){
try {
httpClient.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
My test case is the following:
#Mock HttpClient httpClient;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
testBackend= new TestBackend(3,httpClient);
}
#Test
public void teststartHttpClient() throws Exception {
testBackend.startHttpClient();
assertNotNull(httpClient);
verify(httpClient,atLeastOnce()).start();
}
The problem is that the Mock HttpClient inherit the start() method from AbstractLifeCycle, so I got a null pointer exception on the AbstractLifeCycle class:
How can I mock the inherited method start() call fot the class HttpClient?
The Exception got:
java.lang.NullPointerException
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:61)
at test.TestBackendTest.teststartHttpClient(TestBackendTest.java:75)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.runners.ParentRunner.run(ParentRunner.java:309)
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)
I'm testing Tinkerpop frames. I have a relationship similar to Person Knows Person but everytime I test my code it gives me this error
com.orientechnologies.orient.core.exception.OSchemaException: Cannot change the schema while a transaction is active. Schema changes are not transactional
at com.orientechnologies.orient.core.metadata.schema.OSchemaShared.saveInternal(OSchemaShared.java:956)
at com.orientechnologies.orient.core.metadata.schema.OSchemaShared.releaseSchemaWriteLock(OSchemaShared.java:751)
at com.orientechnologies.orient.core.metadata.schema.OSchemaShared.doCreateClass(OSchemaShared.java:385)
at com.orientechnologies.orient.core.metadata.schema.OSchemaShared.createClass(OSchemaShared.java:314)
at com.orientechnologies.orient.core.metadata.schema.OSchemaProxy.createClass(OSchemaProxy.java:77)
at com.tinkerpop.blueprints.impls.orient.OrientElement$1.call(OrientElement.java:469)
at com.tinkerpop.blueprints.impls.orient.OrientElement$1.call(OrientElement.java:465)
at com.tinkerpop.blueprints.impls.orient.OrientBaseGraph.executeOutsideTx(OrientBaseGraph.java:1658)
at com.tinkerpop.blueprints.impls.orient.OrientElement.checkForClassInSchema(OrientElement.java:465)
at com.tinkerpop.blueprints.impls.orient.OrientEdge.getClassName(OrientEdge.java:474)
at com.tinkerpop.blueprints.impls.orient.OrientEdge.createDocument(OrientEdge.java:511)
at com.tinkerpop.blueprints.impls.orient.OrientEdge.convertToDocument(OrientEdge.java:441)
at com.tinkerpop.blueprints.impls.orient.OrientEdge.setProperty(OrientEdge.java:314)
at com.tinkerpop.frames.annotations.PropertyAnnotationHandler.processElement(PropertyAnnotationHandler.java:34)
at com.tinkerpop.frames.annotations.PropertyAnnotationHandler.processElement(PropertyAnnotationHandler.java:11)
at com.tinkerpop.frames.FramedElement.invoke(FramedElement.java:89)
at com.sun.proxy.$Proxy7.setWeight(Unknown Source)
at com.spicegraph.test.domain.test.DbTest.addNewStuffToDb(DbTest.java:52)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.runners.ParentRunner.run(ParentRunner.java:309)
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)
I'm using OrientDb as the graph provider. What could have possible gone wrong?
Here is my code:
public interface Ingredient extends VertexFrame{
#Property("name")
public String getName();
#Property("name")
public void setName(String name);
#Adjacency(label="usedwith", direction=Direction.BOTH)
public Iterable<Ingredient> getUsedWithIngredient();
#Adjacency(label = "usedwith")
public Ingredient addUsedWithNewIngredient();
#Incidence(label = "usedwith")
public UsedWithEdge addUsedWithIngredient(final Ingredient person);
#Adjacency(label = "usedwith")
public void removeUsedWithIngredient(final Ingredient person);
#Incidence(label = "usedwith")
public void removeUsedWith(final UsedWithEdge usedWith);
}
public interface UsedWithEdge extends EdgeFrame {
#Property("weight")
public void setWeight(int weight);
#Property("weight")
public int getWeight();
#OutVertex
Ingredient getOutIngredient();
#InVertex
Ingredient getInIngredient();
}
and this is a snippet of the test case
Ingredient v1=framedGraph.addVertex(null, Ingredient.class);
Ingredient v2=framedGraph.addVertex(null, Ingredient.class);
v1.setName("cumin");
v1.setConservAndStoring("conservation yearly");
v1.setPrepAndUse("prep prep ");
v2.setName("cinnamon");
v2.setAbout("about is about");
v2.setConservAndStoring("conservation is daily");
UsedWithEdge edge=v2.addUsedWithIngredient(v2);
edge.setWeight((1)); // << error is thrown here !!
framedGraph.getBaseGraph().commit();
This is an OrientDb problem. I used OrientGraphNoTx
OrientGraphNoTx graph=new OrientDbGenericDao().graphInstance();
FramedGraphFactory factory = new FramedGraphFactory(); // make sure you reuse the factory when creating new framed graphs.
framedGraph = factory.create(graph); // wrap the base graph
and it worked pretty well.
I am trying to write a test-case without using a ZUL file for now. I have a very basic function with the following lines.
Class to initialize Listbox:
public class TestComposer {
public void listboxTest(){
ListModelList model = new ListModelList();
Listbox lbOne = new Listbox();
lbOne.setModel(model);
lbOne.setMultiple(true);
System.out.println(lbOne);
System.out.println(model);
}
}
Test-case
public class testSuit {
static ZatsEnvironment env;
#BeforeClass
public static void init() {
System.out.println("Setting server space.");
env = new DefaultZatsEnvironment("/main/webapp/WEB-INF");
env.init("/main/webapp");
}
#AfterClass
public static void end() {
env.destroy();
}
#After
public void after() {
env.cleanup();
}
#Test
public void test() {
TestComposer tc = new TestComposer();
tc.listboxTest();
}
}
This piece of code will produce a null pointer exception. It shows that lbOne is null. Is there a way to use listbox without calling/using a ZUL file?
Stack trace:
java.lang.NullPointerException at
org.zkoss.zk.ui.event.Events.postEvent(Events.java:383) at
org.zkoss.zk.ui.event.Events.postEvent(Events.java:432) at
org.zkoss.zk.ui.event.Events.postEvent(Events.java:366) at
org.zkoss.zul.Listbox.postOnInitRender(Listbox.java:2501) at
org.zkoss.zul.Listbox.setModel(Listbox.java:2266) at
test.TestComposer.listboxTest(TestComposer.java:12) at
test.testSuit.test(testSuit.java:75) 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:47) at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at
org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at
org.junit.runners.ParentRunner.run(ParentRunner.java:309) 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)
Line/trace of the NPE error
test.TestComposer.listboxTest(TestComposer.java:12) at test.testSuit.test(testSuit.java:75) at
lbOne.setModel(model);
PS: I am using ZK web framework. I am using a testing tool that can't capture the traces in zats simulated environment. Thus, I want to directly call these methods without depending on ZUL files.
what about using this strategy:
public void listboxTest(){
ListModelList model = new ListModelList();
Listbox lbOne = new Listbox();
lbOne.setModel(model);
lbOne.setMultiple(true);
System.out.println(lbOne);
System.out.println(model);
}
your TestComposer should extend SelectorComposer if you develop in MVC pattern. Please read MVC tutorial
you should test a composer with a zul instead of creating an instance by yourself.
For example, you should apply the composer to a component and run ZATS client to visit the zul. Please read First Mimic Test Case