Spock test for java code by mocking jdbc - unit-testing

i have a class as follows-
#Repository
public class TableImpl implements Table{
#Autowired
DataSource dataSource;
#Autowired
DataSource dataSource2;
private static List<Object> tableInfo;
public List<Object> getTableInfo() {
return tableInfo;
}
public void setTableInfo(List<Object> tableInfo) {
this.tableInfo = tableInfo;
}
public List<String> fetchColumns(BasicDataSource dataSource){
JdbcTemplate jdbctemplate = new JdbcTemplate(dataSource);
List<Map<String, Object>> columnMap = jdbctemplate.queryForList("show columns from " + tableInfo.get(0).toString());
List<String> columns = new ArrayList<String>();
for (Map<String, Object> column: columnMap)
{
columns.add(column.get("FIELD").toString());
}
return columns;
}}
I wanted to write a test for method fetchColumns here in spock.
This is what I have tried but this is not working--
class TableImplSpec extends spock.lang.Specification{
JdbcTemplate jdbcTemplate=Mock()
DataSource dataSource1=Mock()
def List<Object> tableInfo=[1]
def BasicDataSource dataSourceValue
def BasicDataSource dataSource
def TableImpl obj=new TableImpl(
dataSource: dataSource1
)
def Table obj1=new TableImpl(
dataSource: dataSource1,tableInfo: tableInfo
)
List<Map<String, Object>> recordsList
def "fetch columns"()
{
given:
TableImpl TableImplMock = Mock()
def Table obj2=new TableImpl(
dataSource: dataSource1,tableInfo: tableInfo
)
tableInfo.add(0,"promptpaySched")
tableInfo.add(1,"promptpaySchedID")
tableInfo.add(2,"name,lastname")
tableInfo.add(3,1)
jdbcTemplate.queryForList(_ as String)>> null
when:
obj2.fetchColumns(dataSource)
then:
1*jdbcTemplate.queryForList(_ as String)
}}
the error I am getting is---
IllegalArgumentException: Property 'dataSource' is required. I am totally confused what changes I have to make to my test class.

If you are writing tests for code that is interacting with the database, (i.e. reads data from or stores data into it) you might need to adjust your tests to pass actual dataSource (not mocked one).
Else try to mock at Repository level and use it to return expected objects.

Related

Mock DbQuery to use FromSql from unit tests

I am using EF Core in a projet to get stored procedure calling. In my context i have the following :
public class MyContext : DbContext
{
public DbQuery<User> UserQuery { get; set; }
public MyContext(DbContextOptions<MyContext> options) : base(options) { }
}
And i call the stored procedure like this :
public virtual async Task<User> GetUserAsync(string name)
{
return await MyContext.Query<User>()
.FromSql($"EXEC [dbo].[GetUser], #Login = {name}")
.FirstOrDefaultAsync();
}
Code is working fine. I need to test this method in unit tests, i'm using InMemoryDatabase to mock my context MyContext like this :
[Fact]
public async Task GetUserAsync_should_return_first_user_with_login_and_password_if_exists()
{
// Arrange
var users = new List<User>
{
new User()
{
Login = "test#outlook.fr",
Password = "pass1",
},
};
var options = new DbContextOptionsBuilder<MyContext>()
.UseInMemoryDatabase(databaseName: "BddName")
.Options;
var context = new MyContext(options);
var loginProvider = A.Fake<LoginProvider>(opts => opts.WithArgumentsForConstructor(() => new LoginProvider(context)));
// Act
// Assert
context.Dispose();
}
And i have no idea how can i set my list into the result of the stored procedure called from DbQuery. I tried to follow this article : https://nodogmablog.bryanhogan.net/2017/11/unit-testing-entity-framework-core-stored-procedures/ but it works for DbSet only and not DbQuery.
I need some advices for this case.
Thanks in advance.
The link in the OP does apply to the DbQuery type as well, as you're mocking the provider. Both DbSet and DbQuery work in the same way in this regard.
See https://stackoverflow.com/a/56940311/2975810 for a previous answer on the topic.

