单元测试用于Spring Boot应用程序。

huangapple 未分类评论54阅读模式
英文:

Unit Tests for spring boot application

问题

public class WordService {
    
    public WordService() {
        
    }
    
    public static String upperCaseFirst(String value) {
        char[] listChar = value.toCharArray();
        listChar[0] = Character.toUpperCase(listChar[0]);
        return new String(listChar);
    }
    
    /**
     * Find and return the search word
     * @param name
     * @return the word sought or null if not found
     */
    public Word findWordByName(String name){
        
        String nameUpper = upperCaseFirst(name);
        
        WordDao w = new WordDao();
        Word found = w.findWord(nameUpper);
        
        List<String> definitions = new ArrayList<>();
        
        if(found != null) {
            for(int i=0; i<found.getDefinition().size(); i++) {
                StringBuffer defBuffer = new StringBuffer();
                
                String definitionFound = found.getDefinition().get(i);
                definitionFound = definitionFound.replace("\n", "");
                
                defBuffer.append(definitionFound);
                defBuffer.append("_");
                
                definitions.add(i, defBuffer.toString());
            }
            found.setDefinition(definitions);
        }
        return found;
    }
    
    /**
     * 
     * @return Return a list of words
     */
    public List<Word> findAllWord(){
        
        WordDao w = new WordDao();
        return w.findAllWords();
    }
}
英文:

I have never used JUnit testing before.I need to test my code with JUnit.
I have been searching google for all day but the problem is that I found examples using Mockito but in my code I didn't use dependency injections(@Autowired).
How can i use it for these methods?

Thanks in advance.

public class WordService {

public WordService() {
	
}


public static String upperCaseFirst(String value) {
    char[] listChar = value.toCharArray();
    listChar[0] = Character.toUpperCase(listChar[0]);
    return new String(listChar);
}

/**
 * Find and return the search word
 * @param name
 * @return the word sought or null if not found
 */
public Word findWordByName(String name){
	
	String nameUpper = upperCaseFirst(name);
	
	WordDao w = new WordDao();
	Word found = w.findWord(nameUpper);
	
	List&lt;String&gt; definitions = new ArrayList&lt;&gt;();
	
	if(found != null) {
		for(int i=0; i&lt;found.getDefinition().size(); i++) {
			StringBuffer defBuffer = new StringBuffer();
			
			String definitionFound = found.getDefinition().get(i);
			definitionFound = definitionFound.replace(&quot;\n&quot;, &quot;&quot;);
			
			defBuffer.append(definitionFound);
			defBuffer.append(&quot;_&quot;);
			
			definitions.add(i, defBuffer.toString());
		}
		found.setDefinition(definitions);
	}
	return found;
}


/**
 * 
 * @return Return a list of words
 */
public List&lt;Word&gt; findAllWord(){
	
	WordDao w = new WordDao();
	return w.findAllWords();
}

}

答案1

得分: 0

你可以将WordDao提取为类级别的字段,并创建set方法。
然后在单元测试中,你可以模拟WordDao并控制方法调用的结果。对于第二种方法,可以像这样实现:

WordDao wMocked = Mock(WordDao.class);
Word word1 = new...
Word word2 = new...
List<Word> words = List.of(word1, word2);
when(w.findAllWords()).thenReturn(words);
WordService ws = new WordService();
ws.setWordDao(wMocked);
Assert.equals(words, ws.findAllWords);
英文:

You can extract WordDao to class level as a field. Create set method.
After that in unit test you can mock WordDao and control what will be result of methods call. For the second method it something like:

WordDao wMocked = Mock(WordDao.class)
Word word1 = new...
Word word2 = new...
List&lt;Word&gt; words = List.of(word1, word2);
when(w.findAllWords()).thenReturn(words);
WordService ws = new WordService();
ws.setWordDao(wMocked);
Assert.equals(words, ws.findAllWords);

huangapple
  • 本文由 发表于 2020年4月4日 21:41:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61028980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定