Error calling method using same data (new data is always generated) - java

I have the following problem to be solved, could you help me?
I have two methods in a class.
The first generates a document (calling another class) and stores it in a string.
The second one I want to save this document number for use in other methods and in other classes, in a way that the document is the same generated initially. That is, do not generate a different document!
I'm not getting ... = //
First Methods in one class (generates document, calling a method of another class):
public class oneClass {
private String cpf;
private String document() {
if (this.cpf == null) {
this.cpf = incluiDocumento.cpf(false);
} else {
}
return this.cpf;
}
public void one() {
System.out.println(document());
System.out.println(document());
System.out.println(document());
}
public void two() {
System.out.println(document());
}
}
Second class:
#Test
public void testDocuments() {
new oneClass().one();
new oneClass().two();
}
Conclusion:
I can generate my document and store it in a string. However, in the next methods and classes, I can never use the first document ever generated. It will always generate new documents.
How can I generate a document and store it for use in tests and validate it?
Tool: Selenium Webdriver, Java.
Thanks in advance!!!

In this case you might use this approach:
public class OneClass{
private String cpf;
//...
public String document() {
if(this.cpf==null){
this.cpf = document.cpf(false);
}
return this.cpf;
}
//... method one() and two()
}
The document is created only once and saved in a class variable. Any call after that will return the saved document.
So the Second Method will always get the first document generated.
Edit:
And test it like in the following:
#Test
public void testDocuments() {
OneClass oneClass = new OneClass();
oneClass.one();
oneClass.two();
}
I changed the name of your class from oneClass to OneClass because in Java class names start with capital letter.

Related

Java: How to fix code duplication in multiple methods?

I have some theoretical knowledge related to Design Patterns and now I have some issues to make these info and another ones in action.
I have the following 2 methods in my ServiceImpl class:
#Override
public MultipartFile exportA() throws IOException {
// repeated lines-I same as exportB method (code omitted for brevity)
// other lines special to exportA method
// repeated lines-II same as exportB method (code omitted for brevity)
}
#Override
public MultipartFile exportB() throws IOException {
// repeated lines-I same as exportA method (code omitted for brevity)
// other lines special to exportB method
// repeated lines-II same as exportA method (code omitted for brevity)
}
As it is shown, there are repeated parts in all of these methods. So, should I create 2 methods for repeated lines-I and II, eand then move these code blocks to these newly created 2 methods? Or, is there a better way for Design Patterns?
If I have well understood your statement, this sounds to me a
Builder Pattern that you are looking for. You methods are building a MultipartFile whereas the build process itself depends on 'arguments/parameters' (here I guess sheet file path) and distinct code (the one you referred to as "other lines special to this method").
For that I would create a class MultipartFileBuilder that does the staff and that I would call in each method; of course, by means of setting the appropriate parameters and "code" each time. The code is simply an implementation of the java.util.function.Consumer<T> functional interface used in the following code (*2) and other parameters are using simple setters as well (here (*1)).
Note that I invoked the Consumer<T> as lambda expression here (the c->... construct). And note also that the type parameter <T> in Consumer<T> here is a new class I introduced MultipartFileBuildContext to allow multiple information to be passed to the code you are willing to write in each method. I guest the 'sheet' var would be a starting point. You can add other information if you need to.
To summer up things, this is how the code would look :
#Override
public MultipartFile exportMethodA() throws IOException {
return new MultipartFileBuilder()
.setSheetPath("sheetA/path") (*1)
.setAction(c->{
//(*2) do your staff here for method A
// the "other lines special to this method"
XSSFSheet sheet=c.getSheet()
...
}).build();
}
#Override
public MultipartFile exportMethodB() throws IOException {
return new MultipartFileBuilder()
.setSheetPath("sheetB/path") (*1)
.setAction(c->{
//(*2) do your staff here for method B
// the "other lines special to this method"
XSSFSheet sheet=c.getSheet()
...
}).build();
}
class MultipartFileBuildContext {
private XSSFSheet sheet;
public MultipartFileBuildContext(XSSFSheet sheet){this.sheet=sheet;}
public String getSheetPath() {
return sheetPath;
}
}
class MultipartFileBuilder {
String sheetPath;
Consumer<MultipartFileBuildContext> action;
public String getSheetPath() {
return sheetPath;
}
public MultipartFileBuilder setSheetPath(String sheetPath) {
this.sheetPath = sheetPath;
return this;
}
public Consumer<MultipartFileBuildContext> getAction() {
return action;
}
public MultipartFileBuilder setAction(Consumer<MultipartFileBuildContext> action) {
this.action = action;
return this;
}
public MockMultipartFile build() {
// repeated lines
workbook = new XSSFWorkbook();
sheet = workbook.createSheet(sheetPath);
//
if(action!=null){
MultipartFileBuildContext c=new MultipartFileBuildContext(sheet);
action.accept(c);
}
// repeated lines ======================================================
outputFile = File.createTempFile(...);
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
workbook.write(outputStream);
} catch (IOException e) {
LoggingUtils.error("Writing failed ", e);
}
final FileInputStream input = new FileInputStream(outputFile);
final String fileName = TextBundleUtil.read(...);
return new MockMultipartFile(fileName,
fileName, CONTENT_TYPE, IOUtils.toByteArray(input));
//
}
}
At the end, this pattern needs to be used with care because you need to factorize all that you can to make the builder really useful, but not too much to make it a "boat of anything". For instance, you can have as input the sheet path or an inputstream of it to make it more useful/generic.

