There is a code that, according to the Euclidean algorithm, finds the greatest common divisor.

import java.util.Scanner; /** * Created by user on 24.11.2015. * По данным двум числам 1<a,b<2*10^9 найдите их наибольший общий делитель. */ public class Euclid { public static int Euclid(int a, int b) { //конструктор if (a == 0 || b == 0) { //Рассматривается случай, когда одно из if (a == 0) { //делимых равно нулю return b; } else { return a; } } else { if (a > b) { return Euclid(a % b, b); //рекурсивно вызовется } // алгоритм, если будет остаток от деления большего if (b > a) { // числа на меньшее и наоборот return Euclid(a, b % a); } else return Euclid(a % b, b); } } public static void main(String[] args) { Scanner sc1 = new Scanner(System.in); //ввод с клавиатуры Scanner sc2 = new Scanner(System.in); int a = sc1.nextInt(); int b = sc2.nextInt(); System.out.println(Euclid(a,b)); } } 

Despite the fact that the program works, I decided to practice practicing testing on it.

I am writing a test:

 import org.junit.Test; import static org.junit.Assert.*; public class EuclidTest { @Test public void testEuclid() throws Exception { int result = new Euclid(234, 45); //в этой строке ошибка компиляции assertEquals(9, result, 1e-9); } } 

In the line where I declare result I get a compilation error. enter image description here

When I rewrote the class, replacing the constructor with a method with a different name, everything went like clockwork. Related issues:

  1. Is it possible to cover the constructor with tests so as not to cause a compilation error?
  2. How to do it?
  • one
    There should be no logic in the constructor at all. The constructor is intended to initialize the fields, and the more so it cannot return anything. - etki

1 answer 1

 public static int Euclid(int a, int b) { //конструктор 

The fact is that this is not a constructor for you, but a static method that returns an int . The constructor would be like this (it returns an object of the Euclid class):

 public Euclid(int a, int b) { //конструктор 

In general, the constructor is not needed here in any way. You do not need to store any state, so there is no need to create objects.

Rename your euclid method with a small letter, as expected by the naming standard and write this test:

 import org.junit.Test; import static org.junit.Assert.*; public class EuclidTest { @Test public void testEuclid() throws Exception { assertEquals(9, euclid(234, 45), 1e-9); } } 
  • 3
    And if it was a designer, keeping logic in it is a bad form. - Nofate
  • @Nofate: thanks, didn’t notice the static extra) - Nick Volynkin
  • @ Nick Volynkin, thanks, but you can clarify: aren't static methods covered by tests? And constructors, in principle, can or cannot be covered with tests? Explain please! - Andrew Kachalin
  • @AndrewKachalin: constructors (+ factory methods) can and should be tested, but based on what the requirements for the object are. I will try to come up with a good example. - Nick Volynkin