I want to return a filled Map with my mocked Object, but the size of the Map is always Null. The mocked Object "CommandLineValues options" is not Null and also the Boolean variable "doCleanFirst" I can mock successfully.
Here is my Testclass:
#RunWith(MockitoJUnitRunner.class)
public class IndexBMECatTest {
#InjectMocks
private IndexBMECat classUnderTest;
#Mock
private CommandLineValues options;
#Test
public void testAccessoryItemHasNoDublicates() {
Map<String, String> testMap = new HashMap<>();
testMap.put("key", "value");
when(options.getCleanFirst()).thenReturn(false);
when(options.readWhitlist()).thenReturn(testMap);
classUnderTest.run();
}
}
Here is the constructor of my class where the code start, the tested Method is not relevant:
private boolean doCleanFirst;
private Map<String, String> whiteList;
public IndexBMECat(TransportClient client, CommandLineValues options, BMECatReader reader) throws Exception {
this.doCleanFirst = options.getCleanFirst();
this.whiteList = options.readWhitlist();
if (whiteList.isEmpty()) {
throw new Exception("Missing whiteList");
}
}
I also tried other variants:
Mock the Map and the return value of the method "isEmpty"
Initialize the Testclass and give the mocked Object to the constructor
But the whiteList has always the size = 0
Thx, this works now:
private IndexBMECat classUnderTest;
#Mock
private CommandLineValues options;
#Mock
private BMECatReader reader;
#Mock
TransportClient client;
#Before
public void setUp() throws Exception {
Map<String, String> testMap = new HashMap<>();
testMap.put("key", "value");
when(options.getCleanFirst()).thenReturn(false);
when(options.readWhitlist()).thenReturn(testMap);
classUnderTest = new IndexBMECat(client, options, reader);
}
#Test
public void testAccessoryItemHasNoDublicates() {
classUnderTest.run();
}
First I mock the methods which will be executed in the contructor and then I create the instance of my testclass.
Related
I want to write a unit test for a controller that uses a FormFactory object
My controller:
public class LoginController extends ApiController {
private final FormFactory formFactory;
#Inject
public LoginController(FormFactory formFactory) {
this.formFactory = formFactory;
}
public Result login(Http.Request request) {
DynamicForm form = formFactory.form().bindFromRequest(request);
String username = form.get("username");
String password = form.get("password");
doSomething();
return result;
}
}
In my test class I'll have to mock the external dependency FormFactory and to stub .form() and .bindFromRequest(request) as well, but I don't know how.
My test class is:
public class LoginControllerTest {
#InjectMocks
private LoginController controller;
#Mock
private FormFactory formFactory;
#Before
public void setup() {
MockitoAnnotations.openMocks(this);
}
#Test
public void login_ok() {
Map<String, String> formData = new HashMap<>();
formData.put("username", "user");
formData.put("password", "pass");
Http.RequestBuilder fakeRequest = new Http.RequestBuilder().method(Helpers.POST).bodyForm(formData);
when(formFactory.form()).thenReturn(new DynamicForm(???));
Result result = controller.adminLogin(fakeRequest.build());
}
}
If I test my class I got a NPE because .form() is null in LoginController class.
How Can I solve this issue?
What parameters I have to pass in new DynamicForm(???)?
Hi I am trying to initialize a field with #PostConstruct method but in tests, this method does not fill the bidiMap field.
Is there any way to mock bidiMap field which is a field of DataSource ?
#Component
#Getter
#Setter
#NoArgsConstructor
public class DataSource {
public DualHashBidiMap<String, String> bidiMap = new DualHashBidiMap();
#PostConstruct
public void loadData() throws IOException {
Map<String, String> data = new ObjectMapper().readValue(new File("src/main/resources/static/data.json"), Map.class);
data.entrySet().stream().forEach(item -> this.bidiMap.put(item.getKey(), item.getValue()));
}
}
Test:
#Test
void shouldReturnPrettifiedUrl_WithQueryParams() throws IOException {
// Given
List<String> inputParameters = Arrays.asList("/products?gender=female&tag=123&tag=1234");
HashMap<String, String> expectedResult = PrettyUrlResponseBuilder.PrettyUrlResponseBuilder().createPrettyUrlResponseWithParams();
HashMap<String, String> databaseResponse = DatabaseResponse.DatabaseResponse().createDatabaseResponse();
System.out.println(dataSource);
// When
Map<String, String> actualResult = prettyUrlService.toPretty(inputParameters);
// Then
assertEquals(expectedResult, actualResult);
}
The class being tested:
#Service
public class PrettyUrlService {
private final DataSource dataSource;
public PrettyUrlService(DataSource dataSource) {
this.dataSource = dataSource;
}
public Map<String, String> toPretty(List<String> urlList) throws IOException {
var data = dataSource.getBidiMap(); // Here, data is null.
// ...
}
}
If you're unit testing the PrettyUrlService class then you should not need to also test the underlying DataSource class within the same test. Simply mocking the DataSource interactions and verifying that all the expected interactions are happening should be more than enough.
E.g. (assuming JUnit 5):
#ExtendWith(MockitoExtension.class)
class PrettyUrlServiceTest {
#Mock
private DataSource dataSource;
private PrettyUrlService prettyUrlService;
#BeforeEach
void setUp() {
prettyUrlService = new PrettyUrlService(dataSource);
}
#Test
void exampleTest() {
DualHashBidiMap<String, String> bidiMap = new DualHashBidiMap();
// add test data to the map
bidiMap.put("testKey", "testValue"); // replace with data you need for test
Mockito.when(dataSource.getBidiMap()).thenReturn(bidiMap);
// insert test logic here (prettyServiceUrl call + assertions)
// ...
// finally, verify that dataSource.getBidiMap() was called exactly once
Mockito.verify(dataSource).getBidiMap();
}
}
I am having a protected method inside a static child class. I am running a testcase , its getting successful but code coverage is not increasing.
public class A{
private static final String var1 = "key1";
protected static class B extends OsCmd{
private String abc1;
private String abc2;
protected B(String xyz, String xyz2) {
this.abc1 = xyz;
this.abc2 = xyz2;
}
#Override
protected void updateEnv(Map<String, String> env) {
env.put(VAR1, "FALSE");
env.put(VAR2, "TRUE");
env.put("key3", abc1);
env.put("key4", abc2);
}
}
}
Below is my test case
#ExtendWith(MockitoExtension.class)
public class ATest {
private A mockA;
#BeforeEach
public void setup() {
mockA = Mockito.spy(A.class);
}
#Test
public void test2() {
try (MockedConstruction mockedConstruction =
mockConstruction(A.B.class)) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
A.B mockB =
new A.B("a", "b");
//doNothing().when(mockB).updateEnv(map);
mockB.updateEnv(map);
}
}
}
Can someone please help here, what mistake i am doing?
When you mock the constructor, then all internal method calls are also mocked and do not go through the actual code.
If you remove the following try-with-resources:
try (MockedConstruction mockedConstruction =
mockConstruction(A.B.class))
The real code will be executed and the coverage will increase.
Below is main code consist of one util class and service class using it
#PropertySource("classpath:atlas-application.properties")
public class ApacheAtlasUtils {
#Value("${atlas.rest.address}")
private String atlasURL;
#Value("${atlas.rest.user}")
private String atlasUsername;
#Value("${atlas.rest.password}")
private String atlasPassword;
private AtlasClientV2 client;
public AtlasClientV2 createClient() {
if (client == null) {
return new AtlasClientV2(new String[] {atlasURL}, new String[] {atlasUsername, atlasPassword});
} else {
return client;
}
}
}
Service Class is below :-
#Override
public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
throws AtlasServiceException {
// Some code
client = new ApacheAtlasUtils().createClient();
//some code
}
I am writing unit test for service method and I am getting exception for createClient method asking for values for url, username and password which should not happen as this should be mocked but the mocking is giving me below error
java.lang.IllegalArgumentException: Base URL cannot be null or empty.
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:141)
at org.apache.atlas.AtlasServerEnsemble.<init>(AtlasServerEnsemble.java:35)
at org.apache.atlas.AtlasBaseClient.determineActiveServiceURL(AtlasBaseClient.java:318)
at org.apache.atlas.AtlasBaseClient.initializeState(AtlasBaseClient.java:460)
at org.apache.atlas.AtlasBaseClient.initializeState(AtlasBaseClient.java:448)
at org.apache.atlas.AtlasBaseClient.<init>(AtlasBaseClient.java:132)
at org.apache.atlas.AtlasClientV2.<init>(AtlasClientV2.java:82)
at com.jlr.stratus.commons.utils.ApacheAtlasUtils.createClient(ApacheAtlasUtils.java:40)
at com.jlr.stratus.rest.service.impl.FileSearchService.findFilesWithPages(FileSearchService.java:49)
The Test code is as follows:-
private FileSearchService fileSearchService;
#Spy
private ApacheAtlasUtils apacheAtlasUtils;
#Mock
private AtlasClientV2 client;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
fileSearchService = new FileSearchService();
}
#Test
public void findFilesWithPages_searchAll() throws AtlasServiceException {
Mockito.doReturn(client).when(apacheAtlasUtils).createClient();
service.search(queryParent,pageable);
}
Your idea with spying is adequate (you can even go for mocking if you do not actually need any true implementation of that class).
The problem lies in the implementation:
// Some code
client = new ApacheAtlasUtils().createClient();
//some code
}
Instead of having the ApacheAtlasUtils as an instance variable (or a supplier method) you create the instance on the fly.
Mockito is not smart enough to catch that operation and replace the real object with you spy.
With the supplier method you can set up your test as follows:
#Spy
private FileSearchService fileSearchService = new FileSearchService();
#Spy
private ApacheAtlasUtils apacheAtlasUtils = new ApacheAtlasUtils();
#Mock
private AtlasClientV2 client;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
doReturn(apacheAtlasUtils).when(fileSearchService).getApacheUtils();
}
in your SUT:
#Override
public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
throws AtlasServiceException {
// Some code
client = getApacheUtils().createClient();
//some code
}
ApacheAtlasUtils getApacheUtils(){
return new ApacheAtlasUtils();
}
I have a final class with private static method which is invoked inside another static method
public final class GenerateResponse{
private static Map<String, String> getErrorDetails(JSONObject jsonObject) {
// implementation
}
public static String method1(params...){
Map<String, String> map = getErrorDetails(new JsonObject());
// implementation
}
}
I need to mock the private static method call getErrorDetails(), but my test is calling the actual method. Here is my code:
#RunWith(PowerMockRunner.class)
#PrepareForTest(GenerateResponse.class)
public class GenerateResponseTest{
#Test
public void testFrameQtcErrorResponse() throws Exception {
Map<String, String> errorDtls = new HashMap<String, String>();
PowerMockito.spy(GenerateResponse.class);
PowerMockito.doReturn(errorDtls).when(GenerateResponse.class, "getErrorDetails", JSONObject.class);
String response = GenerateResponse.method1(params...);
}
You should use an argument matcher in the whenmethod. I've modified your code a little bit to run the test case.
Actual method
public final class GenerateResponse{
private static Map<String, String> getErrorDetails(JSONObject jsonObject) {
return null;
}
public static String method1() {
Map<String, String> map = getErrorDetails(new JSONObject());
return map.get("abc");
}
}
Test method
#RunWith(PowerMockRunner.class)
#PrepareForTest(GenerateResponse.class)
public class GenerateResponseTest {
#Test
public void testFrameQtcErrorResponse() throws Exception {
Map<String, String> errorDtls = new HashMap<String, String>();
errorDtls.put("abc", "alphabets");
PowerMockito.mockStatic(GenerateResponse.class, Mockito.CALLS_REAL_METHODS);
PowerMockito.doReturn(errorDtls).when(GenerateResponse.class,
"getErrorDetails", Matchers.any(JSONObject.class));
String response = GenerateResponse.method1();
System.out.println("response =" + response);
}
}
Output
response =alphabets