JUnit - IllegalArgumentException.class failes? - java

I have created an extremely simple junit test, which creates a null product in the product table in the db.
#Test(expected = IllegalArgumentException.class)
public void testCreate_NULL() {
Product p = null;
createProduct(p);
}
but when I do the junit test it turns blue.
btw. other tests like creating a product, deleting ect. are all green...
I appreciate your answer!!!
PS.: I am using hsql db!
PPS.: The error is:
java.lang.AssertionError: Expected exception:
java.lang.IllegalArgumentException at
org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:35)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at
org.junit.runners.ParentRunner.run(ParentRunner.java:300) 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)
UPDATE:
my createProduct Method justs inserts a product into the db and before it checks:
if(p==null) {
throw new IllegalArgumentException("null objects impossible");
}

Perhaps it's throwing NullReferenceException instead of IllegalArgumentException? You should be able to see in the test failure details.
It's fairly hard to diagnose this without any indication of what's in the createProduct method.

Related

Firestore local junit tests

Is there a library to get a mocked version of Firestore for local unit tests like the one for google app engines datastore:
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()); (https://cloud.google.com/appengine/docs/standard/java/tools/localunittesting#datastore-memcache)
The Firestore mock of this file https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-firestore/src/test/java/com/google/cloud/firestore/FirestoreTest.java does not work:
java.lang.NullPointerException
at com.google.cloud.firestore.FirestoreImpl.sendRequest(FirestoreImpl.java:399)
at com.google.cloud.firestore.UpdateBuilder.commit(UpdateBuilder.java:467)
at com.google.cloud.firestore.WriteBatch.commit(WriteBatch.java:41)
at com.google.cloud.firestore.DocumentReference.set(DocumentReference.java:156)
at com.google.cloud.firestore.FirestoreMocked.test(FirestoreMocked.kt:25)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)

Mock spring MessageContext

i would like to mock these methods:
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) messageContext.getRequest();
SoapBody requestBody = saajSoapMessage.getSoapBody();
I tried it with this
messageContextMock = mock(MessageContext.class);
saajSoapMessageMock = mock(SaajSoapMessage.class);
when(messageContextMock.getRequest()).thenReturn((SaajSoapMessage) saajSoapMessageMock);
when(saajSoapMessageMock.getSoapBody()).thenReturn(soapBodyMock);
But I have problem with mocking getSoapBody() because Junit wrote me:
at org.springframework.ws.soap.AbstractSoapMessage.getSoapBody(AbstractSoapMessage.java:36)
How to mock this operation correctly?
EDIT (full stack):
java.lang.NullPointerException
at org.springframework.ws.soap.AbstractSoapMessage.getSoapBody(AbstractSoapMessage.java:36)
at com.project.my.WebMessageTest.setUp(WebMessageTest.java:33)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)
Try adding a body to your mocked objects, try this:
messageContextMock = mock(MessageContext.class);
saajSoapMessageMock = mock(SaajSoapMessage.class);
soapEnvelopeMock = mock(SOAPEnvelope.class);
when(saajSoapMessageMock.getEnvelope()).thenReturn(soapEnvelopeMock);
when(messageContextMock.getRequest()).thenReturn(saajSoapMessageMock);

Protostuff serialized bytecode on Android cannot be deserialized on PC

