I have some tests in error after upgrading from Spring boot 2.5.6 to 2.7.3.
For information we use Oracle for the database and h2 for tests.
I have some tests in failure with the following error:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException
In fact, the version of h2 was 1.4.200 before and is 2.1.214 now and a lot of things seem to have changed. The reason of the error is not always the same according to the test in error. Sometimes it is an error with a table not found (not solved yet), sometimes it is an error with "Values of types "BOOLEAN" and "INTEGER" are not comparable" (solved by updating a query where a comparison was done with a boolean column like this myBoolean = 0 and it has been updated to myBoolean = false) and I also have an error on a query done with a PageRequest.
For this last case, I have a Controller like this:
public Page<MyEntity> doSomething() {
final Sort sort = Sort.by(Order.desc("column1"));
final PageRequest pageRequest = PageRequest.of(0, 1000, sort);
return myEntityRepository.findAll(pageRequest);
}
But I have an error like that:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "select myentity0_.id as id1_47_, myentity0_.column1 as column1_47_, myentity0_.column2 as column2_47_ from my_table myentity0_ order by myentity0_.column1 desc [*]limit ?"; SQL statement:
select myentity0_.id as id1_47_, myentity0_.column1 as column1_47_, myentity0_.column2 as column2_47_ from my_table myentity0_ order by myentity0_.column1 desc limit ? [42000-214]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:502)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:477)
at org.h2.message.DbException.get(DbException.java:223)
at org.h2.message.DbException.get(DbException.java:199)
at org.h2.message.DbException.getSyntaxError(DbException.java:247)
at org.h2.command.Parser.getSyntaxError(Parser.java:898)
at org.h2.command.Parser.prepareCommand(Parser.java:572)
at org.h2.engine.SessionLocal.prepareLocal(SessionLocal.java:631)
at org.h2.engine.SessionLocal.prepareCommand(SessionLocal.java:554)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1116)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:92)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:288)
at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:337)
at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:149)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:176)
... 205 more
If I change the Controller like this,the test is in success:
public Page<MyEntity> doSomething() {
List<MyEntity> result = myEntityRepository.findAll();
return new PageImpl<MyEntity>(result);
}
So It seems that the problem was due by the use of PageRequest.
Do you have an idea please?
Java persistence libraries are usually tested only with default Regular mode of H2 and may not work well with other modes.
Oracle doesn't support MySQL/PostgreSQL-style LIMIT, and H2 doesn't allow it in Oracle compatibility mode, but some libraries produce LIMIT instead of standard OFFSET / FETCH for H2.
Spring Data JDBC (spring-data-relational) added support of custom compatibility modes of H2 only about a month ago and version 2.4.3 with this fix isn't released yet.
Hibernate ORM 6.*.* should work well, but Hibernate ORM 5.6.* has a known issue:
https://hibernate.atlassian.net/jira/software/c/projects/HHH/issues/HHH-15318
You can enable LIMIT in Oracle compatibility mode of H2 as a temporary workaround. To do that, you need to execute the following Java code during initialization of your application:
org.h2.engine.Mode mode = org.h2.engine.Mode.getInstance("ORACLE");
mode.limit = true;
I am trying two run two tests on a single Test Class in Laravel dusk. It passes when ran individually but fails when ran as part of a suite.
Here, my first test passes, but second test is failing.
Exception looks like:
There was 1 error:
1) Tests\Browser\Front\JobApplyTest::jobseeker_can_apply_to_a_job
Illuminate\Contracts\Container\BindingResolutionException: Target class [env] does not exist.
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:879
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:758
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:851
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:694
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:836
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:1423
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:571
/home/ellite/code/labs/jagirhouse/app/Core/Providers/TelescopeServiceProvider.php:26
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:279
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:61
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:323
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:281
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:330
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:475
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Watchers/QueryWatcher.php:48
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:404
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:249
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:887
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:728
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:683
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:502
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php:109
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:364
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:227
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
/home/ellite/code/labs/jagirhouse/database/migrations/2014_10_12_000000_create_users_table.php:46
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:394
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:403
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:202
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:167
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:112
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:85
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:585
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:94
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Util.php:40
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:653
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Command/Command.php:298
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php:68
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php:28
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:55
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Util.php:40
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:653
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Command/Command.php:298
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:1005
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:299
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:171
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Application.php:94
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Application.php:186
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:263
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:260
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:413
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:66
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:19
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:126
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:91
/home/ellite/code/labs/jagirhouse/vendor/laravel/dusk/src/TestCase.php:23
Caused by
ReflectionException: Class "env" does not exist
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:877
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:758
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:851
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:694
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:836
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:1423
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:571
/home/ellite/code/labs/jagirhouse/app/Core/Providers/TelescopeServiceProvider.php:26
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:279
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:61
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:323
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:281
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:330
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Telescope.php:475
/home/ellite/code/labs/jagirhouse/vendor/laravel/telescope/src/Watchers/QueryWatcher.php:48
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:404
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:249
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:887
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:728
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:683
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Connection.php:502
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php:109
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:364
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:227
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
/home/ellite/code/labs/jagirhouse/database/migrations/2014_10_12_000000_create_users_table.php:46
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:394
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:403
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:202
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:167
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:112
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:85
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:585
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:94
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Util.php:40
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:653
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Command/Command.php:298
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php:68
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php:28
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php:55
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Util.php:40
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Container/Container.php:653
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:136
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Command/Command.php:298
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:1005
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:299
/home/ellite/code/labs/jagirhouse/vendor/symfony/console/Application.php:171
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Application.php:94
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Console/Application.php:186
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:263
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:260
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:413
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:66
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseMigrations.php:19
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:126
/home/ellite/code/labs/jagirhouse/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:91
/home/ellite/code/labs/jagirhouse/vendor/laravel/dusk/src/TestCase.php:23
Automated tests are supposed to be side-effect free. That means well-designed tests should be possible to execute in any order and still receive the same results per test.
So when you have a test which passes or fails depending on other tests, then your tests are not properly isolated.
Find out what pre-conditions a test assumes (like specific data being in a specific database table or specific global variables hold a specific value) and ensure that your pre-conditions are fulfilled before the actual test is performed.
I am trying to create DB2 XA Datasource using JBoss-cli using the following command.
/profile=full-ha/subsystem=datasources/xa-data-source=DB2DSXA3:add(jndi-name="java:/DB2DSXA3",connection-url="jdbc:db2://localhost:50000/TESTDB",driver-name=db2jccxa,is-same-rm-override="false",user-name=sambati,password=Summi135#,recover-plugin-class-name="org.jboss.jca.core.recovery.ConfigurableRecoveryPlugin",valid-connection-checker-class-name="org.jboss.jca.adapters.jdbc.extensions.db2.DB2ValidConnectionChecker",stale-connection-checker-class-name="org.jboss.jca.adapters.jdbc.extensions.db2.DB2StaleConnectionChecker",exception-sorter-class-name="org.jboss.jca.adapters.jdbc.extensions.db2.DB2ExceptionSorter")
Looks like some mandatory fields are missing. i am getting the following error message:
connection-url' is not found among the supported properties: [allocation-retry, allocation-retry-wait-millis, allow-multiple-users, authentication-context, background-validation, background-validation-millis, blocking-timeout-wait-millis, capacity-decrementer-class, capacity-decrementer-properties, capacity-incrementer-class, capacity-incrementer-properties, check-valid-connection-sql, connectable, connection-listener-class, connection-listener-property, credential-reference, driver-name, elytron-enabled, enabled, enlistment-trace, exception-sorter-class-name, exception-sorter-properties, flush-strategy, idle-timeout-minutes, initial-pool-size, interleaving, jndi-name, max-pool-size, mcp, min-pool-size, new-connection-sql, no-recovery, no-tx-separate-pool, pad-xid, password, pool-fair, pool-prefill, pool-use-strict-min, prepared-statements-cache-size, query-timeout, reauth-plugin-class-name, reauth-plugin-properties, recovery-authentication-context, recovery-credential-reference, recovery-elytron-enabled, recovery-password, recovery-plugin-class-name, recovery-plugin-properties, recovery-security-domain, recovery-username, same-rm-override, security-domain, set-tx-query-timeout, share-prepared-statements, spy, stale-connection-checker-class-name, stale-connection-checker-properties, statistics-enabled, track-statements, tracking, transaction-isolation, url-delimiter, url-property, url-selector-strategy-class-name, use-ccm, use-fast-fail, use-java-context, use-try-lock, user-name, valid-connection-checker-class-name, valid-connection-checker-properties, validate-on-match, wrap-xa-resource, xa-datasource-class, xa-resource-timeout]
can some one help me please.
The error indicates that the connection-url is not a property on an xa-data-source resource. The connection URL will need to be added as a property in a batch command.
Here's an example MySQL CLI command:
module add --name=com.mysql --resources=~/Downloads/mysql-connector-java-5.1.37/mysql-connector-java-5.1.37-bin.jar --dependencies=javax.api,javax.transaction.api
batch
/subsystem=datasources/jdbc-driver=com.mysql:add(driver-name=com.mysql, driver-module-name=com.mysql, driver-xa-datasource-class-name=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource)
/subsystem=datasources/xa-data-source=mysql:add(driver-name=com.mysql, jndi-name="java:/jdbc/MySQLXA", enabled=true)
/subsystem=datasources/xa-data-source=mysql/xa-datasource-properties=URL:add(value="jdbc:mysql://localhost:3306/temp?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8")
run-batch
I'm trying to mock the Cache::put() facade. But it gives me an error. I have tried different ways but couldn't figure it out.
public function testGetAllFromDatabase()
{
$industry = new Industry();
Cache::shouldReceive('has')
->once()
->with('industries.all')
->andReturn(false);
Cache::shouldReceive('put')
->with('industries.all', '', 0)
->andReturn(true);
$this->industryMock
->shouldReceive('all')
->once()
->andReturn(array_reverse($this->industries));
$this->app->instance(Industry::class, $this->industryMock);
$industryRepository = new IndustryRepository();
$all = $industryRepository->all();
dd($all);
$this->assertContains( $this->industries[2], $all);
}
But when I execute it the following error is occurring.
$ vendor/bin/phpunit
PHPUnit 7.2.7 by Sebastian Bergmann and contributors.
...E 4 / 4 (100%)
Time: 3.76 seconds, Memory: 12.00MB
There was 1 error:
1) Tests\Unit\RepositoriesTests\IndustryRepositoryTest::testGetAllFromDatabase
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_1_Illuminate_Cache_CacheManager::put('industries.all', object(Illuminate\Database\Eloquent\Collection), '1440'). Either the method was unexpected or its arguments matched no expected argument list for this method
Objects: ( array (
'Illuminate\\Database\\Eloquent\\Collection' =>
array (
'class' => 'Illuminate\\Database\\Eloquent\\Collection',
'properties' =>
array (
),
),
))
F:\development\consulting.local\src\vendor\mockery\mockery\library\Mockery\ExpectationDirector.php:92
F:\development\consulting.local\src\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:223
F:\development\consulting.local\src\app\Repositories\IndustryRepository.php:30
F:\development\consulting.local\src\tests\Unit\RepositoriesTests\IndustryRepositoryTest.php:81
I have tried many ways but couldn't get it to fix. Thank you.
Since it may help others, Laravel's facade includes helper functions that allow swapping then with a Mockery test double
This means that when you use a shouldReceive you can chain it with any Mockery expectation for example in this case if you don't care about some parameters you can use:
Cache::shouldReceive('put')
->with('industries.all', \Mockery::any(), \Mockery::any())
->andReturn(true);
In case it helps others, it's good to point out that you may not want to mock Cache, but instead actually use the real Cache.
That's because it's a Cache for testing only:
When running tests via vendor/bin/phpunit, Laravel [....] automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.
https://laravel.com/docs/8.x/testing#environment
Note that unlike the OP's test, you may need to follow Laravel's guidance about your test class extending their TestCase to get the behavior, e.g.
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
I'm trying to work through the [Elastisch tutorial] to create some test data in an ElasticSearch instance running on a VM.
I am running this code:
(ns content-rendering.core
(:require [clojurewerkz.elastisch.native :as esr]
[clojurewerkz.elastisch.native.index :as esi]))
(defn populate-test-data
[]
(let [conn (esr/connect "http://10.10.10.101:9200")]
(esi/create conn "test")))
(populate-test-data)
And I am seeing the following exception when I try and execute the code in the namespace using either Cider in emacs or from a Leiningen repl:
Caused by java.lang.UnsupportedOperationException
nth not supported on this type: Character
RT.java: 933 clojure.lang.RT/nthFrom
RT.java: 883 clojure.lang.RT/nth
native.clj: 266 clojurewerkz.elastisch.native/connect
core.clj: 7 content-rendering.core/populate-test-data
core.clj: 10 content-rendering.core/eval5078
If I require the Elastisch namespaces into a repl and run something like the following, it works fine:
(def conn (esr/connect "http://10.10.10.101:9200"))
(esi/create conn "test") ; {:acknowledged true}
Any ideas what I'm missing here?
There are two clients in elastisch, the REST one and the native one. You're using the native transport, but passing it the REST URL when it expects a seq of [host port] pairs.
You can switch to the REST client by changing esr/esi to their clojurewerkz.elastisch.rest pendants, or point the native one to the correct endpoints:
(esr/connect [["10.10.10.101" 9300]])
If your cluster name is not the default you have to set it using an additional options map:
(esr/connect [["10.10.10.101" 9300]] {"cluster.name" "my-es"})