Skip to content

Commit

Permalink
Merge pull request ballerina-platform#40251 from Dilhasha/call-original
Browse files Browse the repository at this point in the history
Verify member variable is public before mocking
  • Loading branch information
Dilhasha committed Apr 27, 2023
2 parents 8acc1b7 + 08c9ec1 commit 56add9c
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class MockConstants {
public static final String INVALID_MOCK_OBJECT_ERROR = "InvalidObjectError";
public static final String FUNCTION_SIGNATURE_MISMATCH_ERROR = "FunctionSignatureMismatchError";
public static final String INVALID_MEMBER_FIELD_ERROR = "InvalidMemberFieldError";
public static final String NON_PUBLIC_MEMBER_FIELD_ERROR = "NonPublicMemberFieldError";

public static final String FUNCTION_CALL_ERROR = "FunctionCallError";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.ballerina.runtime.api.PredefinedTypes;
import io.ballerina.runtime.api.TypeTags;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.flags.SymbolFlags;
import io.ballerina.runtime.api.types.Field;
import io.ballerina.runtime.api.types.MethodType;
import io.ballerina.runtime.api.types.ObjectType;
Expand Down Expand Up @@ -282,7 +283,12 @@ public static BError thenReturn(BObject caseObj) {

// register return value for member field
String fieldName = caseObj.getStringValue(StringUtils.fromString("fieldName")).toString();

if (!validateFieldAccessIsPublic(objectType, fieldName)) {
String detail = "member field should be public to be mocked. " +
"The provided field '" + fieldName + "' is not public";
return ErrorCreator.createError(StringUtils.fromString(MockConstants.NON_PUBLIC_MEMBER_FIELD_ERROR),
StringUtils.fromString(detail));
}
if (!validateFieldValue(returnVal, objectType.getFields().get(fieldName).getFieldType())) {
String detail = "return value provided does not match the type of '" + fieldName + "'";
return ErrorCreator.createError(
Expand All @@ -297,6 +303,10 @@ public static BError thenReturn(BObject caseObj) {
return null;
}

private static boolean validateFieldAccessIsPublic(ObjectType objectType, String fieldName) {
return SymbolFlags.isFlagOn(objectType.getFields().get(fieldName).getFlags(), SymbolFlags.PUBLIC);
}

/**
* Registers a sequence of return values to the case provided.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ public void testObjectMocking_NegativeCases(String message, String test, String
AssertionUtils.assertOutput(baseOutputFile, output);
}

@Test
public void testObjectMocking_NonPublicField() throws BallerinaTestException, IOException {
String[] args = mergeCoverageArgs(new String[]{"--tests",
"non_public_field_mock:testNonPublicMemberFieldMock"});
String output =
balClient.runMainAndReadStdOut("test", args, new HashMap<>(),
projectBasedTestsPath.resolve("non-public-field-mock").toString(), false);
String firstString = "Generating Test Report\n\t";
String endString = "project-based-tests";
output = CommonUtils.replaceVaryingString(firstString, endString, output);
AssertionUtils.assertOutput("MockTest-testObjectMocking_NegativeCases8.txt", output);
}

@DataProvider(name = "testNegativeCases")
public static Object[][] negativeCases() {
return new Object[][] {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Compiling source
intg_tests/non_public_field_mock:0.0.0

Running Tests with Coverage

non_public_field_mock

[fail] testNonPublicMemberFieldMock:

member field should be public to be mocked. The provided field 'url' is not public



0 passing
1 failing
0 skipped

Generating Test Report
*****project-based-tests/non-public-field-mock/target/report/test_results.json

error: there are test failures
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Compiling source
intg*****project-based-tests\non-public-field-mock\target\report\test_results.json

error: there are test failures
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "intg_tests"
name = "non_public_field_mock"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2023 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

public client class BasicHttpClient {

string url = "";

remote function get(string path) returns string {
return self.url + path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2023 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/test;
import non_public_field_mock.TestClient;

@test:Config {}
function testNonPublicMemberFieldMock() {
TestClient:BasicHttpClient mockBasicClient = test:mock(TestClient:BasicHttpClient);
test:prepare(mockBasicClient).getMember("url").thenReturn("tmp");
}

0 comments on commit 56add9c

Please sign in to comment.