Is there a way to know inside a #MethodSource for which test method it's providing data?

In my TestClass I have a lot of different test methods that all have to run with the same data and therefore have the same #MethodSource:
public class TestClass {
private class TestData { /* some fields */ }
private List<TestData> testDataTemplates = asList(
new TestData(...),
/* some more elements */
);
static private Stream<Arguments> testDataProvider {
List<TestData> testData = new ArrayList<>();
// Here we do some nested .forEach iterating over the testDataTemplates
return testData.stream();
}
#ParametrizedTest
#MethodSource("testDataProvider")
public void testMethod1(...) { }
// some more test methods
#ParametrizedTest
#MethodSource("testDataProvider")
public void testMethodN(...) { }
}
For testMethodN it is necessary to have the data a tiny bit different than for the other methods. My idea was to detect inside testDataProvider for which method it is currently generating data and adapt the output accordingly.
Is there a way to do so?
If not, what it the cleanest way to achieve what I need to, underlining again, that the output of testDataProvider for testMethodN is really really similar to the output for the other test methods?
One could solve this using a new #MethodSource:
static private Stream<Arguments> filteredTestDataProvider() {
List<Arguments> testCases = new ArrayList<>();
testDataProvider().forEach(args -> {
// decide whether and how the current args are used
});
return testCases.stream();
}
And, of course, the #MethodSource("filteredTestDataProvider") of testMethodN should be adapted accordingly.

What are my options to prevent duplicate code in Java in my case

