ktcl
ktcl

Reputation: 413

How do I create a BLOB object by Java without connection to database

Like the title, I need to create a BLOB object for my unit test.

I read this

How to create BLOB object in java?

But it needs to connect to a specific database.

Thank you for taking a look.

Upvotes: 2

Views: 7966

Answers (2)

ktcl
ktcl

Reputation: 413

I think this way is ok:

byte[] bytes = "A byte array".getBytes();
Blob blob = new javax.sql.rowset.serial.SerialBlob(bytes);

Upvotes: 2

sweet suman
sweet suman

Reputation: 1082

Since BLOB is an expensive resource and you are doing a test, mocking is a great way to create a BLOB.

Also with this, you can control what the BLOB is without actually reading through a large object.

How to do it depends entirely on the testing framework you are using. Example, this is how it is done in Mockito: Blob blob = mock(Blob.class);

Upvotes: 3

Related Questions