- Its my first projet in Maven, i have code like in below and, How can i edit my code to run tests, with parameters that I sorted in main. I wonna to write test with parameters a and b, I tried getters but didnt work. How can I extract this varaibles ?
MAIN
import org.junit.Before;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.ToIntBiFunction;
import static jdk.xml.internal.SecuritySupport.getContextClassLoader;
public class Calculator {
private Calculator calculator;
@Before
void setUp() throws IOException{
try {
InputStream in = getContextClassLoader().getResourceAsStream("src/file.txt");
calculator = new Calculator(in);
List<String> list = Files.readAllLines(Paths.get("src/file.txt"));
int apply = Integer.valueOf(list.get(list.size() - 1).split(" ")[1]);
Map<String, ToIntBiFunction<Integer, Integer>> map = prepMap();
for (int i = 0; i < list.size() - 1; i++) {
apply = map.get(list.get(i).split(" ")[0]).applyAsInt(apply, Integer.valueOf(list.get(i).split(" ")[1]));
}
System.out.println(apply);
} catch (IOException e) {
e.printStackTrace();
}
}
public int add(int a, int b) {
return a+b; }
public int subtract(int a, int b) {
return a-b;
}
public int multiply(int a, int b) {
return a*b;
}
public int divide(int a, int b) {
return a/b;
}
public Map<String, ToIntBiFunction<Integer, Integer>> prepMap() {
Map<String, ToIntBiFunction<Integer, Integer>> map = new HashMap<>();
map.put("multiply", (a, b) -> a * b);
map.put("add", (a, b) -> a + b);
map.put("devide", (a, b) -> {
if (b == 0) {
System.out.println("deviding by 0\nskiping this opperation");
return a;
}
return a / b;
});
map.put("subtract", (a, b) -> a - b);
return map;
}
}
Example for testing file add 2 multiply 3 apply 10
My Tests
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.ToIntBiFunction;
import java.util.logging.Logger;
import static org.assertj.core.api.Assertions.assertThat;
public class CalculatorTests extends Calculator{
private static Logger LOGGER = Logger.getLogger("InfoLogging");
private String path= "src/file.txt";
public CalculatorTests(String path) {
super(path);
}
@Test
public void testAdd() {
LOGGER.info("Running When Case1: testAdd");
//given
Calculator calculator = new Calculator(path);
//when
Scanner sc1 = new Scanner(path);
Scanner sc2 = new Scanner(path);
int result = calculator.add(10, 2);
//then
assertThat(result).isEqualTo(12);
}
@Test
public void testSubtract() {
LOGGER.info("Running When Case2: testSubtract");
//given
Calculator calculator = new Calculator(path);
//when
int result = calculator.subtract(3, 1);
//then
assertThat(result).isEqualTo(2);
}
@Test
public void testMultiply() {
LOGGER.info("Running When Case3: testMultiply");
//given
Calculator calculator = new Calculator(path);
//when
int result = calculator.multiply(12, 3);
//then
assertThat(result).isEqualTo(36);
}
@Test
public void testDivide() {
LOGGER.info("Running When Case4: testDivide");
//given
Calculator calculator = new Calculator(path);
//when
int result = calculator.divide(4,2);
//then
assertThat(result).isEqualTo(2);
}
}
I was trying with scanners too but didnt worked, and now I have no idea what to change in code to make it work. I search for ansers but couldnt find any, thats why im writing here, dont hate me pls :(