I got about 6 classes that are doing 'almost' the same with different values. I'll give two classes and an example below and then describe what i'm doing.
public class AttributeRangeRule implements Template {
#Override
public String writeTemplate(BusinessRule businessRule) throws Exception {
String link = TemplateReader.getInstance().getLinkToQuery(businessRule.getBusinessRuleTypeCode());
String template = TemplateReader.getInstance().readQuery(link);
ST templateFixer = new ST(template);
templateFixer.add("code", businessRule.getBusinessRuleTypeCode());
templateFixer.add("attribute_table", businessRule.getListOfTables().get(0).getName());
templateFixer.add("operator", businessRule.getOperator().getName());
templateFixer.add("range_min", businessRule.getListOfValues().get(0).getValue());
templateFixer.add("range_max", businessRule.getListOfValues().get(1).getValue());
templateFixer.add("attribute_column", businessRule.getListOfColumns().get(0).getName());
templateFixer.add("error", businessRule.getErrorMessage());
templateFixer.add("GreaterOrEqual", ">=");
templateFixer.add("LessOrEqual", "<=");
templateFixer.add("LessThen", "<");
templateFixer.add("GreaterThen", ">");
String templateDLL = templateFixer.render();
return templateDLL;
}
}
public class AttributeCompareRule implements Template {
#Override
public String writeTemplate(BusinessRule businessRule) throws Exception {
String link = TemplateReader.getInstance().getLinkToQuery(businessRule.getBusinessRuleTypeCode());
String template = TemplateReader.getInstance().readQuery(link);
ST templateFixer = new ST(template);
templateFixer.add("code", businessRule.getBusinessRuleTypeCode());
templateFixer.add("attribute_table", businessRule.getListOfTables().get(0).getName());
templateFixer.add("operand", businessRule.getOperator().getName());
templateFixer.add("compare_with", businessRule.getListOfValues().get(0).getValue());
templateFixer.add("error", businessRule.getErrorMessage());
String templateDLL = templateFixer.render();
return templateDLL;
}
}
templateFixer.add("code..") is for example duplicate. They are written the same in both classes but the value is different.
I have different classes with different implementation of the method writeTemplate(). As you can see AttributeRangeRule is different from AttributeCompareRule. This code is writting a query for me. Intellij is telling me that the code is duplicate even if the values arent unique. I have no idea how to solve this issue. How can i solve this issue, since duplicate code isnt the best to have in your code. Thanks in advance.
You should try to take advantage of OOPS concepts here and use inheritance here
You can create a base class named AttributeRule that overrides writeTemplate() method and put all the redundant code in there and have this class be extended by your subclasses i.e. AttributeCompareRule and AttributeCompareRule
Here's the conceptual idea and some snippets
public class AttributeRule implements Template {
#Override
public String writeTemplate(BusinessRule businessRule) throws Exception {
String link = TemplateReader.getInstance().getLinkToQuery(businessRule.getBusinessRuleTypeCode());
String template = TemplateReader.getInstance().readQuery(link);
ST templateFixer = new ST(template);
templateFixer.add("code", businessRule.getBusinessRuleTypeCode());
templateFixer.add("attribute_table", businessRule.getListOfTables().get(0).getName());
}
}
public class AttributeCompareRule extends AttributeRule {
#Override
public String writeTemplate(BusinessRule businessRule) throws Exception {
super.writeTemplate(rule);
// Custom class code here
templateFixer.add("operand", businessRule.getOperator().getName());
templateFixer.add("compare_with", businessRule.getListOfValues().get(0).getValue());
templateFixer.add("error", businessRule.getErrorMessage());
String templateDLL = templateFixer.render();
return templateDLL;
}
}
public class AttributeRangeRule extends AttributeRule {
super.writeTemplate(rule);
// Custom class code here
}