NullPointerException: Cannot get property 'template' on null object in Groovy

I got NPE with the following code. How to initiate the list properly? I want to iterate the list and use the value to create different names
class Comp {
private List<String> testSuite
Comp(List<String> testcase){
this.testSuites = testcase
}
Job start() {
for (String s : testCase) {
Job.newInstance()
.withName("T/TESTS/"+ s)
}
}
def testCase = [
'name1', 'name2'
]
}
Comp.newInstance().start()
I think :
private List<String> testSuite = []
is enough and have no problem with this implementation.
Or you can do it in java way like shown below,
private List<String> testSuite = new ArrayList<>();

junit test : java.lang.ClassCastException

I am trying to implement junit testing with spring data jpa application. On controller level I am trying to implement unit testing. But I am getting Test failure class cast exception error.
DepartmentController.java
#RestController
#RequestMapping("/api.spacestudy.com/SpaceStudy/Control/SearchFilter")
public class DepartmentController {
#Autowired
DepartmentService depService;
#CrossOrigin(origins = "*")
#GetMapping("/loadDepartments")
public ResponseEntity<Set<Department>> findDepName() {
Set<Department> depname = depService.findDepName();
return ResponseEntity.ok(depname);
}
}
Junit test class
#RunWith(SpringRunner.class)
#WebMvcTest(DepartmentController.class)
public class SpaceStudyControlSearchFilterApplicationTests {
#Autowired
DepartmentController depController;
#Autowired
private MockMvc mockMvc;
#MockBean
DepartmentService depService;
#SuppressWarnings("unchecked")
Set<Department> mockDepartment = (Set<Department>) new Department(21629, "170330", "Administrative Computer");
#Test
public void findDepNameTest() throws Exception {
Mockito.when(depService.findDepName()).thenReturn( mockDepartment);
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(
"/api.spacestudy.com/SpaceStudy/Control/SearchFilter/loadDepartments").accept(
MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
System.out.println(result.getResponse());
String expected = "{nDeptId: 21629}";
JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false);
}
}
Junit failure
java.lang.ClassCastException: com.spacestudy.model.Department cannot be cast to java.util.Set
at com.spacestudy.SpaceStudyControlSearchFilterApplicationTests.<init>(SpaceStudyControlSearchFilterApplicationTests.java:39)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
I am new to junit testing. Can any one tell me what I am doing wrong in test?
You are trying to cast a Department to Set<Department> at this line:
Set<Department> mockDepartment = (Set<Department>) new Department(21629, "170330", "Administrative Computer");
This cannot work. Instead you should create an empty set and then add the department, i.e. like this:
Set<Department> mockDepartment = new HashSet<Department>() {{
add(new Department(21629, "170330", "Administrative Computer"));
}};

Unit testing when Database is involved