When I try to deserialize my serialized model on PC, I get the strange error seen at the bottom.
Deserializing works on Android, as does the case where I serialize the same model on PC and deserialize it on PC.
So this appears to be an interoperability problem.
What can I do to ensure it serializes the same way?
My model that has to be serialized has the following POJOS and collections:
ExplicitIdStrategy.Registry reg = new ExplicitIdStrategy.Registry();
reg.registerPojo(EntityData.class, 1);
reg.registerPojo(ProbabilityModel.class, 2);
reg.registerPojo(ProbabilityModelEntryList.class, 3);
reg.registerCollection(CollectionSchema.MessageFactories.valueOf("ArrayList"), 4);
reg.registerMap(MapSchema.MessageFactories.valueOf("HashMap"), 5);
reg.registerEnum(ProbabilityModel.OtherCounted.class, 6);
reg.registerCollection(CollectionSchema.MessageFactories.valueOf("HashSet"), 7);
It seems the Map is serialized differently on Android and on a Windows PC. I have no deep knowledge of protostuff, but it is fairly strange that it depends on operating systems when using operating system independent JAVA...
com.dyuproject.protostuff.ProtostuffException: The map was incorrectly serialized.
at com.dyuproject.protostuff.MapSchema.mergeFrom(MapSchema.java:316)
at com.dyuproject.protostuff.MapSchema.mergeFrom(MapSchema.java:31)
at com.dyuproject.protostuff.GraphCodedInput.mergeFrom(GraphCodedInput.java:153)
at com.dyuproject.protostuff.CodedInput.mergeObjectEncodedAsGroup(CodedInput.java:271)
at com.dyuproject.protostuff.CodedInput.mergeObject(CodedInput.java:239)
at com.dyuproject.protostuff.GraphCodedInput.mergeObject(GraphCodedInput.java:108)
at com.dyuproject.protostuff.runtime.RuntimeMapFieldFactory$5.mergeFrom(RuntimeMapFieldFactory.java:463)
at com.dyuproject.protostuff.runtime.MappedSchema.mergeFrom(MappedSchema.java:188)
at com.dyuproject.protostuff.GraphIOUtil.mergeFrom(GraphIOUtil.java:76)
at com.android.diabetesmodel.ModelEntity.deserialize(ModelEntity.java:185)
at com.android.diabetesmodel.test.VersionManagerEntityTest.from1to2Upgrade(VersionManagerEntityTest.java:47)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)
Did you serialize and deserialize with the same Protostuff JVM option protostuff.runtime.collection_schema_on_repeated_fields?

Column "name_c" not found error when running a query on H2

I'm working with an in-memory instance of H2 for unit tests and I'm trying to run the following query:
SELECT name AS name_c FROM users ORDER BY lower(name_c)
(The real query is much more complicated but this simple query has the same symptom)
And I receive the following exception:
org.h2.jdbc.JdbcSQLException: Column "NAME_C" not found; SQL statement:
SELECT name AS "name_c" FROM users ORDER BY lower(name_c) [42122-163]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:138)
at org.h2.expression.Function.optimize(Function.java:1705)
at org.h2.command.dml.Select.prepare(Select.java:799)
at org.h2.command.Parser.prepareCommand(Parser.java:218)
at org.h2.engine.Session.prepareLocal(Session.java:415)
at org.h2.engine.Session.prepareCommand(Session.java:364)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1121)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:164)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:152)
at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
at com.name.dal.Connection.executeQuery(Connection.java:462)
at com.name.model.users.UserTest.testTest(UserTest.java:324)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)
Anyone knows why?
I'm creating the db using the following connection string:
jdbc:h2:mem:db1;MODE=MYSQL;DB_CLOSE_DELAY=60;IGNORECASE=TRUE
Thanks,
Column aliases are only available in the order by clause if they are used as is (alone). This is the same behavior as with other databases (according to my test, PostgreSQL and Derby). There are some databases that do support what you want (MySQL for example), but this is not part of the SQL standard. So for example:
create table test(id int primary key, name varchar(255));
insert into test values(1, 'hello');
insert into test values(2, 'world');
This works with all databases I tested:
select name as c_name from test order by c_name;
select name as c_name from test order by lower(name);
With this data, the following query failed for me with PostgreSQL, Apache Derby, and H2. It works with MySQL:
select name as c_name from test order by lower(c_name);

Log4j + Amqp Log sample Code is Failing

I am trying to create a Distributed Logging system, where my each tomcat instance will put the logs into a exchange/queue and it will be accumulated and stored in one plcae.
I was reading about Log4j + Amqp using rabbitmq.
I have downloaded the following sample code also.
https://nodeload.github.com/SpringSource/spring-amqp-samples/zipball/master
Imported project log4j from the package.
When i run the IntegrationTest.java (Junit). I get a failure. Following is the trace.
java.lang.AssertionError: Time out waiting for message
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.assertTrue(Assert.java:43)
at org.springframework.amqp.rabbit.log4j.web.controller.IntegrationTest.logInfo(IntegrationTest.java:75)
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.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
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:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
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)
This is happening because no logs is coming to the listener AmqpLogMessageListener in the watch time.
I have some doubts about the code:
How the listener AmqpLogMessageListener.java is registered for listening to the configured exchange.
Why the Test is failing ?
Juste to be sure, check your log4j properties file against the source https://github.com/SpringSource/spring-amqp-samples/blob/master/log4j/src/main/resources/log4j.properties.
Then, it may be your connection on RabbitMQ which can be long to obtain, so change this:
new CountDownLatch(1);
by this:
new CountDownLatch(5);

Categories

Resources