Javassist - redirect field access to methods (doesn't work)

I have the following Class
public class Booking{
public String name;
public String comment;
public String session;
public void test(){
this.name = "hi";
}
}
I instrument it using the following:
cc.instrument(new ExprEditor(){
public void edit(FieldAccess arg) throws CannotCompileException {
if (arg.isWriter()) {
StringBuffer code = new StringBuffer();
code.append("$0.");
code.append(arg.getFieldName());
code.append("=$1.toUpperCase();");
arg.replace(code.toString());
}
}
});
Now when I call this:
Booking b = new Booking();
b.name = "hello";
System.out.println(b.name); // Edited correction
b.test();
System.out.println(b.name);
Gives me
hello // Externally, doesn't.
HI // Internally, works as expected
What am I missing? It just seems like one of those things I should be able to accomplish easily.
Please don't tell me I have to do a blanket "fieldAccess.replace" on all classes? O.O
Your example code fragment that contains the statement b.name = "hello"; isn't being instrumented, hence the value it writes is not converted to uppercase. An ExprEditor can only transform the field access from classes that are instrumented by it. If you want every write to the 'name' field converted to uppercase, you will have to instrument every class that contains a write statement for that field.

Mocking member variables of a class using Mockito

I am a newbie to development and to unit tests in particular .
I guess my requirement is pretty simple, but I am keen to know others thoughts on this.
Suppose I have two classes like so -
public class First {
Second second ;
public First(){
second = new Second();
}
public String doSecond(){
return second.doSecond();
}
}
class Second {
public String doSecond(){
return "Do Something";
}
}
Let's say I am writing unit test to test First.doSecond() method. However, suppose, i want to Mock Second.doSecond() class like so. I am using Mockito to do this.
public void testFirst(){
Second sec = mock(Second.class);
when(sec.doSecond()).thenReturn("Stubbed Second");
First first = new First();
assertEquals("Stubbed Second", first.doSecond());
}
I am seeing that the mocking does not take effect and the assertion fails.
Is there no way to mock the member variables of a class that I want to test . ?
You need to provide a way of accessing the member variables so you can pass in a mock (the most common ways would be a setter method or a constructor which takes a parameter).
If your code doesn't provide a way of doing this, it's incorrectly factored for TDD (Test Driven Development).
This is not possible if you can't change your code. But I like dependency injection and Mockito supports it:
public class First {
#Resource
Second second;
public First() {
second = new Second();
}
public String doSecond() {
return second.doSecond();
}
}
Your test:
#RunWith(MockitoJUnitRunner.class)
public class YourTest {
#Mock
Second second;
#InjectMocks
First first = new First();
public void testFirst(){
when(second.doSecond()).thenReturn("Stubbed Second");
assertEquals("Stubbed Second", first.doSecond());
}
}
This is very nice and easy.
If you look closely at your code you'll see that the second property in your test is still an instance of Second, not a mock (you don't pass the mock to first in your code).
The simplest way would be to create a setter for second in First class and pass it the mock explicitly.
Like this:
public class First {
Second second ;
public First(){
second = new Second();
}
public String doSecond(){
return second.doSecond();
}
public void setSecond(Second second) {
this.second = second;
}
}
class Second {
public String doSecond(){
return "Do Something";
}
}
....
public void testFirst(){
Second sec = mock(Second.class);
when(sec.doSecond()).thenReturn("Stubbed Second");
First first = new First();
first.setSecond(sec)
assertEquals("Stubbed Second", first.doSecond());
}
Another would be to pass a Second instance as First's constructor parameter.
If you can't modify the code, I think the only option would be to use reflection:
public void testFirst(){
Second sec = mock(Second.class);
when(sec.doSecond()).thenReturn("Stubbed Second");
First first = new First();
Field privateField = PrivateObject.class.
getDeclaredField("second");
privateField.setAccessible(true);
privateField.set(first, sec);
assertEquals("Stubbed Second", first.doSecond());
}
But you probably can, as it's rare to do tests on code you don't control (although one can imagine a scenario where you have to test an external library cause it's author didn't :))
You can mock member variables of a Mockito Mock with ReflectionTestUtils
ReflectionTestUtils.setField(yourMock, "memberFieldName", value);
If you can't change the member variable, then the other way around this is to use powerMockit and call
Second second = mock(Second.class)
when(second.doSecond()).thenReturn("Stubbed Second");
whenNew(Second.class).withAnyArguments.thenReturn(second);
Now the problem is that ANY call to new Second will return the same mocked instance. But in your simple case this will work.
I had the same issue where a private value was not set because Mockito does not call super constructors. Here is how I augment mocking with reflection.
First, I created a TestUtils class that contains many helpful utils including these reflection methods. Reflection access is a bit wonky to implement each time. I created these methods to test code on projects that, for one reason or another, had no mocking package and I was not invited to include it.
public class TestUtils {
// get a static class value
public static Object reflectValue(Class<?> classToReflect, String fieldNameValueToFetch) {
try {
Field reflectField = reflectField(classToReflect, fieldNameValueToFetch);
reflectField.setAccessible(true);
Object reflectValue = reflectField.get(classToReflect);
return reflectValue;
} catch (Exception e) {
fail("Failed to reflect "+fieldNameValueToFetch);
}
return null;
}
// get an instance value
public static Object reflectValue(Object objToReflect, String fieldNameValueToFetch) {
try {
Field reflectField = reflectField(objToReflect.getClass(), fieldNameValueToFetch);
Object reflectValue = reflectField.get(objToReflect);
return reflectValue;
} catch (Exception e) {
fail("Failed to reflect "+fieldNameValueToFetch);
}
return null;
}
// find a field in the class tree
public static Field reflectField(Class<?> classToReflect, String fieldNameValueToFetch) {
try {
Field reflectField = null;
Class<?> classForReflect = classToReflect;
do {
try {
reflectField = classForReflect.getDeclaredField(fieldNameValueToFetch);
} catch (NoSuchFieldException e) {
classForReflect = classForReflect.getSuperclass();
}
} while (reflectField==null || classForReflect==null);
reflectField.setAccessible(true);
return reflectField;
} catch (Exception e) {
fail("Failed to reflect "+fieldNameValueToFetch +" from "+ classToReflect);
}
return null;
}
// set a value with no setter
public static void refectSetValue(Object objToReflect, String fieldNameToSet, Object valueToSet) {
try {
Field reflectField = reflectField(objToReflect.getClass(), fieldNameToSet);
reflectField.set(objToReflect, valueToSet);
} catch (Exception e) {
fail("Failed to reflectively set "+ fieldNameToSet +"="+ valueToSet);
}
}
}
Then I can test the class with a private variable like this. This is useful for mocking deep in class trees that you have no control as well.
#Test
public void testWithRectiveMock() throws Exception {
// mock the base class using Mockito
ClassToMock mock = Mockito.mock(ClassToMock.class);
TestUtils.refectSetValue(mock, "privateVariable", "newValue");
// and this does not prevent normal mocking
Mockito.when(mock.somthingElse()).thenReturn("anotherThing");
// ... then do your asserts
}
I modified my code from my actual project here, in page. There could be a compile issue or two. I think you get the general idea. Feel free to grab the code and use it if you find it useful.
If you want an alternative to ReflectionTestUtils from Spring in mockito, use
Whitebox.setInternalState(first, "second", sec);
Lots of others have already advised you to rethink your code to make it more testable - good advice and usually simpler than what I'm about to suggest.
If you can't change the code to make it more testable, PowerMock: https://code.google.com/p/powermock/
PowerMock extends Mockito (so you don't have to learn a new mock framework), providing additional functionality. This includes the ability to have a constructor return a mock. Powerful, but a little complicated - so use it judiciously.
You use a different Mock runner. And you need to prepare the class that is going to invoke the constructor. (Note that this is a common gotcha - prepare the class that calls the constructor, not the constructed class)
#RunWith(PowerMockRunner.class)
#PrepareForTest({First.class})
Then in your test set-up, you can use the whenNew method to have the constructor return a mock
whenNew(Second.class).withAnyArguments().thenReturn(mock(Second.class));
Yes, this can be done, as the following test shows (written with the JMockit mocking API, which I develop):
#Test
public void testFirst(#Mocked final Second sec) {
new NonStrictExpectations() {{ sec.doSecond(); result = "Stubbed Second"; }};
First first = new First();
assertEquals("Stubbed Second", first.doSecond());
}
With Mockito, however, such a test cannot be written. This is due to the way mocking is implemented in Mockito, where a subclass of the class to be mocked is created; only instances of this "mock" subclass can have mocked behavior, so you need to have the tested code use them instead of any other instance.

Categories