I'm quite new to unit testing and I need an hand to understand if I'm doing things in the correct way. My major problem is regarding the DB testing... Here's my code then I'll expose my perplexities
Consider this class that's an item of a pipeline I've to perform
public class RetrieveApplicationUsernamePipelineStep : IPipelineStep
{
public const string RetrieveApplicationUsernameKey = "RetrieveApplicationUsername";
private readonly IRetrieveApplicationUserRepository repository;
public int Order => 3;
public string Name => RetrieveApplicationUsernameKey;
public RetrieveApplicationUsernamePipelineStep(IRetrieveApplicationUserRepository repository)
{
this.repository = repository;
}
public async Task<IDictionary<string, object>> Action(IDictionary<string, object> context)
{
string res = await repository.GetApplicationUser(context);
context[Resources.ApplicationUser] = res;
return context;
}
}
I wrote the following tests
[TestFixture]
public class RetrieveApplicationUsernamePipelineStepTests
{
private IRetrieveApplicationUserRepository retrieveApplicationUserRepository;
[OneTimeSetUp]
public void Start()
{
var configuration = new ConfigurationFromConfigFile();
retrieveApplicationUserRepository = new RetrieveApplicationUserRepository(configuration);
}
[Test]
public async Task ActionSuccessfullyCompleted()
{
var context = new Dictionary<string, object>();
var repository = Substitute.For<IRetrieveApplicationUserRepository>();
repository.GetApplicationUser(context).Returns("user1");
var pipeline = new RetrieveApplicationUsernamePipelineStep(repository);
var res = await pipeline.Action(context);
Assert.IsNotNull(res[Resources.ApplicationUser]);
Assert.IsNotEmpty((string)res[Resources.ApplicationUser]);
}
[Test]
public void ActionFailingCompleted()
{
var context = new Dictionary<string, object>();
var repository = Substitute.For<IRetrieveApplicationUserRepository>();
repository.GetApplicationUser(context).Throws(new UserMappingNotFoundException());
var pipeline = new RetrieveApplicationUsernamePipelineStep(repository);
Assert.ThrowsAsync<UserMappingNotFoundException>(async () => await pipeline.Action(context));
}
[Test]
public void NameTest()
{
var pipeline = new RetrieveApplicationUsernamePipelineStep(retrieveApplicationUserRepository);
Assert.IsTrue(pipeline.Name == RetrieveApplicationUsernamePipelineStep.RetrieveApplicationUsernameKey);
}
[Test]
public void OrderTest()
{
var pipeline = new RetrieveApplicationUsernamePipelineStep(retrieveApplicationUserRepository);
Assert.IsTrue(pipeline.Order == 3);
}
}
And those test works fine since for ActionSuccessfullyCompleted and ActionFailingCompleted I substitute the IRetrieveApplicationUserRepository's result with my expected one.
The real implementation of ther repository is
public class RetrieveApplicationUserRepository : IRetrieveApplicationUserRepository
{
#region Variables
private readonly IConfiguration configuration;
#endregion
#region Ctor
public RetrieveApplicationUserRepository(IConfiguration configuration)
{
this.configuration = configuration;
}
#endregion
#region IRetrieveApplicationUserRepository
public async Task<string> GetApplicationUser(IDictionary<string, object> context)
{
if (configuration.AppSettings[Resources.ApplicationUserFromDomainUserKey] == null)
throw new KeyNotFoundException(Resources.ApplicationUserFromDomainUserKey);
if (string.IsNullOrEmpty(configuration.ConnectionString))
throw new NullReferenceException();
string storedProcedure = configuration.AppSettings.Get(Resources.ApplicationUserFromDomainUserKey);
string result;
using (var sqlConnection = new SqlConnection(configuration.ConnectionString))
{
using (var sqlCommand = new SqlCommand(storedProcedure, sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("#DOMAINUSER", context[Resources.DomainUser]);
sqlCommand.Parameters.AddWithValue("#DOMAIN", context[Resources.DomainName]);
sqlCommand.Parameters.AddWithValue("#APPID", context[Resources.ApplicationId]);
sqlConnection.Open();
result = (string)await sqlCommand.ExecuteScalarAsync();
}
}
if (result == null)
throw new UserMappingNotFoundException();
return result;
}
#endregion
}
Here're the questions :
Are the test I wrote correct?
I've seen using Resharper's Code Coverage that it wants me to test-cover the properties...is there a way I can avoid this? is this test meaningful?
What's your approach when you've to unit test component that're related to DB? Have you got a real-db that's used for test? Consider that the real DB is about 10Gb so I don't want to have a copy as mdf (condider I can have this) just to test a small portion of the DB
Talking with my colleagues they told me to use test just for TDD while I wish to use them to avoid regressions
Going back to the DB question, I don't want to have a test where I write if username is "John" and maybe tomorrow John user won't be present in the DB anymore,so that the test I expect to pass wont' pass anoymore
The usual approach is to isolate the database side with an abstraction, so you can provide a test dummy (mock, fake, etc) of that abstraction. Only test an actual database when you do the integration testing.
For the tests of database stored procedures, you may well want a different test harness, creating a new test database in memory (equivalently, in a RAM-backed filesystem). You only need to populate enough data for the individual test (we're doing functional testing here, not performance testing), and you may be able to retain table structure across tests with judicious use of rollback.
I have done this, but it's some time ago, so I'll refrain from giving examples that may be no longer state-of-the-art (even if the code does still exist, and if I could find it).

JSON rendering fails for domain objects in ControllerUnitTestCase

I'm writing a unit test for a Grails controller that renders a domain class to a JSON response:
class MyController {
def find = {
def domainInst = MyDomainClass.get(params.id)
render ([data: domainInst] as JSON)
}
}
The unit test extends ControllerUnitTestCase and provides a mock for the domain object:
class MyControllerTests extends ControllerUnitTestCase {
#Before
void setUp() {
super.setUp()
mockDomain(MyDomainClass, [new MyDomainClass(id: 7)])
}
#Test
void testFind() {
def inst = MyDomainClass.get(7)
controller.params.id = inst.id
controller.find()
assert(controller.response.json.data.id == inst.id)
}
This all seems to be working nicely except for the JSON rendering, which spits out a nasty stack trace:
| Failure: testFind(MyControllerTests)
| org.apache.commons.lang.UnhandledException:
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class MyDomainClass
Caused by: org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class MyDomainClass
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.convertAnother(JSON.java:162)
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.render(JSON.java:134)
... 5 more
Caused by: java.lang.reflect.InvocationTargetException
... 9 more
Caused by: groovy.lang.MissingMethodException: No signature of method: MyDomainClass.isAttached() is applicable for argument types: () values: []
Possible solutions: isAttached(), attach()
... 9 more
Changing the return to a Map instead of a domain class works:
render ([data: [id: domainInst.id]] as JSON)
What's causing the JSON marshaller to die on the domain class? It works in a normal environment, but not in the mock test environment. Is there a way to make this test work?
Looks like you might need to do some fine tuning to make the converters realize that you're trying to render a domain class as a JSON object. It works when you manually put your id into a map because it is rendering the response from a Map object instead of a Grails domain class, which needs to go through a special ObjectMarshaller.
Something like this:
// Domain Class
class Foo {
String foo
}
// Controller class
class MyController {
def find = {
def domainInst = Foo.get(params.id)
render domainInst as JSON
}
}
// Controller Test Class
class MyControllerTests extends ControllerUnitTestCase {
static application
#Before
void setUp() {
super.setUp()
// Register some common classes so that they can be converted to XML, JSON, etc.
def convertersInit = new ConvertersConfigurationInitializer()
convertersInit.initialize(application)
[ List, Set, Map, Errors ].each { addConverters(it) }
def xmlErrorMarshaller = new ValidationErrorsMarshaller()
XML.registerObjectMarshaller(xmlErrorMarshaller)
def jsonErrorMarshaller = new ValidationErrorsMarshaller()
JSON.registerObjectMarshaller(jsonErrorMarshaller)
ApplicationHolder.application.addArtefact("Domain", Foo)
mockDomain(Foo, [new Foo(foo: "foo")] )
}
#Test
void testJSON() {
def inst = Foo.list()[0]
controller.params.id = inst.id
def model = controller.find()
assert controller.response.json.foo == "foo"
}
#Override
protected def bindMockWebRequest(GrailsMockHttpServletRequest mockRequest, GrailsMockHttpServletResponse mockResponse) {
MockApplicationContext ctx = new MockApplicationContext()
application = new DefaultGrailsApplication([testClass] as Class[], getClass().classLoader)
application.initialise()
ctx.registerMockBean("grailsApplication", application)
ctx.registerMockBean(testClass.name, testClass.newInstance())
def lookup = new TagLibraryLookup(applicationContext: ctx, grailsApplication: application)
lookup.afterPropertiesSet()
ctx.registerMockBean("gspTagLibraryLookup", lookup)
ctx.registerMockBean(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService())
mockRequest.servletContext.setAttribute(ApplicationAttributes.APPLICATION_CONTEXT, ctx)
mockRequest.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx)
webRequest = new GrailsWebRequest(mockRequest, mockResponse, mockRequest.servletContext)
mockRequest.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, webRequest)
RequestContextHolder.setRequestAttributes(webRequest)
}
}
Hope this helps!